P10107 [GDKOI2023 提高组] 树

题意

给定一个大小为 n 的树,每个点有点权 vi

q 次查询,参数 x,k,查询

usubtree(x),dist(u,x)kvudist(u,x)

n,q106

分析

考虑链怎么做。异或问题考虑拆位,考虑每一位对答案的贡献,故设 fi,j 表示 [i,i+2j) 内的答案,这样异或造成的贡献只有下 j 位。转移方程很显然:fi,j=fi,j1+fi+2j1,j1+k=i+2j1i+2j1[vu,j1=0][vu,j1=1]。合并也比较同理,时间复杂度 O(nlogn)

现在考虑树怎么做。思路同理,fi,j 表示 i 子树内距离 i 小于 2j 的答案。同理有 fi,j=fi,j1+dist(u,i)=2j1fu,j1+2j1dist(u,i)<2j[vu,j1=0][vu,j1=1]

发现这些东西跟深度有关,考虑长剖,我们需要维护的是单点加和区间查,如果使用 BIT 维护那么时间复杂度 O(nlog2n)。考虑前缀和维护,但是短链向长链合并时要给长链的整个后缀都加一遍,复杂度爆炸,但是发现维护后缀和就不会有这样的问题,所以考虑维护后缀和,这样就能 O(1)O(1) 查了。时间复杂度 O(nlogn),长剖不能用 vector 维护。

点击查看代码
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<cmath>
#include<map>
#include<unordered_map>
#include<vector>
#include<queue>
#include<stack>
#include<bitset>
#include<set>
#include<ctime>
#include<random>
#include<cassert>
#define x1 xx1
#define y1 yy1
#define IOS ios::sync_with_stdio(false)
#define ITIE cin.tie(0);
#define OTIE cout.tie(0);
#define PY puts("Yes")
#define PN puts("No")
#define PW puts("-1")
#define P0 puts("0")
#define P__ puts("")
#define PU puts("--------------------")
#define mp make_pair
#define fi first
#define se second
#define gc getchar
#define pc putchar
#define pb emplace_back
#define un using namespace
#define il inline
#define all(x) x.begin(),x.end()
#define mem(x,y) memset(x,y,sizeof x)
#define popc __builtin_popcountll
#define rep(a,b,c) for(int a=(b);a<=(c);++a)
#define per(a,b,c) for(int a=(b);a>=(c);--a)
#define reprange(a,b,c,d) for(int a=(b);a<=(c);a+=(d))
#define perrange(a,b,c,d) for(int a=(b);a>=(c);a-=(d))
#define graph(i,j,k,l) for(int i=k[j];i;i=l[i].nxt)
#define lowbit(x) ((x)&-(x))
#define lson(x) ((x)<<1)
#define rson(x) ((x)<<1|1)
//#define double long double
//#define int long long
//#define int __int128
using namespace std;
using i64=long long;
using u64=unsigned long long;
using pii=pair<int,int>;
template<typename T1,typename T2>inline void ckmx(T1 &x,T2 y){x=x>y?x:y;}
template<typename T1,typename T2>inline void ckmn(T1 &x,T2 y){x=x<y?x:y;}
inline auto rd(){
	int qwqx=0,qwqf=1;char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-')qwqf=-1;ch=getchar();}
	while(ch>='0'&&ch<='9'){qwqx=(qwqx<<1)+(qwqx<<3)+ch-48;ch=getchar();}return qwqx*qwqf;
}
template<typename T>inline void write(T qwqx,char ch='\n'){
	if(qwqx<0){qwqx=-qwqx;putchar('-');}
	int qwqy=0;char qwqz[40];
	while(qwqx||!qwqy){qwqz[qwqy++]=qwqx%10+48;qwqx/=10;}
	while(qwqy--)putchar(qwqz[qwqy]);if(ch)putchar(ch);
}
bool Mbg;
const int maxn=1e6+5,inf=0x3f3f3f3f;
const int m=19;
const long long llinf=0x3f3f3f3f3f3f3f3f;
int n,Q,a[maxn];
vector<int>G[maxn];
int len[maxn],son[maxn];
void dfs0(int x){
	len[x]=1;
	for(int u:G[x]){
		dfs0(u),ckmx(len[x],len[u]+1);
		if(len[u]>len[son[x]])son[x]=u;
	}
}
//f[i][j] sub(i) f[i][j] g[i][j] sub(i) bit=j 
struct node{
	i64 f[20];
	int g[20];
	node(){rep(i,0,m)f[i]=g[i]=0;}
}qwq[maxn],*d[maxn],*buf=qwq;
vector<pii>q[maxn];
i64 ans[maxn];
void dfs(int x,int rt){
//	write(x,32),write(rt);
	if(x==rt){
		d[x]=buf,buf+=len[x];
	}
	if(son[x])d[son[x]]=d[x]+1,dfs(son[x],rt);
	for(int u:G[x])if(u^son[x]){
		dfs(u,u);
		rep(i,0,len[u]-1)rep(j,0,m)d[x][i+1].f[j]+=d[u][i].f[j],d[x][i+1].g[j]+=d[u][i].g[j];
	}
	if(len[x]>1){
		rep(j,0,m)d[x][0].g[j]=d[x][1].g[j];
	}
	rep(j,0,m){
		if((a[x]>>j)&1)d[x][0].g[j]--;
		else d[x][0].g[j]++;
	}
	d[x][0].f[0]+=a[x];
	rep(j,1,m){
		d[x][0].f[j]=d[x][0].f[j-1];
		if((1<<(j-1))<len[x]){
			d[x][0].f[j]+=d[x][(1<<(j-1))].f[j-1]+(d[x][(1<<(j-1))].g[j-1]
			-((1<<j)>=len[x]?0:d[x][(1<<j)].g[j-1]))*(1ll<<(j-1));
		}
	}
	for(pii _:q[x]){
		int k=min(_.fi,len[x]),id=_.se,p=0;
		per(i,m,0)if((k>>i)&1){
			ans[id]+=d[x][p].f[i]+((p+(1<<i)>=len[x]?0:d[x][p+(1<<i)].g[i])-(k>=len[x]?0:d[x][k].g[i]))*(1ll<<i);
			p+=(1<<i);
		}
	}
}
inline void solve_the_problem(){
	n=rd();
	rep(i,1,n)a[i]=rd();
	rep(i,2,n){
		int fa=rd();
		G[fa].emplace_back(i);
	}
	dfs0(1);
//	rep(i,1,n)write(len[i],32),write(son[i]);
	Q=rd();
	rep(i,1,Q){
		int x=rd(),k=rd()+1;
		q[x].emplace_back(mp(k,i));
	}
	dfs(1,1);
	rep(i,1,Q)write(ans[i]);
}
bool Med;
signed main(){
//	freopen("p10107_1.in","r",stdin);freopen("out.out","w",stdout);
	fprintf(stderr,"%.3lfMB\n",(&Mbg-&Med)/1048576.0);
	int _=1;
	while(_--)solve_the_problem();
}
/*
10
9 3 0 7 4 8 8 7 2 5
1 1 2 2 3 6 6 8 7
1
1 4
*/

作者:dcytrl

出处:https://www.cnblogs.com/dcytrl/p/18715831

版权:本作品采用「署名-非商业性使用-相同方式共享 4.0 国际」许可协议进行许可。

posted @   dcytrl  阅读(9)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现
more_horiz
keyboard_arrow_up dark_mode palette
选择主题
点击右上角即可分享
微信分享提示