#include <iostream> #include <string> #include <list> #include <algorithm> #include <vector> using namespace std; class Span { public: int beg; int end; Span(int i, int j) : beg(i), end(j) {} }; class Ranges { public: list<Span> spans; void add(int beg, int end) { auto i = spans.begin(); bool extend = false; auto extended = spans.begin(); while (i != spans.end()) { if (extend) { if (end > i->end) { auto j = i; i++; spans.erase(j); continue; } if (end < i->beg - 1) { extended->end = end; return; } extended->end = i->end; spans.erase(i); return; } if (beg > i->end + 1) { i++; continue; } if (end < i->beg - 1) { spans.insert(i, Span(beg, end)); return; } if (beg < i->beg) { i->beg = beg; } if (end <= i->end) { return; } if (end > i->end) { extend = true; extended = i; } else { return; } i++; } if (extend) { extended->end = end; } else { spans.push_back(Span(beg, end)); } } }; int main() { int n, m; std::cin >> n >> m; Ranges colors[3]; vector<bool> v1(n); vector<bool> v2(n); vector<bool> v3(n); vector<vector<bool>> v{ v1, v2, v3 }; int l, r, k; while (m > 0) { std::cin >> l >> r >> k; k--; if (r - l > 706) { colors[k].add(l, r); } else { for (int i = l; i <= r; i++) { v[k][i] = true; } } m--; } for (int c = 0; c < 3; c++) { auto i = colors[c].spans.begin(); while (i != colors[c].spans.end()) { for (int j = i->beg; j <= i->end; j++) { v[c][j] = true; } i++; } } for (int i = 0; i < n; i++) { v[0][i] = v[0][i] && v[1][i]; } int green = 0; for (int i = 0; i < n; i++) { if (v[0][i] && !v[2][i]) { green++; } } cout << green; 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 138 139 140 141 142 143 144 145 146 | #include <iostream> #include <string> #include <list> #include <algorithm> #include <vector> using namespace std; class Span { public: int beg; int end; Span(int i, int j) : beg(i), end(j) {} }; class Ranges { public: list<Span> spans; void add(int beg, int end) { auto i = spans.begin(); bool extend = false; auto extended = spans.begin(); while (i != spans.end()) { if (extend) { if (end > i->end) { auto j = i; i++; spans.erase(j); continue; } if (end < i->beg - 1) { extended->end = end; return; } extended->end = i->end; spans.erase(i); return; } if (beg > i->end + 1) { i++; continue; } if (end < i->beg - 1) { spans.insert(i, Span(beg, end)); return; } if (beg < i->beg) { i->beg = beg; } if (end <= i->end) { return; } if (end > i->end) { extend = true; extended = i; } else { return; } i++; } if (extend) { extended->end = end; } else { spans.push_back(Span(beg, end)); } } }; int main() { int n, m; std::cin >> n >> m; Ranges colors[3]; vector<bool> v1(n); vector<bool> v2(n); vector<bool> v3(n); vector<vector<bool>> v{ v1, v2, v3 }; int l, r, k; while (m > 0) { std::cin >> l >> r >> k; k--; if (r - l > 706) { colors[k].add(l, r); } else { for (int i = l; i <= r; i++) { v[k][i] = true; } } m--; } for (int c = 0; c < 3; c++) { auto i = colors[c].spans.begin(); while (i != colors[c].spans.end()) { for (int j = i->beg; j <= i->end; j++) { v[c][j] = true; } i++; } } for (int i = 0; i < n; i++) { v[0][i] = v[0][i] && v[1][i]; } int green = 0; for (int i = 0; i < n; i++) { if (v[0][i] && !v[2][i]) { green++; } } cout << green; return 0; } |