#include <iostream> #include <algorithm> #include <vector> #include <utility> using namespace std; int n, seg; vector <pair<int, int> > yellow; vector <pair<int, int> > blue; vector <pair<int, int> > red; int a, b, c; int tab[1000100]; int main() { cin.tie(0);cout.tie(0); ios_base::sync_with_stdio(0); cin >> n >> seg; for(int i = 0; i < seg; i++){ cin >> a >> b >> c; if(c == 1){ yellow.push_back(make_pair(a,b)); } else if(c == 2){ blue.push_back(make_pair(a,b)); } else{ red.push_back(make_pair(a,b)); } } sort(yellow.begin(), yellow.end()); sort(blue.begin(), blue.end()); sort(red.begin(), red.end()); //yellow int current = 0; int ind = 0; while(current < yellow.size()){ for(int i = max(ind, yellow[current].first); i <= yellow[current].second; i++){ tab[i] |= 1; } ind = max(ind, yellow[current].second); current++; } /* for(int i = 1; i <= n; i++){ cout << tab[i] << " "; } return 0;*/ //blue current = 0; ind = 0; while(current < blue.size()){ for(int i = max(ind, blue[current].first); i <= blue[current].second; i++){ tab[i] |= 2; } ind = max(ind, blue[current].second); current++; } //red current = 0; ind = 0; while(current < red.size()){ for(int i = max(ind, red[current].first); i <= red[current].second; i++){ tab[i] |= 4; } ind = max(ind, red[current].second); current++; } //final check int green = 0; for(int i = 1; i <= n; i++){ if(tab[i] == 3) green++; } cout << green; 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 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 | #include <iostream> #include <algorithm> #include <vector> #include <utility> using namespace std; int n, seg; vector <pair<int, int> > yellow; vector <pair<int, int> > blue; vector <pair<int, int> > red; int a, b, c; int tab[1000100]; int main() { cin.tie(0);cout.tie(0); ios_base::sync_with_stdio(0); cin >> n >> seg; for(int i = 0; i < seg; i++){ cin >> a >> b >> c; if(c == 1){ yellow.push_back(make_pair(a,b)); } else if(c == 2){ blue.push_back(make_pair(a,b)); } else{ red.push_back(make_pair(a,b)); } } sort(yellow.begin(), yellow.end()); sort(blue.begin(), blue.end()); sort(red.begin(), red.end()); //yellow int current = 0; int ind = 0; while(current < yellow.size()){ for(int i = max(ind, yellow[current].first); i <= yellow[current].second; i++){ tab[i] |= 1; } ind = max(ind, yellow[current].second); current++; } /* for(int i = 1; i <= n; i++){ cout << tab[i] << " "; } return 0;*/ //blue current = 0; ind = 0; while(current < blue.size()){ for(int i = max(ind, blue[current].first); i <= blue[current].second; i++){ tab[i] |= 2; } ind = max(ind, blue[current].second); current++; } //red current = 0; ind = 0; while(current < red.size()){ for(int i = max(ind, red[current].first); i <= red[current].second; i++){ tab[i] |= 4; } ind = max(ind, red[current].second); current++; } //final check int green = 0; for(int i = 1; i <= n; i++){ if(tab[i] == 3) green++; } cout << green; return 0; } |