#include <iostream> #include <vector> #include <utility> #include <algorithm> using namespace std; int main() { ios_base::sync_with_stdio(0); int N, M; cin >> N >> M; typedef pair<int, int> Event; vector<Event> starts; vector<Event> ends; starts.reserve(M); ends.reserve(M); int colors[] = {0,0,0}; enum { Y, B, R }; auto isGreen = [&colors]() { return colors[Y] > 0 & colors[B] > 0 && colors[R] == 0; }; for (int i = 0; i < M; i++) { int l, r, k; cin >> l >> r >> k; starts.push_back(Event(l-1, k-1)); ends.push_back(Event(r, k-1)); } sort(starts.begin(), starts.end()); sort(ends.begin(), ends.end()); int s = 0, e = 0; int prev = 0; int res = 0; auto update = [&res, &prev, &isGreen](int val) { if (val != prev) { if (isGreen()) res += val - prev; prev = val; } }; while (s < starts.size() || e < ends.size()) { if (s < starts.size() && starts[s].first <= ends[e].first) { update(starts[s].first); colors[starts[s].second]++; s++; } else { update(ends[e].first); colors[ends[e].second]--; e++; } } cout << res << endl; 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 | #include <iostream> #include <vector> #include <utility> #include <algorithm> using namespace std; int main() { ios_base::sync_with_stdio(0); int N, M; cin >> N >> M; typedef pair<int, int> Event; vector<Event> starts; vector<Event> ends; starts.reserve(M); ends.reserve(M); int colors[] = {0,0,0}; enum { Y, B, R }; auto isGreen = [&colors]() { return colors[Y] > 0 & colors[B] > 0 && colors[R] == 0; }; for (int i = 0; i < M; i++) { int l, r, k; cin >> l >> r >> k; starts.push_back(Event(l-1, k-1)); ends.push_back(Event(r, k-1)); } sort(starts.begin(), starts.end()); sort(ends.begin(), ends.end()); int s = 0, e = 0; int prev = 0; int res = 0; auto update = [&res, &prev, &isGreen](int val) { if (val != prev) { if (isGreen()) res += val - prev; prev = val; } }; while (s < starts.size() || e < ends.size()) { if (s < starts.size() && starts[s].first <= ends[e].first) { update(starts[s].first); colors[starts[s].second]++; s++; } else { update(ends[e].first); colors[ends[e].second]--; e++; } } cout << res << endl; return 0; } |