#include <bits/stdc++.h> using namespace std; #define PB push_back #define SZ(x) int((x).size()) #define REP(i, n) for (int i = 0, _ = (n); i < _; ++i) #define FOR(i, a, b) for (int i = (a), _ = (b); i <= _; ++i) #define FORD(i, a, b) for (int i = (a), _ = (b); i >= _; --i) #ifdef DEBUG #define DEB(x) (cerr << x) #else #define DEB(x) #endif typedef long long ll; typedef vector<int> vi; typedef pair<int, int> pii; const int INF = 1e9 + 9; const int MAX_N = 1e6 + 1; struct Event { int position; int value; int color; bool operator<(const Event &other) const { if (position != other.position) { return position < other.position; } return value > other.value; } }; int counter[4] = {}; vector<Event> events; void inline one() { int n, m; cin >> n >> m; while (m--) { int left, right, color; cin >> left >> right >> color; events.PB((Event){left, +1, color}); events.PB((Event){right, -1, color}); } sort(events.begin(), events.end()); int result = 0; int last_processed = 0; for (auto const &event : events) { DEB("event: pos=" << event.position << " value=" << event.value << " color=" << event.color << "\n"); DEB("before: " << counter[1] << ", " << counter[2] << ", " << counter[3] << "\n"); if (event.value > 0) { if (counter[1] and counter[2] and (not counter[3])) { int len = event.position - last_processed - 1; DEB("result1 += " << len << "\n"); result += len; } counter[event.color] += event.value; last_processed = event.position - 1; } else { if (counter[1] and counter[2] and (not counter[3])) { int len = event.position - last_processed; DEB("result2 += " << len << "\n"); result += len; } counter[event.color] += event.value; last_processed = event.position; } DEB("after: " << counter[1] << ", " << counter[2] << ", " << counter[3] << "\n"); } cout << result << "\n"; } int main() { ios::sync_with_stdio(false); cin.tie(0); // int z; cin >> z; while(z--) one(); }
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 | #include <bits/stdc++.h> using namespace std; #define PB push_back #define SZ(x) int((x).size()) #define REP(i, n) for (int i = 0, _ = (n); i < _; ++i) #define FOR(i, a, b) for (int i = (a), _ = (b); i <= _; ++i) #define FORD(i, a, b) for (int i = (a), _ = (b); i >= _; --i) #ifdef DEBUG #define DEB(x) (cerr << x) #else #define DEB(x) #endif typedef long long ll; typedef vector<int> vi; typedef pair<int, int> pii; const int INF = 1e9 + 9; const int MAX_N = 1e6 + 1; struct Event { int position; int value; int color; bool operator<(const Event &other) const { if (position != other.position) { return position < other.position; } return value > other.value; } }; int counter[4] = {}; vector<Event> events; void inline one() { int n, m; cin >> n >> m; while (m--) { int left, right, color; cin >> left >> right >> color; events.PB((Event){left, +1, color}); events.PB((Event){right, -1, color}); } sort(events.begin(), events.end()); int result = 0; int last_processed = 0; for (auto const &event : events) { DEB("event: pos=" << event.position << " value=" << event.value << " color=" << event.color << "\n"); DEB("before: " << counter[1] << ", " << counter[2] << ", " << counter[3] << "\n"); if (event.value > 0) { if (counter[1] and counter[2] and (not counter[3])) { int len = event.position - last_processed - 1; DEB("result1 += " << len << "\n"); result += len; } counter[event.color] += event.value; last_processed = event.position - 1; } else { if (counter[1] and counter[2] and (not counter[3])) { int len = event.position - last_processed; DEB("result2 += " << len << "\n"); result += len; } counter[event.color] += event.value; last_processed = event.position; } DEB("after: " << counter[1] << ", " << counter[2] << ", " << counter[3] << "\n"); } cout << result << "\n"; } int main() { ios::sync_with_stdio(false); cin.tie(0); // int z; cin >> z; while(z--) one(); } |