there is a row of trees on the L-length road outside the gate of a school, and the distance between each two adjacent trees is 1 meter. We can think of the road as a number axis. One end of the road is in the position of 0, and the other end is in the position of L. All have a tree.
because there are some areas on the road to build the subway. These areas are represented by their starting and ending points on the number axis. It is known that the coordinates of the start and end points of any region are integers, and there may be coincident parts between regions. Now remove the trees in these areas (including the two trees at the end of the area). Your task is to calculate how many trees are left on the road after all these trees have been removed.
enter
the first line has two integers L (1 < = L < = 10000) and M (1 < = M < = 10000), L for the length of the road, M represents the number of areas, L and M are separated by a space. The next M line contains two different integers separated by a space to represent the coordinates of the start and end points of a region.
for 20% of the data, there is no overlap between regions;
for other data, there is overlap between regions. The
output
includes a line that contains only an integer indicating the number of trees remaining on the road.
sample input
500 3
150 300
100 200
470 471
sample output
298
my program is as follows
-sharpinclude <iostream>
using namespace std;
int p[10010]={0};
int main(){
int l,m,sum;
int x,y;
cin>>l>>m;
for(int i=0;i<l+1;iPP)
p[i]=1;
for(int i=0;i<m;iPP)
{
cin>>x>>y;
for(int j=x;j<y+1;jPP)
p[j]=0;
}
for(int i=0;i<l+1;iPP)
{
if(p[i]==1)
sumPP;
}
cout<<sum;
return 0;
}
there are always errors in online submission. Enter the sample is correct