B(1940): Remove the tree

Description

A famous university will build a subway, so the captain want to remove all the trees. They do have decided though we don't know why they must remove trees to build the subway. There are so many trees in the university, and captain decide to remove all of them. Many people disagree with it, so the captain decide to only remove some trees in some intevalsts to avoid annoying people. But will they really remove trees in a little intevals?

Input

Input with n and m. n is the number of trees, m is the numble of inteval to remove trees. ( 1<n<1000000000; 1 < m < 20000 ). Followed m lines and every line has two integers x,y represent inteval where the trees will be remoded between x and y including the breakpoint.

Output

Output the number of the left trees.

Sample Input

5000 10 
841 1845 
1155 3359 
4122 4300 
3552 3646 
3820 4981 
1950 4381 
2659 3936 
2230 4954 
4038 4459 
1230 1632

Sample Output

860
#include<iostream>
#include<stdio.h>
#include<queue>
#include<cmath>
#include<algorithm>
using namespace std;
struct node{
    int l,r;
    friend bool operator<(node m,node n){
        if(m.l==n.l) return m.r<n.r;
        return m.l<n.l;
    }
}a[40005];
int main(){
    int n,m;
    while(scanf("%d%d",&n,&m)!=EOF){
        for(int i=0;i<m;i++) scanf("%d%d",&a[i].l,&a[i].r);
        sort(a,a+m);
        //for(int i=0;i<m;i++) printf("%d %d\n",a[i].l,a[i].r);
        int Max=0;n++;
        for(int i=0;i<m;i++){
            if(a[i].l>Max) n-=(a[i].r-a[i].l+1),Max=a[i].r;
            else if(a[i].l<=Max&&a[i].r>Max) n-=(a[i].r-Max),Max=a[i].r;
            //printf("%d\n",n);
        }
        printf("%d\n",n);
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值