#include <bits/stdc++.h> #define pb push_back using namespace std; void modify(vector<int>& t, int a, int b, int val, int M) { int va = M + a, vb = M + b; t[va] += val; if (va != vb) t[vb] += val; while (va / 2 != vb / 2) { if (va % 2 == 0) t[va + 1] += val; if (vb % 2 == 1) t[vb - 1] += val; va /= 2; vb /= 2; } } int query(vector<int>& t, int x, int M) { int v = M + x; int res = t[v]; while (v != 1) { v /= 2; res += t[v]; } return res; } int main() { std::ios::sync_with_stdio(false); int n, m; cin >> n >> m; int M = 1; while (M < n) { M = M * 2; } vector<int> yellow(2*M, 0); vector<int> blue(2*M, 0); vector<int> red(2*M, 0); for (int i=0; i<m; i++) { int l, r, v; cin >> l >> r >> v; if (v==1) { modify(yellow, l-1, r-1, v, M); } else if (v==2) { modify(blue, l-1, r-1, v, M); } else { modify(red, l-1, r-1, v, M); } } int res = 0; for (int i=0; i<n; i++) { int yy = query(yellow, i, M); int bb = query(blue, i, M); int rr = query(red, i, M); if (yy >= 1 && bb >= 1 && rr < 1) { res += 1; } } cout << res << endl; 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 61 62 63 64 65 66 67 68 69 | #include <bits/stdc++.h> #define pb push_back using namespace std; void modify(vector<int>& t, int a, int b, int val, int M) { int va = M + a, vb = M + b; t[va] += val; if (va != vb) t[vb] += val; while (va / 2 != vb / 2) { if (va % 2 == 0) t[va + 1] += val; if (vb % 2 == 1) t[vb - 1] += val; va /= 2; vb /= 2; } } int query(vector<int>& t, int x, int M) { int v = M + x; int res = t[v]; while (v != 1) { v /= 2; res += t[v]; } return res; } int main() { std::ios::sync_with_stdio(false); int n, m; cin >> n >> m; int M = 1; while (M < n) { M = M * 2; } vector<int> yellow(2*M, 0); vector<int> blue(2*M, 0); vector<int> red(2*M, 0); for (int i=0; i<m; i++) { int l, r, v; cin >> l >> r >> v; if (v==1) { modify(yellow, l-1, r-1, v, M); } else if (v==2) { modify(blue, l-1, r-1, v, M); } else { modify(red, l-1, r-1, v, M); } } int res = 0; for (int i=0; i<n; i++) { int yy = query(yellow, i, M); int bb = query(blue, i, M); int rr = query(red, i, M); if (yy >= 1 && bb >= 1 && rr < 1) { res += 1; } } cout << res << endl; return 0; } |