tree outside school gate

4: The tree outside the school gate


Total time limit: 
1000ms
Memory limit: 
65536kB
describe

There is a row of trees on the road with a length of L outside the gate of a school, and the interval between every two adjacent trees is 1 meter. We can think of the road as a number line, one end of the road is at the position of number 0, and the other end is at the position of L; each integer point on the number line, namely 0, 1, 2, ..., L, has a tree planted .

Because there are some areas on the road to be used to build the subway. These areas are represented by their start and end points on the number line. It is known that the coordinates of the start and end points of any region are integers, and there may be overlapping parts between regions. The trees in these regions (including the two trees at the region's endpoints) are now removed. Your task is to count 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 <= 100). L represents the length of the road, M represents the number of areas, and there is a space between L and M. open. The next M lines each contain two distinct integers, separated by a space, representing 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.
output
Include a line containing only an integer representing the number of trees remaining on the road.
sample input
500 3
150 300
100 200
470 471
Sample output
298





















#include<iostream>
using namespace std;
const int MAX = 10001;
int c[MAX];
int main(){
	int l,n;
	
	int a[MAX],b[MAX];
	
	cin>>l>>n;
	for(int i=0;i<n;i++){
		cin>>a[i]>>b[i];
		for(int j=a[i];j<=b[i];j++)
			c[j]=1;
	}
	int count=0;
	for(int i=0;i<=l;i++)
	{
		if(c[i]==0) count++;
	}
	cout<<count<<endl;
	return 0;
}

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326860832&siteId=291194637