题解:P17636 [ICPC 2019 Yinchuan R] XOR Tree

· · 题解

看到平方直接拆,先将 a_i 二进制分解:

答案相当于枚举每两个数的两位: $$ans_u=\frac 12\sum\limits_{i\in S_u}\sum\limits_{j\in S_u}\sum\limits_{d1}\sum\limits_{d2}2^{d1+d2}[b_{i,d1}\oplus b_{j,d1}=1\land b_{i,d2}\oplus b_{j,d2}=1]$$ 这里的 $\frac 12$ 是因为去掉了每两个数只能贡献一次的限制。$i=j$ 肯定是没有贡献的所以不需要考虑。 考虑固定 $d1,d2$。 对 $d1=d2$ 的情况,令 $c_o$ 为 $S_u$ 中 $d$ 位为 $o$ 的个数,这部分的贡献可以写成 $$2^{2\cdot d1}\cdot c_0\cdot c_1$$ 这里又钦定 $0$ 和 $1$ 之间只能贡献一次,所以把前面的 $\frac 12$ 消掉了。 对 $d1\ne d2$ 的情况,不妨设 $d1<d2$,消掉前面的 $\frac 12$。 令 $c_{o1,o2}$ 为 $S_u$ 中 $d1$ 位为 $o1$,$d2$ 位为 $o2$ 的个数,这部分的贡献可以写成 $$2^{d1+d2+1}\cdot(c_{0,0}\cdot c_{1,1}+c_{0,1}\cdot c_{1,0})$$ 问题转化为对子树内离 $u$ 距离 $\le k$ 的点计数。考虑长剖,维护 $c$ 的后缀和即可。复杂度 $O(n\log^2 V)$。 :::info[代码:] ```cpp #include<bits/stdc++.h> #define il inline #define ui unsigned int #define ll long long #define ull unsigned ll #define lll __int128 #define db double #define ldb long double #define pii pair<int,int> #define vi vector<int> #define vpii vector<pii> #define fir first #define sec second #define gc getchar #define pc putchar #define pb push_back #define lb lower_bound #define ub upper_bound #define pct __builtin_popcount #define mst(a,x) memset(a,x,sizeof a) #define mcp(a,b) memcpy(a,b,sizeof b) using namespace std; const int N=2e5+10,INF=0x3f3f3f3f,MOD=998244353; const ll INFll=0x3f3f3f3f3f3f3f3f; il int rd() {int x=0,f=1; char ch=gc(); while(ch<'0'||ch>'9') {if(ch=='-') f=-1; ch=gc();} while(ch>='0'&&ch<='9') x=x*10+ch-'0',ch=gc(); return x*f;} il ll rdll() {ll x=0; int f=1; char ch=gc(); while(ch<'0'||ch>'9') {if(ch=='-') f=-1; ch=gc();} while(ch>='0'&&ch<='9') x=x*10+ch-'0',ch=gc(); return x*f;} il void wr(int x) {if(x==INT_MIN) return printf("-2147483648"),void(); if(x<0) return pc('-'),wr(-x); if(x<10) return pc(x+'0'),void(); wr(x/10),pc(x%10+'0');} il void wrll(ll x) {if(x==LLONG_MIN) return printf("-9223372036854775808"),void(); if(x<0) return pc('-'),wrll(-x); if(x<10) return pc(x+'0'),void(); wrll(x/10),pc(x%10+'0');} il void wr(int x,const char *s) {wr(x),printf("%s",s);} il void wrll(ll x,const char *s) {wrll(x),printf("%s",s);} il int vmod(int x) {return x>=MOD?x-MOD:x;} il int vadd(int x,int y) {return vmod(x+y);} il int vsub(int x,int y) {return vmod(x-y+MOD);} il int vmul(int x,int y) {return 1ll*x*y%MOD;} il int qpow(int x,int y) {int r=1; for(;y;y>>=1,x=vmul(x,x)) if(y&1) r=vmul(r,x); return r;} il void cadd(int &x,int y) {x=vmod(x+y);} il void csub(int &x,int y) {x=vmod(x-y+MOD);} il void cmul(int &x,int y) {x=vmul(x,y);} il void cmax(int &x,int y) {x<y&&(x=y);} il void cmaxll(ll &x,ll y) {x<y&&(x=y);} il void cmin(int &x,int y) {x>y&&(x=y);} il void cminll(ll &x,ll y) {x>y&&(x=y);} int n,k,id,hd[N],A[N],a[N]; struct EDGE {int to,ne;} e[N]; il void add(int u,int v) {e[++id]={v,hd[u]},hd[u]=id;} int idx,h[N],sn[N],dfn[N]; void dfs1(int u) { for(int i=hd[u],v;i;i=e[i].ne) { dfs1(v=e[i].to); if(h[v]+1>h[u]) h[u]=h[v]+1,sn[u]=v; } } void dfs2(int u) { dfn[u]=++idx; if(sn[u]) dfs2(sn[u]); for(int i=hd[u],v;i;i=e[i].ne) if((v=e[i].to)!=sn[u]) dfs2(v); } int f[N][4]; ull as[N]; void dp1(int u,int d) { if(sn[u]) dp1(sn[u],d),mcp(f[dfn[u]],f[dfn[u]+1]); f[dfn[u]][a[u]]++; for(int i=hd[u],v;i;i=e[i].ne) if((v=e[i].to)!=sn[u]) { dp1(v,d); for(int j=0;j<=h[v];j++) for(int o=0;o<2;o++) f[dfn[u]+j+1][o]+=f[dfn[v]+j][o]; for(int o=0;o<2;o++) f[dfn[u]][o]+=f[dfn[v]][o]; } int c[2]={}; for(int o=0;o<2;o++) { c[o]=f[dfn[u]][o]; if(h[u]>k) c[o]-=f[dfn[u]+k+1][o]; } as[u]+=(ull)c[0]*c[1]<<d*2; } void dp2(int u,int d1,int d2) { if(sn[u]) dp2(sn[u],d1,d2),mcp(f[dfn[u]],f[dfn[u]+1]); f[dfn[u]][a[u]]++; for(int i=hd[u],v;i;i=e[i].ne) if((v=e[i].to)!=sn[u]) { dp2(v,d1,d2); for(int j=0;j<=h[v];j++) for(int o=0;o<4;o++) f[dfn[u]+j+1][o]+=f[dfn[v]+j][o]; for(int o=0;o<4;o++) f[dfn[u]][o]+=f[dfn[v]][o]; } int c[4]={}; for(int o=0;o<4;o++) { c[o]=f[dfn[u]][o]; if(h[u]>k) c[o]-=f[dfn[u]+k+1][o]; } for(int o=0;o<4;o++) as[u]+=(ull)c[o]*c[o^3]<<d1+d2; } void QwQ() { n=rd(),k=rd(); for(int i=1;i<=n;i++) A[i]=rd(); for(int i=2;i<=n;i++) add(rd(),i); dfs1(1),dfs2(1); for(int d=0;d<30;d++) { for(int i=1;i<=n;i++) a[i]=A[i]>>d&1,mst(f[i],0); dp1(1,d); } for(int d1=0;d1<30;d1++) for(int d2=d1+1;d2<30;d2++) { for(int i=1;i<=n;i++) a[i]=((A[i]>>d1&1)<<1)|(A[i]>>d2&1),mst(f[i],0); dp2(1,d1,d2); } for(int i=1;i<=n;i++) printf("%llu\n",as[i]); } signed main() { // freopen("in.in","r",stdin),freopen("out.out","w",stdout); int T=1; while(T--) QwQ(); } ``` :::