# ybt 1107：校门外的树(YBT 1107: trees outside the school gate)-其他

## ybt 1107：校门外的树(YBT 1107: trees outside the school gate)

### 【输入样例】

``````500 3
150 300
100 200
470 471``````

### 【输出样例】

``298``
``````#include<iostream>
using namespace std;
int main()
{
int L,M,i,j;
int section= {0};
int a,b,tree_sum=0,tree_ex,tree_new;
cin>>L>>M;//输入马路长度与区域数目
for (i=0; i<M; i++)
{
cin>>a>>b;//输入起始点与终止点
for(j=a; j<=b; j++) //令范围内的树移走
section[j]=1;
}
for(i=0; i<=L; i++) //统计马路长度内被移走的树的数目
if(section[i]==1)
tree_sum++;
tree_ex=L+1;//原来的树的数目
tree_new=tree_ex-tree_sum;//移走后的树的数目
cout<<tree_new<<endl;
return 0;
}``````
————————

### [Title Description]

There is a row of trees on the road with a length of L outside the gate of a school, and the interval between each two adjacent trees is 1 meter. We can regard the road as a number axis. One end of the road is at the position of number axis 0 and the other end is at the position of L; Every integer point on the number axis, i.e. 0, 1, 2,…, l, has a tree.

Because there are some areas on the road to build subways. These areas are represented by their starting and ending points on the number axis. It is known that the coordinates of the starting point and ending point of any region are integers, and there may be overlapping 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 there are on the road after all these trees are removed.

### [input]

The first line has two integers L (1 ≤ L ≤ 10000) and m (1 ≤ m ≤ 100). L represents the length of the road, M represents the number of areas, and l and m are separated by a space. The next M lines contain two different integers, separated by a space, representing the coordinates of the starting and ending points of an area.

For 20% of the data, there is no overlap between regions; For other data, there is overlap between regions.

### [output]

Includes a row that contains only an integer indicating the number of trees remaining on the road.

### [input example]

``````500 3
150 300
100 200
470 471``````

### [output example]

``298``
``````#include<iostream>
using namespace std;
int main()
{
int L,M,i,j;
int section= {0};
int a,b,tree_sum=0,tree_ex,tree_new;
cin>>L>>M;//输入马路长度与区域数目
for (i=0; i<M; i++)
{
cin>>a>>b;//输入起始点与终止点
for(j=a; j<=b; j++) //令范围内的树移走
section[j]=1;
}
for(i=0; i<=L; i++) //统计马路长度内被移走的树的数目
if(section[i]==1)
tree_sum++;
tree_ex=L+1;//原来的树的数目
tree_new=tree_ex-tree_sum;//移走后的树的数目
cout<<tree_new<<endl;
return 0;
}``````