P14188 [ICPC 2024 Hangzhou R] Barkley III
标签:线段树,二分,位运算
Solution
坏了真学到新东西了
注意到 的数据范围,显然不能开 个 SgT 去存每一位的信息。
类似 P5522 考虑压位,令 表示线段树区间内所有数进行与运算的结果, 表示区间所有数中有且仅有一个数在二进制此位为 的集合,可以得到:
还要打个 tag 处理区间修改,下放时直接令 与 tag 进行与运算就行。
有个小 corner case,处理线段树叶子节点时需要对 进行特判,赋值为 即可。
每次询问把线段树上 区间的信息摘下来,找到 中的最高位,在回到 中找到哪个数此位为 ,除去这个数再求出两个区间的 与起来就是最终答案。
现在的问题就是如何在单 时间内找出这个数,肯定要运用 进行线段树上二分,但此题要在 上进行查找,没有合适的端点( 或 )。
但我们发现在线段树上遍历时,总会出现一个时刻满足 且 ,也就是目前遍历的线段树区间的中点在目标区间内。
我们可以在此时判断出接下来往左还是往右查找,然后就能够获得一个合适的端点了,再做普通的线段树上二分即可。
Code
#include<bits/stdc++.h>
#define int long long
#define N 1000005
namespace IO{
template<typename T> inline void write(T x){
if(x<0) putchar('-'),x=-x;
if(x==0){
putchar('0'); return ;
}
if(x>9) write(x/10);
putchar(x%10+'0');
return ;
}
template<typename T> inline void read(T &x){
x=0; int w=1; char ch=getchar();
while(!isdigit(ch)){
if(ch=='-') w=-1; ch=getchar();
}
while(isdigit(ch))
x=(x<<1)+(x<<3)+(ch^48),ch=getchar();
x*=w; return ;
}
}
using namespace IO;
using namespace std;
const int INF=9223372036854775807ll;
struct tree{
int S,T,lz;
}t[N<<3];
int n,m,a[N];
void push_up(int p){
t[p].S=(t[p<<1].S&t[p<<1|1].S);
t[p].T=((t[p<<1].T&t[p<<1|1].S)|(t[p<<1|1].T&t[p<<1].S));
}
void push_down(int p,int l,int r){
int mid=l+r>>1;
t[p<<1].T&=t[p].lz,t[p<<1|1].T&=t[p].lz;
t[p<<1].S&=t[p].lz,t[p<<1|1].S&=t[p].lz;
t[p<<1].lz&=t[p].lz,t[p<<1|1].lz&=t[p].lz;
if(l==mid) t[p<<1].T=(~t[p<<1].S);
if(mid+1==r) t[p<<1|1].T=(~t[p<<1|1].S);
t[p].lz=INF;
}
void build(int p,int l,int r){
t[p].lz=INF;
if(l==r){t[p].S=a[l],t[p].T=(a[l]^INF);return;}
int mid=l+r>>1; build(p<<1,l,mid),build(p<<1|1,mid+1,r);
push_up(p);
}
void modify(int p,int l,int r,int L,int R,int x){
if(L<=l&&r<=R){
t[p].S&=x,t[p].lz&=x,t[p].T&=x;
if(l==r) t[p].T=(t[p].S^INF);
return;
}
int mid=l+r>>1; push_down(p,l,r);
if(L<=mid) modify(p<<1,l,mid,L,R,x);
if(R>mid) modify(p<<1|1,mid+1,r,L,R,x);
push_up(p);
}
void change(int p,int l,int r,int x,int v){
if(l==r){t[p].S=v,t[p].T=(v^INF);return;}
int mid=l+r>>1; push_down(p,l,r);
if(x<=mid) change(p<<1,l,mid,x,v);
else change(p<<1|1,mid+1,r,x,v);
push_up(p);
}
tree query(int p,int l,int r,int L,int R){
if(L>R) return tree{INF,0,0};
if(L<=l&&r<=R) return t[p];
int mid=l+r>>1; tree lt,rt,res; push_down(p,l,r);
if(L<=mid) lt=query(p<<1,l,mid,L,R);
if(R>mid) rt=query(p<<1|1,mid+1,r,L,R);
if(L>mid) return rt; if(R<=mid) return lt;
res.S=(lt.S&rt.S),res.T=((lt.T&rt.S)|(rt.T<.S));
return res;
}
int query2(int p,int l,int r,int L,int R,int x,int op){
if(l==r) return l;
int mid=l+r>>1; push_down(p,l,r);
if(op==1){
if(R<=mid) return query2(p<<1,l,mid,L,R,x,op);
if((t[p<<1].S>>x)&1) return query2(p<<1|1,mid+1,r,L,R,x,op);
return query2(p<<1,l,mid,L,R,x,op);
}if(op==2){
if(mid<L) return query2(p<<1|1,mid+1,r,L,R,x,op);
if((t[p<<1|1].S>>x)&1) return query2(p<<1,l,mid,L,R,x,op);
return query2(p<<1|1,mid+1,r,L,R,x,op);
}
if(R<=mid) return query2(p<<1,l,mid,L,R,x,op);
if(L>mid) return query2(p<<1|1,mid+1,r,L,R,x,op);
if((query(1,1,n,L,mid).S>>x)&1) return query2(p<<1|1,mid+1,r,L,R,x,1);
return query2(p<<1,l,mid,L,R,x,2);
}
signed main(){
read(n),read(m);
for(int i=1;i<=n;i++) read(a[i]);
build(1,1,n); int op,l,r,x;
while(m--){
read(op),read(l),read(r);
if(op==1) read(x),modify(1,1,n,l,r,x);
if(op==2) change(1,1,n,l,r);
if(op==3){
tree tmp=query(1,1,n,l,r); x=-1;
for(int i=62;~i;i--)
if((tmp.T>>i)&1){x=i;break;}
if(x==-1){
write(tmp.S),putchar('\n');
continue;
}
int pos=query2(1,1,n,l,r,x,0);
write(query(1,1,n,l,pos-1).S&query(1,1,n,pos+1,r).S),putchar('\n');
}
}
return 0;
}