oj1045: tree outside the school

Title requires
a school outside the gate length L of a row of trees on roads, the interval between each two adjacent trees are 1 m. We can put the road number as a shaft, one end of the road at a position number 0 of the shaft, the other end position L; each integer number axis point, i.e., 0,1,2, ......, L, a tree species are .

Because there are some areas on the road to be used to build the subway. These areas represent the start and end points with number axis thereof. Coordinate any known starting and ending points of a region are integers, there may be overlap between the partial regions. Now, we these regions tree (including two trees at the end regions) removed. Your task is the calculation of these trees are removed, how many trees there are on the road.
Input
first line of input two integers L (1 <= L <= 10000) and M (1 <= M <= 100), with the road between the number of the representative length L, M for the region, L and M separated by a space. Next M lines contains two different integers, separated by a space, coordinates indicating the starting and ending points of a region.
Output
output includes line, which contains a single integer, the number of remaining tree on the road representation.
The Input the Sample
Raw
500. 3
150 300
100 200 is
470 471
the Sample the Output
Raw
298
recently read code no problem (secretly lazy), put a few relatively simple to understand previous code = v =

#include<stdio.h>
int main(void)
{
	int L, M, i,b, c, j, k, s = 0, a[10000] = { 0 };
	scanf("%d %d", &L, &M);
	for (j = 0; j < M; j++)
	{
		scanf("%d%d", &b, &c);
		for (k = b; k <= c; k++)
			a[k] = 1;
	}
	for (i = 0; i < L + 1; i++)
		if (a[i] == 1)
			s++;
	printf("%d\n", L - s + 1);
	return 0;
}
Published 38 original articles · won praise 27 · views 3171

Guess you like

Origin blog.csdn.net/qq_45891413/article/details/105169831
Recommended