#include <bits/stdc++.h> using namespace std; #define MAX_N 100000 #define MOD 1000000007 #define INF INT_MAX #define pb push_back #define mp(x, y) make_pair((x), (y)) typedef unsigned int uint; typedef unsigned long long ull; typedef long long ll; typedef pair<ll, ll> pll; typedef pair<int, int> pii; typedef pair<bool, int> pbi; #define RED 4 #define BLU 2 #define YEL 1 #define DES 3 // zielony uint R; uint *t; void update(int v, int val, int l, int r, int p, int k) { if (p > r || k < l) { return; } else if (p >= l && k <= r) { // printf("v: %d, val: %d, prz baz: <%d, %d>\n", v, val, p, k); t[v] |= val; return; } update(2 * v, val, l, r, p, (p + k) / 2); update(2 * v + 1, val, l, r, (p + k) / 2 + 1, k); } uint wyn = 0; int n; int counter[5]; inline void check_if_no(int v, uint &val) { counter[RED] -= ((t[v] & RED) == RED); counter[BLU] -= ((t[v] & BLU) == BLU); counter[YEL] -= ((t[v] & YEL) == YEL); if ((val & RED) && !counter[RED]) { val -= RED; } if ((val & BLU) && !counter[BLU]) { val -= BLU; } if ((val & YEL) && !counter[YEL]) { val -= YEL; } } inline void count(int v, uint &val) { if (v >= 2 * R) { return; } val |= t[v]; counter[RED] += ((t[v] & RED) == RED); counter[BLU] += ((t[v] & BLU) == BLU); counter[YEL] += ((t[v] & YEL) == YEL); // printf("%d: %d || ", v, val); // printf("%d %d %d\n", counter[RED], counter[BLU], counter[YEL]); if (val >= RED) { // printf("wrr %d, t[v]: %d\n", v); check_if_no(v, val); return; } if (val == DES && v >= R) { // printf("OK: %d\n", v); wyn++; check_if_no(v, val); return; } count(v * 2, val); count(v * 2 + 1, val); check_if_no(v, val); } void init_tree() { R = 1; while (R < n) { R *= 2; } t = new uint[2 * R]; counter[RED] = 0; counter[BLU] = 0; counter[YEL] = 0; } // ---------- // SOLUTION // ---------- int main() { int m; scanf("%d%d", &n, &m); init_tree(); for (int i = 0; i < m; i++) { int l, r, k; scanf("%d%d%d", &l, &r, &k); --r; --l; update(1, k == 3 ? 4 : k, l, r, 0, R - 1); } uint val = 0; count(1, val); printf("%d\n", wyn); }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 | #include <bits/stdc++.h> using namespace std; #define MAX_N 100000 #define MOD 1000000007 #define INF INT_MAX #define pb push_back #define mp(x, y) make_pair((x), (y)) typedef unsigned int uint; typedef unsigned long long ull; typedef long long ll; typedef pair<ll, ll> pll; typedef pair<int, int> pii; typedef pair<bool, int> pbi; #define RED 4 #define BLU 2 #define YEL 1 #define DES 3 // zielony uint R; uint *t; void update(int v, int val, int l, int r, int p, int k) { if (p > r || k < l) { return; } else if (p >= l && k <= r) { // printf("v: %d, val: %d, prz baz: <%d, %d>\n", v, val, p, k); t[v] |= val; return; } update(2 * v, val, l, r, p, (p + k) / 2); update(2 * v + 1, val, l, r, (p + k) / 2 + 1, k); } uint wyn = 0; int n; int counter[5]; inline void check_if_no(int v, uint &val) { counter[RED] -= ((t[v] & RED) == RED); counter[BLU] -= ((t[v] & BLU) == BLU); counter[YEL] -= ((t[v] & YEL) == YEL); if ((val & RED) && !counter[RED]) { val -= RED; } if ((val & BLU) && !counter[BLU]) { val -= BLU; } if ((val & YEL) && !counter[YEL]) { val -= YEL; } } inline void count(int v, uint &val) { if (v >= 2 * R) { return; } val |= t[v]; counter[RED] += ((t[v] & RED) == RED); counter[BLU] += ((t[v] & BLU) == BLU); counter[YEL] += ((t[v] & YEL) == YEL); // printf("%d: %d || ", v, val); // printf("%d %d %d\n", counter[RED], counter[BLU], counter[YEL]); if (val >= RED) { // printf("wrr %d, t[v]: %d\n", v); check_if_no(v, val); return; } if (val == DES && v >= R) { // printf("OK: %d\n", v); wyn++; check_if_no(v, val); return; } count(v * 2, val); count(v * 2 + 1, val); check_if_no(v, val); } void init_tree() { R = 1; while (R < n) { R *= 2; } t = new uint[2 * R]; counter[RED] = 0; counter[BLU] = 0; counter[YEL] = 0; } // ---------- // SOLUTION // ---------- int main() { int m; scanf("%d%d", &n, &m); init_tree(); for (int i = 0; i < m; i++) { int l, r, k; scanf("%d%d%d", &l, &r, &k); --r; --l; update(1, k == 3 ? 4 : k, l, r, 0, R - 1); } uint val = 0; count(1, val); printf("%d\n", wyn); } |