#include <algorithm> #include <iostream> #include <vector> using namespace std; int main() { int n, m; cin >> n >> m; struct action { int time; int color; int direction; inline bool operator <(const action &other) const { return time < other.time; } }; vector<action> v; for (int i = 0; i < m; ++i) { int color; int t1, t2; cin >> t1 >> t2 >> color; v.push_back(action{t1 - 1, color - 1, 1}); v.push_back(action{t2, color - 1, -1}); } sort(v.begin(), v.end()); int v_pos = 0; int paint_count[3]{}; int green = 0; for (int i = 0; i < n; ++i) { while(v[v_pos].time <= i && v_pos < v.size()) { paint_count[v[v_pos].color] += v[v_pos].direction; ++v_pos; } if (paint_count[0] && paint_count[1] && !paint_count[2]) ++green; } cout << green; }
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 | #include <algorithm> #include <iostream> #include <vector> using namespace std; int main() { int n, m; cin >> n >> m; struct action { int time; int color; int direction; inline bool operator <(const action &other) const { return time < other.time; } }; vector<action> v; for (int i = 0; i < m; ++i) { int color; int t1, t2; cin >> t1 >> t2 >> color; v.push_back(action{t1 - 1, color - 1, 1}); v.push_back(action{t2, color - 1, -1}); } sort(v.begin(), v.end()); int v_pos = 0; int paint_count[3]{}; int green = 0; for (int i = 0; i < n; ++i) { while(v[v_pos].time <= i && v_pos < v.size()) { paint_count[v[v_pos].color] += v[v_pos].direction; ++v_pos; } if (paint_count[0] && paint_count[1] && !paint_count[2]) ++green; } cout << green; } |