Description
A famous university will build a subway, so the captain want to remove all the trees. They do have decided though we don't know why they must remove trees to build the subway. There are so many trees in the university, and captain decide to remove all of them. Many people disagree with it, so the captain decide to only remove some trees in some intevalsts to avoid annoying people. But will they really remove trees in a little intevals?
Input
Input with n and m. n is the number of trees, m is the numble of inteval to remove trees. ( 1<n<1000000000; 1 < m < 20000 ). Followed m lines and every line has two integers x,y represent inteval where the trees will be remoded between x and y including the breakpoint.
Output
Output the number of the left trees.
Sample Input
5000 10 841 1845 1155 3359 4122 4300 3552 3646 3820 4981 1950 4381 2659 3936 2230 4954 4038 4459 1230 1632
Sample Output
860
#include<iostream> #include<stdio.h> #include<queue> #include<cmath> #include<algorithm> using namespace std; struct node{ int l,r; friend bool operator<(node m,node n){ if(m.l==n.l) return m.r<n.r; return m.l<n.l; } }a[40005]; int main(){ int n,m; while(scanf("%d%d",&n,&m)!=EOF){ for(int i=0;i<m;i++) scanf("%d%d",&a[i].l,&a[i].r); sort(a,a+m); //for(int i=0;i<m;i++) printf("%d %d\n",a[i].l,a[i].r); int Max=0;n++; for(int i=0;i<m;i++){ if(a[i].l>Max) n-=(a[i].r-a[i].l+1),Max=a[i].r; else if(a[i].l<=Max&&a[i].r>Max) n-=(a[i].r-Max),Max=a[i].r; //printf("%d\n",n); } printf("%d\n",n); } }