The Preliminary Contest for ICPC Asia Shanghai 2019 Light bulbs

https://nanti.jisuanke.com/t/41399

离散差分 

#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;


template<typename T>inline void read(T &x)
{
    x=0;
    static int p;p=1;
    static char c;c=getchar();
    while(!isdigit(c)){if(c=='-')p=-1;c=getchar();}
    while(isdigit(c)) {x=(x<<1)+(x<<3)+(c-48);c=getchar();}
    x*=p;
}

//-----------------------------------------------

const int maxn=1e5+2;

struct node
{
    int pos;
    int f;
}a[maxn];
int cnt;
bool cmp(node x,node y)
{
    if(x.pos==y.pos) return x.f>y.f;
    return x.pos<y.pos;
}
int main()
{
    //FAST_IO;

    //freopen("input.txt","r",stdin);

    int cas=0;
    int T;
    //cin>>T;
    read(T);
    while(T--)
    {
        cnt=0;
        int n,m;
        //cin>>n>>m;
        read(n);
        read(m);

        for(int i=1;i<=m;i++)
        {
            int l,r;
            read(l);
            read(r);
            ++l;
            ++r;
            ++r;

            a[++cnt].pos=l;
            a[cnt].f=1;

            a[++cnt].pos=r;
            a[cnt].f=-1;

        }
//
//      a[++cnt].pos=n;
//        a[cnt].f=-1;
        sort(a+1,a+1+cnt,cmp);

        int ans=0;
        int sum=a[1].f;
        int pre=a[1].pos;
        //cout<<pre<<endl;
        for(int i=2;i<=cnt;i++)
        {
            if(sum%2==1)
            {
                //cout<<a[i].pos<<endl;
                ans+=a[i].pos-pre;
            }
            sum+=a[i].f;
            pre=a[i].pos;
        }





        //cout<<"Case #"<<(cas++)<<": ";
        //cout<<ans<<endl;
        printf("Case #%d: %d\n",++cas,ans);
    }

    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值