P2590 树的统计
Posted ukcxrtjr
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了P2590 树的统计相关的知识,希望对你有一定的参考价值。
题面:https://www.luogu.org/problemnew/show/P2590
Code:
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<queue>
#include<cmath>
#include<iostream>
using namespace std;
const int N=100005,INF=1000000000;
int n,m,a[N],x,y,now[N*4],id[N*4],Cnt,topf[N*4],top[N*4];
int head[N*4],Next[N*4],to[N*4],cnt,son[N*4],size[N*4],deep[N*4],father[N*4];
int read()
int X=0,W=1;
char ch=0;
while(ch<'0'||ch>'9')
if(ch=='-')
W=-1;
ch=getchar();
while(ch>='0'&&ch<='9')
X=(X<<3)+(X<<1)+ch-'0';
ch=getchar();
return X*W;
struct node
int w;
int left;
int right;
int v;
int mx;
tree[N*4];
void Spread_Tree(int o)
if(tree[o].v)
tree[o*2].w+=tree[o].v*(tree[o*2].right-tree[o*2].left+1);
tree[o*2+1].w+=tree[o].v*(tree[o*2+1].right-tree[o*2+1].left+1);
tree[o*2].v+=tree[o].v;
tree[o*2+1].v+=tree[o].v;
tree[o*2].mx+=tree[o].v;
tree[o*2+1].mx+=tree[o].v;
tree[o].v=0;
void Build_Tree(int o,int l,int r)
tree[o].left=l;
tree[o].right=r;
if(l==r)
tree[o].w=tree[o].mx=now[tree[o].left];
return;
int mid=(l+r)/2;
Build_Tree(o*2,l,mid);
Build_Tree(o*2+1,mid+1,r);
tree[o].w=tree[o*2].w+tree[o*2+1].w;
tree[o].mx=max(tree[o*2].mx,tree[o*2+1].mx);
int Insert_Tree(int o)
if(tree[o].left==tree[o].right)
return tree[o].w;
tree[o].w=Insert_Tree(o*2)+Insert_Tree(o*2+1);
tree[o].mx=max(Insert_Tree(o*2),Insert_Tree(o*2+1));
return tree[o].w;
int Query_Tree_Sum(int o,int l,int r)
if(tree[o].left>=l&&tree[o].right<=r)
return tree[o].w;
int mid=(tree[o].left+tree[o].right)/2;
Spread_Tree(o);
int ans=0;
if(l<=mid)
ans+=Query_Tree_Sum(o*2,l,r);
if(r>mid)
ans+=Query_Tree_Sum(o*2+1,l,r);
return ans;
int Query_Tree_Max(int o,int l,int r)
if(tree[o].left>=l&&tree[o].right<=r)
return tree[o].mx;
int mid=(tree[o].left+tree[o].right)/2;
Spread_Tree(o);
int ans=-INF;
if(l<=mid)
ans=max(ans,Query_Tree_Max(o*2,l,r));
if(r>mid)
ans=max(ans,Query_Tree_Max(o*2+1,l,r));
return ans;
void Change_Tree(int o,int l,int r,int k)
if(tree[o].right<=r&&tree[o].left>=l)
tree[o].w+=k*(tree[o].right-tree[o].left+1);
tree[o].mx+=k;
tree[o].v+=k;
return;
Spread_Tree(o);
int mid=(tree[o].left+tree[o].right)/2;
if(l<=mid)
Change_Tree(o*2,l,r,k);
if(r>mid)
Change_Tree(o*2+1,l,r,k);
tree[o].w=tree[o*2].w+tree[o*2+1].w;
tree[o].mx=max(tree[o*2].mx,tree[o*2+1].mx);
void Insert(int u,int v)
++cnt;
Next[cnt]=head[u];
head[u]=cnt;
to[cnt]=v;
void dfs_1(int u,int f)
deep[u]=deep[f]+1;
size[u]=1;
father[u]=f;
for(int i=head[u];i;i=Next[i])
int v=to[i];
if(v!=f)
dfs_1(v,u);
size[u]+=size[v];
if(size[v]>size[son[u]])
son[u]=v;
void dfs_2(int u,int topf)
id[u]=++Cnt;
now[Cnt]=a[u];
top[u]=topf;
if(!son[u])
return;
dfs_2(son[u],topf);
for(int i=head[u];i;i=Next[i])
int v=to[i];
if(v==father[u]||v==son[u])
continue;
dfs_2(v,v);
int Query_Interval_Sum(int x,int y)
int ans=0;
while(top[x]!=top[y])
if(deep[top[x]]<deep[top[y]])
swap(x,y);
ans+=Query_Tree_Sum(1,id[top[x]],id[x]);
x=father[top[x]];
if(deep[x]>deep[y])
swap(x,y);
ans+=Query_Tree_Sum(1,id[x],id[y]);
return ans;
int Query_Interval_Max(int x,int y)
int ans=-INF;
while(top[x]!=top[y])
if(deep[top[x]]<deep[top[y]])
swap(x,y);
ans=max(ans,Query_Tree_Max(1,id[top[x]],id[x]));
x=father[top[x]];
if(deep[x]>deep[y])
swap(x,y);
ans=max(ans,Query_Tree_Max(1,id[x],id[y]));
return ans;
void Change_Interval(int x,int y,int k)
while(top[x]!=top[y])
if(deep[top[x]]<deep[top[y]])
swap(x,y);
Change_Tree(1,id[top[x]],id[x],k);
x=father[top[x]];
if(deep[x]>deep[y])
swap(x,y);
Change_Tree(1,id[x],id[y],k);
int Query_Point_Sum(int o)
return Query_Tree_Sum(1,id[o],id[o]);
int Query_Point_Max(int o)
return Query_Tree_Max(1,id[o],id[o]);
void Change_Point(int o,int k)
Change_Tree(1,id[o],id[o],k-Query_Point_Sum(o));
int main()
scanf("%d",&n);
for(int i=1;i<n; i++)
scanf("%d%d",&x,&y);
Insert(x,y);
Insert(y,x);
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
scanf("%d",&m);
dfs_1(1,0);
dfs_2(1,1);
Build_Tree(1,1,n);
for(int i=1;i<=m;i++)
char opt[N];
int p,q,k;
scanf("%s",opt);
if(opt[1]=='H')
p=read();
k=read();
Change_Point(p,k);
else if(opt[1]=='M')
p=read();
q=read();
printf("%d\n",Query_Interval_Max(p,q));
else if(opt[1]=='S')
p=read();
q=read();
printf("%d\n",Query_Interval_Sum(p,q));
return 0;
以上是关于P2590 树的统计的主要内容,如果未能解决你的问题,请参考以下文章