There is a row of trees on the L-length road outside the gate of a school. I don't know what's wrong with my program for beginners.

there is a row of trees on the L-length road outside the gate of a school, and the distance between each two adjacent trees is 1 meter. We can think of the road as a number axis. One end of the road is in the position of 0, and the other end is in the position of L. All have a tree.

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 axis. It is known that the coordinates of the start and end points of any region are integers, and there may be coincident parts between regions. Now 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 have been removed.

enter
the first line has two integers L (1 < = L < = 10000) and M (1 < = M < = 10000), L for the length of the road, M represents the number of areas, L and M are separated by a space. The next M line contains two different integers separated by a space to represent 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. The
output
includes a line that contains only an integer indicating the number of trees remaining on the road.
sample input

500 3
150 300
100 200
470 471

sample output

298

my program is as follows

-sharpinclude <iostream>
using namespace std;

int p[10010]={0};
int main(){
int l,m,sum;
int x,y;
cin>>l>>m;
for(int i=0;i<l+1;iPP)
p[i]=1;
for(int i=0;i<m;iPP)
{
    cin>>x>>y;
    for(int j=x;j<y+1;jPP)
    p[j]=0;
}
for(int i=0;i<l+1;iPP)
{
    if(p[i]==1)
    sumPP;
}
cout<<sum;
return 0;
} 

there are always errors in online submission. Enter the sample is correct

CPP c
Jul.10,2021

-sharpinclude<stdio.h>
-sharpinclude<string.h>
int main()
{
    int m,n,l,k;
    int a[10005];
    while(scanf("%d%d",&m,&n)!=EOF)
    {
        memset(a,0,sizeof(a));
        while(n--)
        {
            scanf("%d%d",&l,&k);
            for(int i=l; i<=k; iPP)
            {
                a[i]=1;
            }
        }
        int count=0;
        for(int j=0; j<=m; jPP)
        {
            if(a[j]==0)
                countPP;
        }
        printf("%d\n",count);
    }
    return 0;
}
Menu