#include <bits/stdc++.h> using namespace std; typedef vector<int> VI; typedef long long LL; #define FOR(x, b, e) for(int x = b; x<= (e); ++x) #define FORD(x, b, e) for(int x = b; x>= (e); --x) #define REP(x, n) for(int x = 0; x<(n); ++x) #define VAR(v, n)typeof(n) v = (n) #define ALL(c) (c).begin(), (c).end() #define SIZE(x) ((int)(x).size()) #define FOREACH(i, c) for(VAR(i, (c).begin()); i != (c).end(); ++i) #define PB pushback #define ST first #define ND second struct node { bool y; bool b; bool r; }; void update(int size, node *TREE, int l, int r, int c) { int v = l + size - 1; int w = r + size - 1; if (c == 1) { TREE[v].y = 1; TREE[w].y = 1; } else if (c == 2) { TREE[v].b = 1; TREE[w].b = 1; } else { TREE[v].r = 1; TREE[w].r = 1; } while (v / 2 != w / 2) { if (c == 1) { if (v % 2 == 0) { TREE[v + 1].y = 1; } v /= 2; if (w % 2 == 1) { TREE[w - 1].y = 1; } w /= 2; } else if (c == 2) { if (v % 2 == 0) { TREE[v + 1].b = 1; } v /= 2; if (w % 2 == 1) { TREE[w - 1].b = 1; } w /= 2; } else { if (v % 2 == 0) { TREE[v + 1].r = 1; } v /= 2; if (w % 2 == 1) { TREE[w - 1].r = 1; } w /= 2; } } return; } bool green(int size, node *TREE, int x) { int u = x + size - 1; bool y = 0, b = 0, r = 1; while (u > 0) { if (TREE[u].y) { y = 1; } if (TREE[u].b) { b = 1; } if (TREE[u].r) { r = 0; } u /= 2; } if (y && b && r) { return true; } else { return false; } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n, m; cin >> n >> m; int size = 1; while (size <= n) { size *= 2; } int l, r, c; node TREE[2*size]; FOR(i, 1, 2 * size - 1) { TREE[i].y = 0; TREE[i].b = 0; TREE[i].r = 0; } FOR(i, 0, m - 1) { cin >> l >> r >> c; update(size, TREE, l, r, c); } int result = 0; FOR(i, 1, n) { if (green(size, TREE, i)) { result++; } } cout << result << "\n"; 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 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 | #include <bits/stdc++.h> using namespace std; typedef vector<int> VI; typedef long long LL; #define FOR(x, b, e) for(int x = b; x<= (e); ++x) #define FORD(x, b, e) for(int x = b; x>= (e); --x) #define REP(x, n) for(int x = 0; x<(n); ++x) #define VAR(v, n)typeof(n) v = (n) #define ALL(c) (c).begin(), (c).end() #define SIZE(x) ((int)(x).size()) #define FOREACH(i, c) for(VAR(i, (c).begin()); i != (c).end(); ++i) #define PB pushback #define ST first #define ND second struct node { bool y; bool b; bool r; }; void update(int size, node *TREE, int l, int r, int c) { int v = l + size - 1; int w = r + size - 1; if (c == 1) { TREE[v].y = 1; TREE[w].y = 1; } else if (c == 2) { TREE[v].b = 1; TREE[w].b = 1; } else { TREE[v].r = 1; TREE[w].r = 1; } while (v / 2 != w / 2) { if (c == 1) { if (v % 2 == 0) { TREE[v + 1].y = 1; } v /= 2; if (w % 2 == 1) { TREE[w - 1].y = 1; } w /= 2; } else if (c == 2) { if (v % 2 == 0) { TREE[v + 1].b = 1; } v /= 2; if (w % 2 == 1) { TREE[w - 1].b = 1; } w /= 2; } else { if (v % 2 == 0) { TREE[v + 1].r = 1; } v /= 2; if (w % 2 == 1) { TREE[w - 1].r = 1; } w /= 2; } } return; } bool green(int size, node *TREE, int x) { int u = x + size - 1; bool y = 0, b = 0, r = 1; while (u > 0) { if (TREE[u].y) { y = 1; } if (TREE[u].b) { b = 1; } if (TREE[u].r) { r = 0; } u /= 2; } if (y && b && r) { return true; } else { return false; } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n, m; cin >> n >> m; int size = 1; while (size <= n) { size *= 2; } int l, r, c; node TREE[2*size]; FOR(i, 1, 2 * size - 1) { TREE[i].y = 0; TREE[i].b = 0; TREE[i].r = 0; } FOR(i, 0, m - 1) { cin >> l >> r >> c; update(size, TREE, l, r, c); } int result = 0; FOR(i, 1, n) { if (green(size, TREE, i)) { result++; } } cout << result << "\n"; return 0; } |