文章

37

粉丝

68

获赞

6

访问

7.0k

头像
1175 剩下的树
综合
发布于2024年3月10日 10:02
阅读数 183

#include <bits/stdc++.h>
using namespace std;

int main()
{
    int L, M;
    while(cin>>L>>M)
    {
        int l, r, num=0;
        int* Trees = new int[L+1];
        int* d = new int[L+1];
        for(int i=0; i<=L; i++)
        {
            Trees[i] = 1;  //种一棵树
            d[i] = 0;      //表示差分数组
        }

        for(int i=0; i<M; i++)
        {
            cin>>l>>r;
            d[l]--;        //对首尾进行差分操作
            d[r+1]++;
            if(l==0) Trees[0]=0;
        }

        for(int i=0; i<L; i++)
        {
            Trees[i+1]=d[i+1]+Trees[i];
            if(Trees[i]<1) num++;   //统计被砍掉的位置
        }
        if(Trees[L]<1) num++;       //因为上面的for循环中num无法统计Trees[L]处的情况
        cout<<L+1-num<<endl;
    }
}

 

登录查看完整内容


登录后发布评论

暂无评论,来抢沙发