您的位置:首页 > 编程语言 > Go语言

HDU-3966-Aragorn's Story(树链剖分)

2017-06-09 23:24 393 查看
题目链接:HDU-3966-Aragorn’s Story

题意是给定一棵树。每次修改一条链,使链上所有点加一个值或减一个值。

每次询问询问一个点的权值。

是裸的树链剖分。而且可以用树状数组来维护权值。

#include<bits/stdc++.h>
using namespace std;
const int N=5e4+7;
int a
,son
,fa
,top
,sz
,id
,d
,cnt,b
;
int n,m,q;
int c
;
vector<int> adj
;
void dfs1(int u,int deep,int father)
{
sz[u]=1;
d[u]=deep;
fa[u]=father;
for(int i=0;i<adj[u].size();++i)
{
int v=adj[u][i];
if(v==father) continue;
dfs1(v,deep+1,u);
sz[u]+=sz[v];
if(son[u]==-1||sz[v]>sz[son[u]])
son[u]=v;
}
}
void dfs2(int u)
{
id[u]=++cnt;
if(son[u]!=-1)
{
top[son[u]]=top[u];
dfs2(son[u]);
}
for(int i=0;i<adj[u].size();++i)
{
int v=adj[u][i];
if(v==fa[u]||v==son[u]) continue;
top[v]=v;
dfs2(v);
}
}

int lowbit(int x)
{
return x&-x;
}

void update(int x,int val)
{
for(;x<=n;x+=lowbit(x))
c[x]+=val;
}

int query(int x)
{
int res=0;
for(;x>0;x-=lowbit(x))
res+=c[x];
return res;
}

void init()
{
for(int i=1;i<=n;i++) adj[i].clear();
top[1]=1;
cnt=0;
memset(c,0,sizeof(c));
memset(son,-1,sizeof(son));
}
int main()
{
while(~scanf("%d%d%d",&n,&m,&q))
{
init();
for(int i=1;i<=n;i++) scanf("%d",&a[i]);
for(int i=0;i<m;++i)
{
int u,v;
scanf("%d%d",&u,&v);
adj[u].push_back(v);
adj[v].push_back(u);
}
dfs1(1,0,0);
dfs2(1);
for(int i=1;i<=n;i++)
{
update(id[i],a[i]);
update(id[i]+1,-a[i]);
}
char op[10];
while(q--)
{
scanf("%s",op);
if(op[0]=='Q')
{
int x;
scanf("%d",&x);
printf("%d\n",query(id[x]));
}
else
{
int u,v,x;
scanf("%d%d%d",&u,&v,&x);
if(op[0]=='D') x=-x;
while(top[u]!=top[v])
{
if(d[top[u]]<d[top[v]]) swap(u,v);
update(id[top[u]],x);
update(id[u]+1,-x);
u=top[u];
u=fa[u];
}
if(d[u]>d[v]) swap(u,v);
update(id[u],x);
update(id[v]+1,-x);
}
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: