码迷,mamicode.com
首页 > 其他好文 > 详细

P3293 [SCOI2016]美味

时间:2019-01-22 23:02:28      阅读:148      评论:0      收藏:0      [点我收藏+]

标签:初始化   getch   using   tps   ref   als   void   遍历   include   

题目

P3293 [SCOI2016]美味

做法

\(l\)~\(r\)中的\(a_i\)使得\((a_i+x)\)^\(b\)的最大值

显然当\((b>>i)\&1\),我们尽量使得这位\(=((b>>i)\&1)\)^\(1\)

初始化\(ans=0\),然后倒序遍历位数,\(ans\)是实时更新的,为还确定这位前的最大值

\(tmp(ans+(((1\&(b>>i))\)^\(1)<<i));\)其中\(tmp\)是我们想要得到的(这位及更高位)值,所以查询\((tmp-x,tmp+(1<<i)-1-x)\)中(满足\(tmp\))是否存在值

My complete code

#include<cstdio>
#include<iostream>
using namespace std;
typedef int LL;
const LL maxn=4000000,inf=0x3f3f3f3f;
inline LL Read(){
    LL x(0),f(1);char c=getchar();
    while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
    while(c>='0'&&c<='9')x=(x<<3)+(x<<1)+c-'0',c=getchar();
    return x*f;
}
LL n,nod,Mi=inf,Mx=-inf,m;
LL a[maxn],val[maxn],son[maxn][2],root[maxn];
inline void Update(LL &now,LL pre,LL l,LL r,LL c){
    now=++nod;
    val[now]=val[pre]+1;
    if(l==r) 
        return;
    LL mid(l+r>>1);
    if(c<=mid){
        Update(son[now][0],son[pre][0],l,mid,c);
        son[now][1]=son[pre][1];
    }else{
        Update(son[now][1],son[pre][1],mid+1,r,c);
        son[now][0]=son[pre][0];
    }
}
LL Query(LL pre,LL now,LL l,LL r,LL lt,LL rt){
    if(lt<=l&&rt>=r)
        return val[now]-val[pre];
    LL ret(0),mid(l+r>>1);
    if(lt<=mid)
        ret=Query(son[pre][0],son[now][0],l,mid,lt,rt);
    if(rt>mid)
        ret+=Query(son[pre][1],son[now][1],mid+1,r,lt,rt);
    return ret;
}
inline bool Check(LL l,LL r,LL ln,LL rn){
    ln=max(ln,Mi),rn=min(rn,Mx);
    if(rn<ln) return false;
    return Query(root[l-1],root[r],Mi,Mx,ln,rn);
}
int main(){
    n=Read(),m=Read();
    for(LL i=1;i<=n;++i){
        a[i]=Read();
        Mi=min(Mi,a[i]),Mx=max(Mx,a[i]);
    }
    for(LL i=1;i<=n;++i)
        Update(root[i],root[i-1],Mi,Mx,a[i]);
    while(m--){
        LL ans(0),b(Read()),x(Read()),l(Read()),r(Read());
        for(LL i=17;i>=0;--i){
            LL tmp(ans+(((1&(b>>i))^1)<<i));
            if(Check(l,r,tmp-x,tmp+(1<<i)-1-x))
                ans=tmp;
            else
                ans+=((b>>i)&1)<<i;
        }
        printf("%d\n",ans^b);
    }
    return 0;
}

P3293 [SCOI2016]美味

标签:初始化   getch   using   tps   ref   als   void   遍历   include   

原文地址:https://www.cnblogs.com/y2823774827y/p/10306250.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!