#include <bits/stdc++.h> using namespace std; typedef long long ll; const ll MAX = 3e6; char tree[MAX]; ll treeX; /** w - white y - yellow b - blue r - red o = y + r = o + r = o + y (orange) g = y + b = g + y = g + b (green) v = b + r = v + b = v + r (violet) z = o + b = g + r = v + y = z + _ (brown) */ char changeCol(char a, char b) { if (a == 'w' || a == b) return b; if ((a == 'y' && b == 'r') || (a == 'o' && b == 'r') || (a == 'o' && b == 'y')) return 'o'; if ((a == 'y' && b == 'b') || (a == 'g' && b == 'y') || (a == 'g' && b == 'b')) return 'g'; if ((a == 'b' && b == 'r') || (a == 'v' && b == 'b') || (a == 'v' && b == 'r')) return 'v'; if (a == 'z' || (a == 'o' && b == 'b') || (a == 'g' && b == 'r') || (a == 'v' && b == 'y') || (a == 'v' && b == 'g') || (a == 'v' && b == 'o') || (a == 'o' && b == 'g')) return 'z'; return changeCol(b, a); } void Ins(ll a, ll b, ll col) { char c = 'w'; switch (col) { case 1: c = 'y'; break; case 2: c = 'b'; break; case 3: c = 'r'; break; } a += treeX; b += treeX; tree[a] = changeCol(tree[a], c); tree[b] = changeCol(tree[b], c); while (a / 2 != b / 2) { if (a % 2 == 0) tree[a + 1] = changeCol(tree[a + 1], c); if (b % 2 == 1) tree[b - 1] = changeCol(tree[b - 1], c); a /= 2; b /= 2; } } char Query(ll a) { char res = 'w'; a += treeX; while (a != 0) { res = changeCol(res, tree[a]); a /= 2; } return res; } int main() { ios_base::sync_with_stdio(0); ll n, q; cin >> n >> q; treeX = 1; while (treeX < n) treeX *= 2; for (int i = 0; i < MAX; ++i) tree[i] = 'w'; ll a, b, col; for (int i = 0; i < q; ++i) { cin >> a >> b >> col; Ins(a-1, b-1, col); } ll cnt = 0; for (int i = 0; i < n; ++i) { if (Query(i) == 'g') cnt++; } cout << cnt << endl; }
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 | #include <bits/stdc++.h> using namespace std; typedef long long ll; const ll MAX = 3e6; char tree[MAX]; ll treeX; /** w - white y - yellow b - blue r - red o = y + r = o + r = o + y (orange) g = y + b = g + y = g + b (green) v = b + r = v + b = v + r (violet) z = o + b = g + r = v + y = z + _ (brown) */ char changeCol(char a, char b) { if (a == 'w' || a == b) return b; if ((a == 'y' && b == 'r') || (a == 'o' && b == 'r') || (a == 'o' && b == 'y')) return 'o'; if ((a == 'y' && b == 'b') || (a == 'g' && b == 'y') || (a == 'g' && b == 'b')) return 'g'; if ((a == 'b' && b == 'r') || (a == 'v' && b == 'b') || (a == 'v' && b == 'r')) return 'v'; if (a == 'z' || (a == 'o' && b == 'b') || (a == 'g' && b == 'r') || (a == 'v' && b == 'y') || (a == 'v' && b == 'g') || (a == 'v' && b == 'o') || (a == 'o' && b == 'g')) return 'z'; return changeCol(b, a); } void Ins(ll a, ll b, ll col) { char c = 'w'; switch (col) { case 1: c = 'y'; break; case 2: c = 'b'; break; case 3: c = 'r'; break; } a += treeX; b += treeX; tree[a] = changeCol(tree[a], c); tree[b] = changeCol(tree[b], c); while (a / 2 != b / 2) { if (a % 2 == 0) tree[a + 1] = changeCol(tree[a + 1], c); if (b % 2 == 1) tree[b - 1] = changeCol(tree[b - 1], c); a /= 2; b /= 2; } } char Query(ll a) { char res = 'w'; a += treeX; while (a != 0) { res = changeCol(res, tree[a]); a /= 2; } return res; } int main() { ios_base::sync_with_stdio(0); ll n, q; cin >> n >> q; treeX = 1; while (treeX < n) treeX *= 2; for (int i = 0; i < MAX; ++i) tree[i] = 'w'; ll a, b, col; for (int i = 0; i < q; ++i) { cin >> a >> b >> col; Ins(a-1, b-1, col); } ll cnt = 0; for (int i = 0; i < n; ++i) { if (Query(i) == 'g') cnt++; } cout << cnt << endl; } |