Calculate how many trees there are on the road

Description questions
A school outside the gate length L of the road with a row of trees, 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 Format
The first line has two integers L (1 <= L <= 10000) and M (1 <= M <= 100), separated by a space between the number L represents the length of the road, M for the region, L and M open. Next M lines contains two different integers, separated by a space, coordinates indicating the starting and ending points of a region.
Output Format
It comprises a line, which contains only an integer representing the number of remaining tree on the road.
Sample input
500 3
150 300
100 200
470 471
Sample Output
298
#include <stdio.h>
int main()
{
    int l,m,a[100001],x,y,i,sum,j;
    while(scanf("%d%d",&l,&m)!=EOF){
        sum=0;
        for(i=0;i<=l;i++){
            a[i]=1;
        }
        for(i=0;i<m;i++){
            scanf("%d%d",&x,&y);
            for(j=x;j<=y;j++){
                a[j]=0;
            }
        }
        for(i=0;i<=l;i++){
            sum+=a[i];
        }
        printf("%d\n",sum);
    }
    return 0;
}

Published 32 original articles · won praise 9 · views 70000 +

Guess you like

Origin blog.csdn.net/yi__cao/article/details/78515982