简述题意:给定 和 次询问,每次给 共四个整数,问 的最大值。
蒟蒻日记:本蒟蒻未能摆脱常规的束缚,绞尽脑汁在 01-Trie 上进位分讨,死翘翘了。
或许可以“始于常规,终于变革”?
如果 ,可使用 01-Trie 求解。这时把 放进 01-Trie 里。本质上是判断是否有 ( 的第 位为 ),或是否有 ( 的第 位为 )。
和 详见这份代码:
inline int query(int x,int L,int v){
int ans=0;
for(int k=23;k>=0;k--){
int c=(v>>k)&1;
if(c&&!(ver[ch[x][0]]>=L)) ans+=(1<<k);
if(!c&&ver[ch[x][1]]>=L) ans+=(1<<k);
if(ver[ch[x][!c]]>=L) x=ch[x][!c];
else x=ch[x][c];
}
return ans^v;
}
// 来自 P4735 最大异或和这题同理,只是 成了 。移项后得:判断是否有 ( 的第 位为 ),或是否有 ( 的第 位为 )。
怎么判断?动态开点权值线段树,启启启启动动动动动!
,可持久化即可。
奉上勇夺最优解第 名的蒟码:
#include<bits/stdc++.h>
using namespace std;
#define FIO
struct IO{
#ifdef FIO
const static int BUFSIZE=1<<20;char buf[BUFSIZE],obuf[BUFSIZE],*p1,*p2,*pp;inline char getchar(){return(p1==p2&&(p2=(p1=buf)+fread(buf,1,BUFSIZE,stdin),p1==p2)?EOF:*p1++);}inline void putchar(char x){((pp-obuf==BUFSIZE&&(fwrite(obuf,1,BUFSIZE,stdout),pp=obuf)),*pp=x,pp++);}inline void flush(){fwrite(obuf,1,pp-obuf,stdout);}IO(){fwrite("Warning: Macro 'FIO' has been defined, keyboard input will be ignored.\n",1,71,stderr);p1=buf,p2=buf,pp=obuf;}~IO(){fwrite(obuf,1,pp-obuf,stdout);}
#else
int(*getchar)()=&::getchar;int(*putchar)(int)=&::putchar;inline void flush(){};
#endif
string sep=" ";int k=2;template<typename Tp,typename enable_if<is_integral<Tp>::value>::type* =nullptr>inline int read(Tp&s){int f=1;char ch=getchar();s=0;while(!isdigit(ch)&&ch!=EOF)f=(ch=='-'?-1:1),ch=getchar();while(isdigit(ch))s=s*10+(ch^48),ch=getchar();s*=f;return ch!=EOF;}template<typename Tp,typename enable_if<is_floating_point<Tp>::value>::type* =nullptr>inline int read(Tp&s){int f=1;char ch=getchar();s=0;while(!isdigit(ch)&&ch!=EOF&&ch!='.')f=(ch=='-'?-1:1),ch=getchar();while(isdigit(ch))s=s*10+(ch^48),ch=getchar();if(ch==EOF)return false;if(ch=='.'){Tp eps=0.1;ch=getchar();while(isdigit(ch))s=s+(ch^48)*eps,ch=getchar(),eps/=10;}s*=f;return ch!=EOF;}inline int read(char&c){char ch=getchar();while(isspace(ch)&&ch!=EOF)ch=getchar();if(ch!=EOF)c=ch;return ch!=EOF;}inline int read(char*c){char ch=getchar();while(isspace(ch)&&ch!=EOF)ch=getchar();while(!isspace(ch)&&ch!=EOF)*(c++)=ch,ch=getchar();return ch!=EOF;}inline int read(string&s){s.clear();char ch=getchar();while(isspace(ch)&&ch!=EOF)ch=getchar();while(!isspace(ch)&&ch!=EOF)s+=ch,ch=getchar();return ch!=EOF;}inline int getline(char*c,const char&ed='\n'){char ch=getchar();while(ch!=ed&&ch!=EOF)*(c++)=ch,ch=getchar();return ch!=EOF;}inline int getline(string&s,const char&ed='\n'){s.clear();char ch=getchar();while(ch!=ed&&ch!=EOF)s+=ch,ch=getchar();return ch!=EOF;}template<typename Tp=int>inline Tp read(){Tp x;read(x);return x;}template<typename Tp,typename...Ts>int read(Tp&x,Ts&...val){return read(x)&&read(val...);}template<typename Tp,typename enable_if<is_integral<Tp>::value>::type* =nullptr>IO&write(Tp x){if(x<0)putchar('-'),x=-x;static char sta[20];int top=0;do sta[top++]=x%10+'0',x/=10;while(x);while(top)putchar(sta[--top]);return*this;}inline IO&write(const string&str){for(char ch:str)putchar(ch);return*this;}inline IO&write(const char*str){while(*str!='\0')putchar(*(str++));return*this;}inline IO&write(char*str){return write((const char*)str);}inline IO&write(const char&ch){return putchar(ch),*this;}template<typename Tp,typename enable_if<is_floating_point<Tp>::value>::type* =nullptr>inline IO&write(Tp x){const static long long pow10[]={1,10,100,1000,10000,100000,1000000,10000000,100000000,1000000000,10000000000,100000000000,1000000000000,10000000000000,100000000000000,1000000000000000,10000000000000000,100000000000000000,100000000000000000,100000000000000000};const auto&n=pow10[k];if(x==0){putchar('0'),putchar('.');for(int i=1;i<=k;++i)putchar('0');return*this;}if(x<0)putchar('-'),x=-x;long long d=(long long)(x+1e-12),y=(long long)(x*n+0.5);write(d),putchar('.');static char sta[20];int top=0;for(;top<k;y/=10)sta[top++]=y%10^48;while(top)putchar(sta[--top]);return*this;}template<typename Tp,typename...Ts>inline IO&write(Tp x,Ts...val){write(x);write(sep);write(val...);return*this;}template<typename...Ts>inline IO&writeln(Ts...val){write(val...);putchar('\n');return*this;}inline IO&writeln(void){putchar('\n');return*this;}template<typename Tp>inline IO&writeWith(Tp x,const string&s){write(x),write(s);return*this;}inline IO&setsep(const string&s){return sep=s,*this;}inline IO&setprecision(const int&K){return k=K,*this;}}io;
const int N=200005;
const int M=N*20;
int n,m;
int a[N],mx;
int root[N],idx;
int ls[M],rs[M],sum[M];
inline void modify(int &x,int y,int l,int r,int v){
sum[x=++idx]=sum[y]+1,ls[x]=ls[y],rs[x]=rs[y];
if(l==r) return;
int mid=l+r>>1;
if(v<=mid) modify(ls[x],ls[y],l,mid,v);
else modify(rs[x],rs[y],mid+1,r,v);
}
inline int query(int x,int y,int l,int r,int a,int b){
if(a<=l&&b>=r) return sum[x]-sum[y];
int mid=l+r>>1;
int res=0;
if(a<=mid) res+=query(ls[x],ls[y],l,mid,a,b);
if(b>mid) res+=query(rs[x],rs[y],mid+1,r,a,b);
return res;
}
inline int Ask(int x,int y,int l,int r,int a,int b){
a=max(a,l),b=min(b,r);
if(a<=b) return query(x,y,l,r,a,b);
return 0;
}
int main(){
io.read(n,m);
for(int i=1;i<=n;i++) io.read(a[i]),mx=max(mx,a[i]);
for(int i=1;i<=n;i++) modify(root[i],root[i-1],0,mx,a[i]);
while(m--){
int b,x,l,r;io.read(b,x,l,r);
int ans=0;
for(int i=17;i>=0;i--){
if(((b>>i)&1)&&!Ask(root[r],root[l-1],0,mx,ans-x,ans-x+(1<<i)-1)) ans+=(1<<i);
else if(!((b>>i)&1)&&Ask(root[r],root[l-1],0,mx,ans-x+(1<<i),ans-x+(1<<i+1)-1)) ans+=(1<<i);
}
io.writeln(ans^b);
}
return 0;
}梳理了讨论区给出的一些易错点:
一、
inline int Ask(int x,int y,int l,int r,int a,int b){
a=max(a,l),b=min(b,r);
if(a<=b) return query(x,y,l,r,a,b);
return 0;
}a=max(a,l),b=min(b,r); 避免越界。
if(a<=b) 避免 a>b 导致的死循环。
二、
for(int i=17;i>=0;i--){
if(((b>>i)&1)&&!Ask(root[r],root[l-1],0,mx,ans-x,ans-x+(1<<i)-1)) ans+=(1<<i);
else if(!((b>>i)&1)&&Ask(root[r],root[l-1],0,mx,ans-x+(1<<i),ans-x+(1<<i+1)-1)) ans+=(1<<i);
}for(int i=17;i>=0;i--) 勿写成 for(int i=16;i>=0;i--),。
完结撒花✿✿ヽ(°▽°)ノ✿