2019 计蒜之道 第五场 A(set的应用)

时间:2019-06-12
本文章向大家介绍2019 计蒜之道 第五场 A(set的应用),主要包括2019 计蒜之道 第五场 A(set的应用)使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

题目:https://nanti.jisuanke.com/t/39451

用set可以很好地解决这道题

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
set<LL>se;
LL quick(LL a,LL b)
{
    LL res=1;
    while(b)
    {
        if(b&1)res=res*a;
        b>>=1;
        a=a*a;
    }
    return res;
}
int main()
{
    LL m,k,q;
    cin>>m>>k>>q;
    LL a,b;
    if(m==1)
    {
        while(q--)
        {
            scanf("%lld%lld",&a,&b);
            if(a==1)se.insert(b);
            else se.erase(se.find(b));
            if(se.empty())printf("%lld\n",k);
            else printf("%lld\n",*se.begin());
        }
    }
    else
    {
        LL sum=quick(2,k)-1;
        while(q--)
        {
            LL ans=sum;
            scanf("%lld%lld",&a,&b);
            if(a==1)se.insert(b);
            else se.erase(se.find(b));
            set<LL>::iterator it;
            for(it=se.begin();it!=se.end();it++)
            {
                int flag=0;
                LL temp=*it;
                if(temp==0)
                {
                    ans=0;break;
                }
                while(temp)//查看其祖先是否已删除,即是否在set里面
                {
                    if(se.find(temp)!=se.end()&&se.find(temp)!=it)
                    {
                        flag=1;break;
                    }
                    temp=(temp-1)/2;//(temp+1)/2-1
                }
                if(!flag)ans-=(quick(2,k-(int)log2(*it+1))-1);
            }
            printf("%lld\n",ans);
        }
    }
    return 0;
}

原文地址:https://www.cnblogs.com/HooYing/p/11010167.html