zoukankan      html  css  js  c++  java
  • P3293 [SCOI2016]美味

    题目

    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
    ",ans^b);
    	}
    	return 0;
    }
    
  • 相关阅读:
    POJ 1015 Jury Compromise【DP】
    POJ 1661 Help Jimmy【DP】
    HDU 1074 Doing Homework【状态压缩DP】
    HDU 1024 Max Sum Plus Plus【DP,最大m子段和】
    占坑补题。。最近占的坑有点多。。。
    Codeforces 659F Polycarp and Hay【BFS】
    Codeforces 659E New Reform【DFS】
    Codeforces 659D Bicycle Race【计算几何】
    廖大python实战项目第四天
    廖大python实战项目第三天
  • 原文地址:https://www.cnblogs.com/y2823774827y/p/10306250.html
Copyright © 2011-2022 走看看