#include <algorithm> #include <iostream> using namespace std; const int maxN = 1e6 + 6; const int maxM = 1e6 + 6; const int base = 1 << 20; int mask(int k) { // k = 1 yellow // k = 2 blue // k = 3 red return 1 << (k - 1); } int green_mask = 3; int tree_cnt[2 * base][8]; int tree_collor[2 * base]; void collor_query(int qb, int qe, int k, int ib = 0, int ie = base, int id = 1) { if (qb <= ib && ie <= qe) { tree_collor[id] |= mask(k); return; } int mid = (ib + ie) / 2; if (qb < mid) collor_query(qb, qe, k, ib, mid, id * 2); if (qe > mid) collor_query(qb, qe, k, mid, ie, id * 2 + 1); for (int i = 0; i < 8; i++) { tree_cnt[id][i] = 0; } for (int i = 0; i < 8; i++) { tree_cnt[id][tree_collor[id * 2] | i] += tree_cnt[id * 2][i]; tree_cnt[id][tree_collor[id * 2 + 1] | i] += tree_cnt[id * 2 + 1][i]; } return; } int main() { ios_base::sync_with_stdio(0); cin.tie(); cout.tie(); int n, m; cin >> n >> m; for (int i = 1; i <= n; i++) { tree_cnt[i + base][0] = 1; } for (int i = base - 1; i > 0; i--) { tree_cnt[i][0] = tree_cnt[i * 2][0] + tree_cnt[i * 2 + 1][0]; } for (int l, r, k, i = 0; i < m; i++) { cin >> l >> r >> k; collor_query(l, r + 1, k); } int res = 0; for (int i = 0; i < 8; i++) { res += ((tree_collor[1] | i) == green_mask ? tree_cnt[1][i] : 0); } cout << res; return 0; }
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 | #include <algorithm> #include <iostream> using namespace std; const int maxN = 1e6 + 6; const int maxM = 1e6 + 6; const int base = 1 << 20; int mask(int k) { // k = 1 yellow // k = 2 blue // k = 3 red return 1 << (k - 1); } int green_mask = 3; int tree_cnt[2 * base][8]; int tree_collor[2 * base]; void collor_query(int qb, int qe, int k, int ib = 0, int ie = base, int id = 1) { if (qb <= ib && ie <= qe) { tree_collor[id] |= mask(k); return; } int mid = (ib + ie) / 2; if (qb < mid) collor_query(qb, qe, k, ib, mid, id * 2); if (qe > mid) collor_query(qb, qe, k, mid, ie, id * 2 + 1); for (int i = 0; i < 8; i++) { tree_cnt[id][i] = 0; } for (int i = 0; i < 8; i++) { tree_cnt[id][tree_collor[id * 2] | i] += tree_cnt[id * 2][i]; tree_cnt[id][tree_collor[id * 2 + 1] | i] += tree_cnt[id * 2 + 1][i]; } return; } int main() { ios_base::sync_with_stdio(0); cin.tie(); cout.tie(); int n, m; cin >> n >> m; for (int i = 1; i <= n; i++) { tree_cnt[i + base][0] = 1; } for (int i = base - 1; i > 0; i--) { tree_cnt[i][0] = tree_cnt[i * 2][0] + tree_cnt[i * 2 + 1][0]; } for (int l, r, k, i = 0; i < m; i++) { cin >> l >> r >> k; collor_query(l, r + 1, k); } int res = 0; for (int i = 0; i < 8; i++) { res += ((tree_collor[1] | i) == green_mask ? tree_cnt[1][i] : 0); } cout << res; return 0; } |