[NOIP2005 Popularization Group] Tree outside the school gate

Topic link

Title Description
There is a row of trees on the road with length l outside the gate of a school, and the distance 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 0 on the number line, 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 build the subway. These areas are represented by their starting and ending points on the number line. It is known that the coordinates of the start point and the end point of any region are integers, and there may be overlapping parts between regions. Now we need to 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 are removed.

Input format
There are two integers in the first line, which respectively represent the length of the road l and the number of areas m.

In the next m lines, each line contains two integers u, v, which represent the coordinates of the start and end points of a region.

Output format
Output an integer on a line, which represents the number of trees remaining on the road after these trees are removed.

Input and output sample
input

500 3
150 300
100 200
470 471

Output

298

Code:

#include<iostream>
using namespace std;
int num[12345] = {
    
    0};
int main()
{
    
    
	int L, M, x, y, count = 0;
	cin >> L >> M;
	while(M--)
	{
    
    
		cin >> x >> y;
		for(int i = x; i <= y; i++) num[i] = 1;
	}
	for(int i = 0; i <= L; i++)
        if(num[i] == 0)
            count++;
	cout << count;
	return 0;
}

Guess you like

Origin blog.csdn.net/qq_44826711/article/details/113738488