题目标签: 数据结构--树链剖分 基础题
题目描述: 有一棵结点数为N的树,和两种操作:
1. 给出两个结点C1,C2,将其路径上的结点(包含端点)的权值增加k;
2. 查询某个结点的权值;
思路: 典型的树链剖分,然后用线段树维护重链和轻边(本题较水,可以用树状数组实现,但线段树应用范围更广).
感觉树链剖分思想并没有想象的那么难,难的是如何在各种各样的变体里应用自如.
Code:
#include<iostream>
#include<stdio.h>
#include<cmath>
#include<string.h>
using namespace std;
class Node
{//线段树结点
public:
int left,right,add;
Node(){}
};
class Edge
{//边结点
public:
int to,next;
Edge(int a=0,int b=0){
to=a,next=b;
}
};
const int MAXN = 51000;
Node tree[MAXN*4];//线段树
Edge edge[MAXN*2];
int edge_num,posi;//边的下标,辅助标记树的顶点的变量
int n,m,q;
int head[MAXN];//表示顶点i在edge里的第一条边的下标
int son[MAXN];//顶点i的重儿子
int father[MAXN];//顶点i的父节点
int pos[MAXN];//点i在线段树中的位置
int rpos[MAXN];//线段树上位置i代表的结点
int depth[MAXN];//点i的深度
int size[MAXN];//以i为根的子树
int top[MAXN];//当前重链的距离根最近的结点
int arr[MAXN];//结点初始值
void init()
{
edge_num=posi=0;
memset(son,-1,sizeof(int)*(n+10));
memset(head,-1,sizeof(int)*(n+10));
}
void add(int a,int b)
{//添加一条从a到b的边
edge[edge_num]=Edge(b,head[a]);
head[a]=edge_num++;
}
void dfs1(int p,int f,int d)
{//第一遍dfs,求father[],depth[],size[],son[]
//p当前结点,f:p的父节点,d当前深度
father[p]=f;
depth[p]=d;
size[p]=1;
for(int i=head[p];i!=-1;i=edge[i].next)
{//遍历每一条边
int u=edge[i].to;
if(u==f)continue;//不能回到父节点
dfs1(u,p,d+1);
size[p]+=size[u];
if(son[p]==-1||size[son[p]]<size[u])//寻找q的重儿子
son[p]=u;
}
}
void dfs2(int p,int itop)
{//第二遍dfs,求top[],pos[],rpos[]
//p当前结点,itop当前重链上距离根最近的点
top[p]=itop;
pos[p]=posi;
rpos[posi++]=p;
if(son[p]==-1)return;//到达叶子及时返回
dfs2(son[p],itop);//重儿子首先递归,保证重链在线段树中连续
for(int i=head[p];i!=-1;i=edge[i].next)
{
int u=edge[i].to;
if(u==father[p]||u==son[p])continue;//重儿子已经访问过
dfs2(u,u);
}
}
void build(int p,int left,int right)
{//线段树初始化
tree[p].left=left,tree[p].right=right;
tree[p].add=0;
if(left==right)
{
tree[p].add=arr[rpos[left]];
return;
}
int mid=(left+right)/2;
build(p*2,left,mid);
build(p*2+1,mid+1,right);
}
int query(int p,int v)
{//查询线段树位置v处的值
if(tree[p].left==tree[p].right)
return tree[p].add;
int mid=(tree[p].left+tree[p].right)/2;
if(v<=mid)return query(p*2,v)+tree[p].add;
else return query(p*2+1,v)+tree[p].add;
}
void tree_change(int p,int left,int right,int val)
{//线段树left到right每个增加val
if(tree[p].left==left&&tree[p].right==right)
{
tree[p].add+=val;
return;
}
int mid=(tree[p].left+tree[p].right)/2;
if(right<=mid)tree_change(p*2,left,right,val);
else if(left>mid)tree_change(p*2+1,left,right,val);
else tree_change(p*2,left,mid,val),tree_change(p*2+1,mid+1,right,val);
}
void change(int a,int b,int c)
{//顶点a到顶点b的路径上,每个结点权值加c
//私以为这个函数是最需要理解的
int f1=top[a],f2=top[b];
while(f1!=f2)
{
if(depth[f1]<depth[f2])
swap(f1,f2),swap(a,b);
tree_change(1,pos[f1],pos[a],c);
a=father[f1],f1=top[a];
}
if(depth[a]>depth[b])swap(a,b);
tree_change(1,pos[a],pos[b],c);
}
int main()
{
while(~scanf("%d%d%d",&n,&m,&q))
{
init();
for(int i=1;i<=n;i++)
scanf("%d",&arr[i]);
int a,b,c;
for(int i=0;i<m;i++)
{
scanf("%d%d",&a,&b);
add(a,b),add(b,a);
}
dfs1(1,0,0);
dfs2(1,1);
build(1,0,posi-1);
char ch[23];
while(q--)
{
scanf("%s",ch);
if(ch[0]=='Q')
{
scanf("%d",&a);
printf("%d\n",query(1,pos[a]));
}
else
{
scanf("%d %d %d",&a,&b,&c);
if(ch[0]=='I')change(a,b,c);
else change(a,b,-c);
}
}
}
return 0;
}