#include <iostream> #include <vector> using namespace std; vector<vector<bool>> Tree; void AddValues(int v, int l, int r, int L, int R, int k) { //cout << v << " " << l << " " << r << " " << L << " " << R << " " << k << endl; if (l == L && r == R) { Tree[v][k] = 1; return; } int m = (L + R - 1) / 2; if (r <= m) { AddValues(v * 2, l, r, L, m, k); } else if (l > m) { AddValues(v * 2 + 1, l, r, m + 1, R, k); } else { AddValues(v * 2, l, m, L, m, k); AddValues(v * 2 + 1, m + 1, r, m + 1, R, k); } } int main() { ios_base::sync_with_stdio(0); int n; cin >> n; int n2 = 1; while (n2 < n) { n2 *= 2; } int TreeLeaves = n2; Tree.resize(n2*2); for (int i = 0; i < n2 * 2; i++) { Tree[i].push_back(0); Tree[i].push_back(0); Tree[i].push_back(0); Tree[i].push_back(0); } int m; cin >> m; const int a = n2; const int b = TreeLeaves * 2 - 1; for (int i = 0; i < m; i++) { int l, r, k; cin >> l; cin >> r; cin >> k; AddValues(1, a + l - 1, a + r - 1, a, b, k); } int SumOfGreenCans = 0; for (int i = TreeLeaves; i < TreeLeaves + n; i++) { int j = i; vector<bool> Result = { 0, 0, 0, 0 }; while (j >= 1) { for (int q = 1; q <= 3; q++) { if (Result[q] == 0 && Tree[j][q] == 1) { Result[q] = 1; } } j /= 2; } if (Result[1] == 1 && Result[2] == 1 && Result[3] == 0) { SumOfGreenCans++; } } cout << SumOfGreenCans; }
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 | #include <iostream> #include <vector> using namespace std; vector<vector<bool>> Tree; void AddValues(int v, int l, int r, int L, int R, int k) { //cout << v << " " << l << " " << r << " " << L << " " << R << " " << k << endl; if (l == L && r == R) { Tree[v][k] = 1; return; } int m = (L + R - 1) / 2; if (r <= m) { AddValues(v * 2, l, r, L, m, k); } else if (l > m) { AddValues(v * 2 + 1, l, r, m + 1, R, k); } else { AddValues(v * 2, l, m, L, m, k); AddValues(v * 2 + 1, m + 1, r, m + 1, R, k); } } int main() { ios_base::sync_with_stdio(0); int n; cin >> n; int n2 = 1; while (n2 < n) { n2 *= 2; } int TreeLeaves = n2; Tree.resize(n2*2); for (int i = 0; i < n2 * 2; i++) { Tree[i].push_back(0); Tree[i].push_back(0); Tree[i].push_back(0); Tree[i].push_back(0); } int m; cin >> m; const int a = n2; const int b = TreeLeaves * 2 - 1; for (int i = 0; i < m; i++) { int l, r, k; cin >> l; cin >> r; cin >> k; AddValues(1, a + l - 1, a + r - 1, a, b, k); } int SumOfGreenCans = 0; for (int i = TreeLeaves; i < TreeLeaves + n; i++) { int j = i; vector<bool> Result = { 0, 0, 0, 0 }; while (j >= 1) { for (int q = 1; q <= 3; q++) { if (Result[q] == 0 && Tree[j][q] == 1) { Result[q] = 1; } } j /= 2; } if (Result[1] == 1 && Result[2] == 1 && Result[3] == 0) { SumOfGreenCans++; } } cout << SumOfGreenCans; } |