传送门:bzoj4811
题解
很容易想到拆位后树剖维护(0/1)经过一段区间操作后的值,然后从高位到低位贪心取,然而复杂度是 O ( n k l o g 2 n ) O(nklog^2n) O(nklog2n)的。
实际上可以把这 k k k位压进一个 u n s i g n e d l o n g l o n g unsigned\ long \ long unsigned long long。
设 f 0 / 1 [ k ] f_{0/1}[k] f0/1[k]分别表示0/1依次经过线段树节点 k k k表示区间 [ l , r ] [l,r] [l,r]的操作后的得到的值,实际上每位都等价于这样一个操作:
f 0 [ k ] = ( f 0 l & f 1 r ) ∣ ( ( f 0 l x o r 1 ) & f 0 r ) f_0[k]=(f_0l\&f_1r)|((f_0l \ xor \ 1)\& f_0r) f0[k]=(f0l&f1r)∣((f0l xor 1)&f0r)
l , r l,r l,r分别表示 k k k的左右儿子节点。 f 1 [ k ] f_1[k] f1[k]同理。
查询时分别合并 x , y x,y x,y到 l c a lca lca的 f 0 / 1 f_{0/1} f0/1,注意 x x x到 l c a lca lca的路径操作恰好是倒序的,所以维护两个 f 0 / 1 f_{0/1} f0/1数组,分别表示正序和倒序操作。
最后还是按位贪心取,这样复杂度降到了 O ( n ( l o g 2 n + k ) ) O(n(log^2n+k)) O(n(log2n+k))
代码
#include<bits/stdc++.h>
#define mid ((l+r)>>1)
#define lc k<<1
#define rc k<<1|1
using namespace std;
typedef unsigned long long ull;
const int N=1e5+10;
int n,m,K;
int son[N],sz[N],top[N],f[N],dep[N],df[N],dfn;
int head[N],to[N<<1],nxt[N<<1],tot;
int opt[N],rv[N];
ull val[N],mx,num,ans,bin[70];
struct P{ull g[4];}t[N<<2],A,B;
inline char gc()
{
static char buf[100005];
static int p1=0,p2=0;
if(p1==p2) p1=0,p2=fread(buf,1,100005,stdin);
if(p1==p2) return EOF;
return buf[p1++];
}
char cp,OS[100];
template<class yyy>
inline void rd(yyy &x)
{
cp=gc();x=0;
for(;!isdigit(cp);cp=gc());
for(;isdigit(cp);cp=gc()) x=(x<<3)+(x<<1)+(cp^48);
}
char wbuf[100005];int p3;
inline void wchar(char x)
{
if(p3==100005) fwrite(wbuf,1,100005,stdout),p3=0;
wbuf[p3++]=x;
}
template<class yyy>
inline void ot(yyy x)
{
int re=0;
for(;(!re)||(x);x/=10) OS[++re]='0'+x%10;
for(;re;--re) wchar(OS[re]);
wchar('\n');
}
inline void lk(int u,int v)
{to[++tot]=v;nxt[tot]=head[u];head[u]=tot;}
void dfs(int x)
{
sz[x]=1;
for(int j,i=head[x];i;i=nxt[i]){
j=to[i];if(j==f[x]) continue;
dep[j]=dep[x]+1;f[j]=x;
dfs(j);sz[x]+=sz[j];
if(sz[son[x]]<sz[j]) son[x]=j;
}
}
void dfss(int x,int tpo)
{
top[x]=tpo;df[x]=++dfn;rv[dfn]=x;
if(!son[x]) return;
dfss(son[x],tpo);
for(int j,i=head[x];i;i=nxt[i]){
j=to[i];if(j==f[x] || j==son[x]) continue;
dfss(j,j);
}
}
inline int LCA(int x,int y)
{
for(;top[x]!=top[y];x=f[top[x]])
if(dep[top[x]]<dep[top[y]]) swap(x,y);
if(dep[x]<dep[y]) swap(x,y);
return y;
}
inline ull mg(ull x,ull y,int op)
{
if(op==1) return (x&y);
if(op==2) return (x|y);
return (x^y);
}
inline P merge(P ls,P rs)
{
P re;
re.g[0]=((ls.g[0]&rs.g[1])|((ls.g[0]^mx)&rs.g[0]));
re.g[1]=((ls.g[1]&rs.g[1])|((ls.g[1]^mx)&rs.g[0]));
re.g[2]=((rs.g[2]&ls.g[3])|((rs.g[2]^mx)&ls.g[2]));
re.g[3]=((rs.g[3]&ls.g[3])|((rs.g[3]^mx)&ls.g[2]));
return re;
}
void build(int k,int l,int r)
{
if(l==r){
l=rv[l];
t[k].g[0]=t[k].g[2]=mg(0,val[l],opt[l]);
t[k].g[1]=t[k].g[3]=mg(mx,val[l],opt[l]);
return;
}
build(lc,l,mid);build(rc,mid+1,r);
t[k]=merge(t[lc],t[rc]);
}
void cg(int k,int l,int r,int pos)
{
if(l==r){
l=rv[l];
t[k].g[0]=t[k].g[2]=mg(0,val[l],opt[l]);
t[k].g[1]=t[k].g[3]=mg(mx,val[l],opt[l]);
return;
}
if(pos<=mid) cg(lc,l,mid,pos);
else cg(rc,mid+1,r,pos);
t[k]=merge(t[lc],t[rc]);
}
inline P ask(int k,int l,int r,int L,int R)
{
if(L<=l && r<=R) return t[k];
if(R<=mid) return ask(lc,l,mid,L,R);
if(L>mid) return ask(rc,mid+1,r,L,R);
return merge(ask(lc,l,mid,L,R),ask(rc,mid+1,r,L,R));
}
inline ull sol(int x,int y,ull z)
{
int i,acs=LCA(x,y),d;num=0,ans=0;
d=(top[x]==top[acs])?acs:top[x];
A=ask(1,1,n,df[d],df[x]);
if(d!=acs){
for(x=f[d];top[x]!=top[acs];x=f[top[x]])
A=merge(ask(1,1,n,df[top[x]],df[x]),A);
A=merge(ask(1,1,n,df[acs],df[x]),A);
}
A.g[0]=A.g[2];A.g[1]=A.g[3];
if(y!=acs){
for(x=y;top[x]!=top[acs] && f[top[x]]!=acs;x=f[top[x]]);
if(top[x]!=top[acs]) acs=top[x];
else acs=son[acs];
d=(top[y]==top[acs])?acs:top[y];
B=ask(1,1,n,df[d],df[y]);
if(d!=acs){
for(y=f[d];top[y]!=top[acs];y=f[top[y]])
B=merge(ask(1,1,n,df[top[y]],df[y]),B);
B=merge(ask(1,1,n,df[acs],df[y]),B);
}
A=merge(A,B);
}
for(i=K-1;~i;--i){
if(num+bin[i]<=z && (((A.g[1]>>i)&1)>((A.g[0]>>i)&1))){
num+=bin[i];ans+=bin[i];
}else if((A.g[0]&bin[i])) ans+=bin[i];
}
return ans;
}
int main(){
int i,j,x,y,q;ull z;
rd(n);rd(m);rd(K);
bin[0]=1;mx=1;
for(i=1;i<K;++i) bin[i]=bin[i-1]<<1,mx+=bin[i];
for(i=1;i<=n;++i){rd(opt[i]);rd(val[i]);}
for(i=1;i<n;++i){
rd(x);rd(y);
lk(x,y);lk(y,x);
}
dep[1]=1;dfs(1);dfss(1,1);
build(1,1,n);
for(;m;--m){
rd(q);rd(x);rd(y);rd(z);
if(q==1) ot(sol(x,y,z));
else{
opt[x]=y;val[x]=z;
cg(1,1,n,df[x]);
}
}
if(p3) fwrite(wbuf,1,p3,stdout);
return 0;
}