#include<iostream> #include<string> #include <algorithm> using namespace std; int colors[2*1000000+10][4]; int points[2*1000000]; int counts[4]; int main(){ int n, m; cin >> n >> m; for(int i = 0; i < m; i++) { int l,r,k; cin >> l >> r >> k; colors[l][k]++; colors[r+1][k]--; points[i*2] = l; points[i*2+1] = r+1; } sort(points, points+m*2); int lastGreenIx = 0; bool isGreen = false; int result = 0; for(int i = 0; i < 2*m; i++){ int k = points[i]; counts[1] += colors[k][1]; counts[2] += colors[k][2]; counts[3] += colors[k][3]; if(!isGreen && counts[1] >= 1 && counts[2] >= 1 && counts[3] < 1) { isGreen = true; lastGreenIx = k; } else if (isGreen && (counts[1] < 1 || counts[2] < 1 || counts[3] >=1) ) { isGreen = false; result += k-lastGreenIx; } } cout << result << 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 | #include<iostream> #include<string> #include <algorithm> using namespace std; int colors[2*1000000+10][4]; int points[2*1000000]; int counts[4]; int main(){ int n, m; cin >> n >> m; for(int i = 0; i < m; i++) { int l,r,k; cin >> l >> r >> k; colors[l][k]++; colors[r+1][k]--; points[i*2] = l; points[i*2+1] = r+1; } sort(points, points+m*2); int lastGreenIx = 0; bool isGreen = false; int result = 0; for(int i = 0; i < 2*m; i++){ int k = points[i]; counts[1] += colors[k][1]; counts[2] += colors[k][2]; counts[3] += colors[k][3]; if(!isGreen && counts[1] >= 1 && counts[2] >= 1 && counts[3] < 1) { isGreen = true; lastGreenIx = k; } else if (isGreen && (counts[1] < 1 || counts[2] < 1 || counts[3] >=1) ) { isGreen = false; result += k-lastGreenIx; } } cout << result << endl; return 0; } |