P10107 [GDKOI2023 提高组] 树
题意
给定一个大小为
有
分析
考虑链怎么做。异或问题考虑拆位,考虑每一位对答案的贡献,故设
现在考虑树怎么做。思路同理,
发现这些东西跟深度有关,考虑长剖,我们需要维护的是单点加和区间查,如果使用 BIT 维护那么时间复杂度
点击查看代码
#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
*/
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现