1330 kinds of trees in OJ-da-gong

Source: Internet
Author: User

Description

The outside length of a school gate isLThere is a row of trees on the road, the intervals between each of the two adjacent trees are1meters. We can think of the road as a axis, one end of the road on the Axis0at the other end of theLEach integer point on the axis, i.e.0,1,2,. .....,L, all planted with a tree.

There are some areas on the road to be used to build subways. These regions are represented by their starting and ending points on the axis. The coordinates of the starting and ending points of any given area are integers, and there may be overlapping parts between regions. Now the trees in these areas (including the two trees at the end of the area) are removed. Your task is to calculate how many trees there are on the road after the trees have been removed.

Input

The first line of input has two integers:L(1 <= L <= 10000) andM(1 <= M <=),Lrepresenting the length of the road,Mrepresents the number of regions,Land theMseparated by a space. The nextMeach row contains two different integers, separated by a space, representing the coordinates of the starting and ending points of an area.

Output

For each input and output line, the number of remaining trees

Sample Input

500 3
150 300
100 200
470 471

Sample Output

298

Source

Freshman Cup 2013
1#include <iostream>2 using namespacestd;3 4 intMain ()5 {6     intN,m,ans;7      while(cin>>n>>m)8     {9ans=0;Ten         int*a=New int[n+1]; One          for(intI=0; i<=n;i++) Aa[i]=1; -          while(m--) -         { the             intx, y; -Cin>>x>>y; -              for(inti=x;i<=y;i++) -a[i]=0; +         } -          for(intI=0; i<=n;i++) +             if(a[i]==1) Aans++; atcout<<ans<<Endl; -      }  -     return 0; -}

1330 kinds of trees in OJ-da-gong

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.