#include <cstdio> #include <iostream> #include <map> #include <string> #include <vector> #include <queue> #include <stack> using namespace std; // 1 - zolty // 2 - niebieski // 3 - czerwony // zielony = 1 + 2 class Elem; Elem *tab; int minIdx = 10000000; int maxIdx = -1; class Elem { public: int from, to; int idx; bool flag[4]; void addColor(int color, int sa, int sb) { stack<int> qa; stack<int> qb; stack<int> qi; qa.push(sa); qb.push(sb); qi.push(idx); while(!qa.empty()) { int a = qa.top(); qa.pop(); int b = qb.top(); qb.pop(); int idx = qi.top(); qi.pop(); // cout << a << " " << b << " " << idx << endl; // a <= b if (tab[idx].from == a && tab[idx].to == b) { tab[idx].flag[color] = true; } else { Elem *left = &tab[2 * idx]; Elem *right = &tab[2 * idx + 1]; // from <= a <= b <= to int a1 = a; int b1 = min(left->to, b); if (a1 <= b1) { qa.push(a1); qb.push(b1); qi.push(2*idx); // left->addColor(color, a1, b1); } int a2 = max(right->from, a); int b2 = min(right->to, b); if (a2 <= b2) { qa.push(a2); qb.push(b2); qi.push(2*idx+1); // right->addColor(color, a2, b2); } } } } Elem() { } void init(int _from, int _to, int _idx) { stack<int> sfrom; stack<int> sto; stack<int> sidx; sfrom.push(_from); sto.push(_to); sidx.push(_idx); while(!sfrom.empty()) { int from = sfrom.top(); sfrom.pop(); int to = sto.top(); sto.pop(); int idx = sidx.top(); sidx.pop(); tab[idx].from = from; tab[idx].to = to; tab[idx].idx = idx; for (int i = 0; i < 4; i++) { tab[idx].flag[i] = false; } if (from < to) { int x = to - from + 1; x /= 2; sfrom.push(from); sto.push(from + x - 1); sidx.push(2*idx); // tab[2 * idx].init(from, from + x - 1, 2 * idx); sfrom.push(from + x); sto.push(to); sidx.push(2*idx + 1); // tab[2 * idx + 1].init(from + x, to, 2 * idx + 1); } else { minIdx = min(minIdx, idx); maxIdx = max(maxIdx, idx); } } } }; void ole(bool* f, int start) { while(start >= 1) { for(int i=0; i<4; i++) { f[i] |= tab[start].flag[i]; } start /= 2; } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n, m; scanf("%d %d", &n, &m); // cin >> n >> m; int x = 1; while (x < n) { x *= 2; } tab = new Elem[2*x+1]; tab[1].init(1, x, 1); // cout << "done init" << endl; int l, r, k; for (int i = 0; i < m; ++i) { // cin >> l >> r >> k; scanf("%d %d %d", &l, &r, &k); tab[1].addColor(k, l, r); } // cout << "done reading" << endl; bool flag[4]; int res = 0; for(int i=minIdx; i<=maxIdx; ++i) { for(int i=0; i<4; ++i) { flag[i] = false; } ole(flag, i); if(flag[1] && flag[2] &&!flag[3]) { res++; } } // cout << res << endl; printf("%d\n", res); }
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 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 | #include <cstdio> #include <iostream> #include <map> #include <string> #include <vector> #include <queue> #include <stack> using namespace std; // 1 - zolty // 2 - niebieski // 3 - czerwony // zielony = 1 + 2 class Elem; Elem *tab; int minIdx = 10000000; int maxIdx = -1; class Elem { public: int from, to; int idx; bool flag[4]; void addColor(int color, int sa, int sb) { stack<int> qa; stack<int> qb; stack<int> qi; qa.push(sa); qb.push(sb); qi.push(idx); while(!qa.empty()) { int a = qa.top(); qa.pop(); int b = qb.top(); qb.pop(); int idx = qi.top(); qi.pop(); // cout << a << " " << b << " " << idx << endl; // a <= b if (tab[idx].from == a && tab[idx].to == b) { tab[idx].flag[color] = true; } else { Elem *left = &tab[2 * idx]; Elem *right = &tab[2 * idx + 1]; // from <= a <= b <= to int a1 = a; int b1 = min(left->to, b); if (a1 <= b1) { qa.push(a1); qb.push(b1); qi.push(2*idx); // left->addColor(color, a1, b1); } int a2 = max(right->from, a); int b2 = min(right->to, b); if (a2 <= b2) { qa.push(a2); qb.push(b2); qi.push(2*idx+1); // right->addColor(color, a2, b2); } } } } Elem() { } void init(int _from, int _to, int _idx) { stack<int> sfrom; stack<int> sto; stack<int> sidx; sfrom.push(_from); sto.push(_to); sidx.push(_idx); while(!sfrom.empty()) { int from = sfrom.top(); sfrom.pop(); int to = sto.top(); sto.pop(); int idx = sidx.top(); sidx.pop(); tab[idx].from = from; tab[idx].to = to; tab[idx].idx = idx; for (int i = 0; i < 4; i++) { tab[idx].flag[i] = false; } if (from < to) { int x = to - from + 1; x /= 2; sfrom.push(from); sto.push(from + x - 1); sidx.push(2*idx); // tab[2 * idx].init(from, from + x - 1, 2 * idx); sfrom.push(from + x); sto.push(to); sidx.push(2*idx + 1); // tab[2 * idx + 1].init(from + x, to, 2 * idx + 1); } else { minIdx = min(minIdx, idx); maxIdx = max(maxIdx, idx); } } } }; void ole(bool* f, int start) { while(start >= 1) { for(int i=0; i<4; i++) { f[i] |= tab[start].flag[i]; } start /= 2; } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n, m; scanf("%d %d", &n, &m); // cin >> n >> m; int x = 1; while (x < n) { x *= 2; } tab = new Elem[2*x+1]; tab[1].init(1, x, 1); // cout << "done init" << endl; int l, r, k; for (int i = 0; i < m; ++i) { // cin >> l >> r >> k; scanf("%d %d %d", &l, &r, &k); tab[1].addColor(k, l, r); } // cout << "done reading" << endl; bool flag[4]; int res = 0; for(int i=minIdx; i<=maxIdx; ++i) { for(int i=0; i<4; ++i) { flag[i] = false; } ole(flag, i); if(flag[1] && flag[2] &&!flag[3]) { res++; } } // cout << res << endl; printf("%d\n", res); } |