Description
The outside length of a school gate isLThere is a row of trees on the road, the intervals between each of the two adjacent trees are1meters. We can think of the road as a axis, one end of the road on the Axis0at the other end of theLEach integer point on the axis, i.e.0,1,2,. .....,L, all planted with a tree.
There are some areas on the road to be used to build subways. These regions are represented by their starting and ending points on the axis. The coordinates of the starting and ending points of any given area are integers, and there may be overlapping parts between regions. Now the trees in these areas (including the two trees at the end of the area) are removed. Your task is to calculate how many trees there are on the road after the trees have been removed.
Input
The first line of input has two integers:L(1 <= L <= 10000) andM(1 <= M <=),Lrepresenting the length of the road,Mrepresents the number of regions,Land theMseparated by a space. The nextMeach row contains two different integers, separated by a space, representing the coordinates of the starting and ending points of an area.
Output
For each input and output line, the number of remaining trees
Sample Input
500 3
150 300
100 200
470 471
Sample Output
298
Source
Freshman Cup 2013
1#include <iostream>2 using namespacestd;3 4 intMain ()5 {6 intN,m,ans;7 while(cin>>n>>m)8 {9ans=0;Ten int*a=New int[n+1]; One for(intI=0; i<=n;i++) Aa[i]=1; - while(m--) - { the intx, y; -Cin>>x>>y; - for(inti=x;i<=y;i++) -a[i]=0; + } - for(intI=0; i<=n;i++) + if(a[i]==1) Aans++; atcout<<ans<<Endl; - } - return 0; -}
1330 kinds of trees in OJ-da-gong