// mrozik[@]gmail #include <cstdio> #include <algorithm> using namespace std; pair<int, int> T[2000000]; int main() { int n, m; scanf("%d%d", &n, &m); for (int i = 0 ; i < m ; i++) { int l, r, k; scanf("%d%d%d", &l, &r, &k); T[2*i] = make_pair(l, k); T[2*i + 1] = make_pair(r+1, -k); } sort(T, T + 2*m); int result = 0; int f[4] {}; int lastP = 1; for (int ix = 0; ix < 2*m; ix++) { int p = T[ix].first; if (f[1] && f[2] && not f[3]) { result += p - lastP; } if (T[ix].second > 0) { f[T[ix].second]++; } else { f[-T[ix].second]--; } lastP = p; } printf("%d\n", result); 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 | // mrozik[@]gmail #include <cstdio> #include <algorithm> using namespace std; pair<int, int> T[2000000]; int main() { int n, m; scanf("%d%d", &n, &m); for (int i = 0 ; i < m ; i++) { int l, r, k; scanf("%d%d%d", &l, &r, &k); T[2*i] = make_pair(l, k); T[2*i + 1] = make_pair(r+1, -k); } sort(T, T + 2*m); int result = 0; int f[4] {}; int lastP = 1; for (int ix = 0; ix < 2*m; ix++) { int p = T[ix].first; if (f[1] && f[2] && not f[3]) { result += p - lastP; } if (T[ix].second > 0) { f[T[ix].second]++; } else { f[-T[ix].second]--; } lastP = p; } printf("%d\n", result); return 0; } |