The tree outside the school gate (NOIP2005 second question of the popularization group)

Source: Internet
Author: User
Tags time limit
A tree outside the school gate


Total time limit: 1000ms memory limit: 65536kB description

There is a row of trees on the road outside the gate of a school, and the spacing between the two adjacent trees is 1 meters. We can see the road as a axis, one end of the road in the 0 axis position, the other end in the position of L; Each integer point on the axis, that is, 0,1,2,......,l, is planted with a tree.

Because there are some areas on the road to build subways. These areas are represented by their starting and ending points on the axis. The coordinates of the starting and ending points of any given region are integers, and there may be overlapping portions between regions. Now you remove the trees in these areas, including the two trees at the end of the area. Your task is to calculate how many trees there are on the road after the trees have been removed. Enter the first line with two integers L (1 <= L <= 10000) and M (1 <= m <=), L represent the length of the road, M represents the number of regions, and L and M are separated by a space. The next line of M lines contains two different integers, separated by a single space, representing the coordinates of the starting and ending points of an area.

For 20% of the data, there is no overlap between the regions;
For other data, there is a overlap between regions. The output includes a row that contains only an integer that represents the number of trees remaining on the road. Sample input

3
470 471
Sample output
298
#include <iostream>  
#include <vector>  //header file #include<vector>.
using namespace std;  A vector is a dynamic array of any type that can be stored to add and compress data.
  
int main ()  
{  
    int L = 0, M = 0;  
    CIN >> L >> M;  
    Vector<int> sample (l+1);  Create a Vector object,vector<int> VEC;
    for (int i = 0; i < l+1 i++)  
    {  
        sample[i] = 1;  
    }  
    for (int i = 0; i < M i++)  
    {  
        int start = 0, end = 0;  
        CIN >> start >> end;  
        for (int j = start; J <= End; J +)  
        {  
            sample[j] = 0;  Mark the tree within the zone as 0
        }  
    int num = 0;  
    for (int i = 0; i < l+1 i++)  
    {  
        num + = sample[i];  The array unit labeled 1 is the remaining tree
    }  
    cout << num << endl;  
    return 0;  
}  

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.