#include <iostream> #include <stdlib.h> #include <stdio.h> #include <vector> #include <algorithm> using namespace std; struct point{ int x; int y; }; vector <point> y ; vector <point> b ; vector <point> r ; int tab[1000005]; bool cmp(point a, point b){ if( a.x>b.x){ return true; } if(a.x==b.x && a.y>b.y){ return true; } return false; } int main() { int n,m; scanf("%d %d", &n, &m); int l,ri,k; for(int i=0;i<m;i++){ scanf("%d %d %d", &l, &ri, &k); point a; a.x = l; a.y = ri; if( k == 1){ y.push_back(a); } if( k == 2){ b.push_back(a); } if( k == 3){ r.push_back(a); } } //cout<<"----"<<endl; sort(y.begin(), y.end(), cmp); sort(b.begin(), b.end(), cmp); sort(r.begin(), r.end(), cmp); //point y = NULL; int maxi =-1; while(!y.empty()){ point c = y.back(); y.pop_back(); if( maxi>c.x){ c.x = maxi; } for(int i =c.x; i<=c.y;i++){ tab[i]= 1; } if(c.y >maxi){ maxi = c.y; } } maxi =-1; while(!b.empty()){ point c = b.back(); b.pop_back(); if( maxi>c.x){ c.x = maxi; } for(int i =c.x; i<=c.y;i++){ tab[i]|= 1<<1; } if(c.y >maxi){ maxi = c.y; } } maxi =-1; while(!r.empty()){ point c = r.back(); r.pop_back(); if( maxi>c.x){ c.x = maxi; } for(int i =c.x; i<=c.y;i++){ tab[i]|= 1<<2; } if(c.y >maxi){ maxi = c.y; } } int result = 0; for(int i =0; i<n;i++){ if( tab[i]==3){ result++; } } 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 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 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 | #include <iostream> #include <stdlib.h> #include <stdio.h> #include <vector> #include <algorithm> using namespace std; struct point{ int x; int y; }; vector <point> y ; vector <point> b ; vector <point> r ; int tab[1000005]; bool cmp(point a, point b){ if( a.x>b.x){ return true; } if(a.x==b.x && a.y>b.y){ return true; } return false; } int main() { int n,m; scanf("%d %d", &n, &m); int l,ri,k; for(int i=0;i<m;i++){ scanf("%d %d %d", &l, &ri, &k); point a; a.x = l; a.y = ri; if( k == 1){ y.push_back(a); } if( k == 2){ b.push_back(a); } if( k == 3){ r.push_back(a); } } //cout<<"----"<<endl; sort(y.begin(), y.end(), cmp); sort(b.begin(), b.end(), cmp); sort(r.begin(), r.end(), cmp); //point y = NULL; int maxi =-1; while(!y.empty()){ point c = y.back(); y.pop_back(); if( maxi>c.x){ c.x = maxi; } for(int i =c.x; i<=c.y;i++){ tab[i]= 1; } if(c.y >maxi){ maxi = c.y; } } maxi =-1; while(!b.empty()){ point c = b.back(); b.pop_back(); if( maxi>c.x){ c.x = maxi; } for(int i =c.x; i<=c.y;i++){ tab[i]|= 1<<1; } if(c.y >maxi){ maxi = c.y; } } maxi =-1; while(!r.empty()){ point c = r.back(); r.pop_back(); if( maxi>c.x){ c.x = maxi; } for(int i =c.x; i<=c.y;i++){ tab[i]|= 1<<2; } if(c.y >maxi){ maxi = c.y; } } int result = 0; for(int i =0; i<n;i++){ if( tab[i]==3){ result++; } } printf("%d\n",result); return 0; } |