Hdu 3966 Aragorn's Story 树链剖分+树状数组

来源:互联网 发布:淘宝拒收怎么申请退款 编辑:程序博客网 时间:2024/06/09 18:05

Aragorn's Story

Time Limit: 10000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 12674    Accepted Submission(s): 3395


Problem Description
Our protagonist is the handsome human prince Aragorn comes from The Lord of the Rings. One day Aragorn finds a lot of enemies who want to invade his kingdom. As Aragorn knows, the enemy has N camps out of his kingdom and M edges connect them. It is guaranteed that for any two camps, there is one and only one path connect them. At first Aragorn know the number of enemies in every camp. But the enemy is cunning , they will increase or decrease the number of soldiers in camps. Every time the enemy change the number of soldiers, they will set two camps C1 and C2. Then, for C1, C2 and all camps on the path from C1 to C2, they will increase or decrease K soldiers to these camps. Now Aragorn wants to know the number of soldiers in some particular camps real-time.
 

Input
Multiple test cases, process to the end of input.

For each case, The first line contains three integers N, M, P which means there will be N(1 ≤ N ≤ 50000) camps, M(M = N-1) edges and P(1 ≤ P ≤ 100000) operations. The number of camps starts from 1.

The next line contains N integers A1, A2, ...AN(0 ≤ Ai ≤ 1000), means at first in camp-i has Ai enemies.

The next M lines contains two integers u and v for each, denotes that there is an edge connects camp-u and camp-v.

The next P lines will start with a capital letter 'I', 'D' or 'Q' for each line.

'I', followed by three integers C1, C2 and K( 0≤K≤1000), which means for camp C1, C2 and all camps on the path from C1 to C2, increase K soldiers to these camps.

'D', followed by three integers C1, C2 and K( 0≤K≤1000), which means for camp C1, C2 and all camps on the path from C1 to C2, decrease K soldiers to these camps.

'Q', followed by one integer C, which is a query and means Aragorn wants to know the number of enemies in camp C at that time.
 

Output
For each query, you need to output the actually number of enemies in the specified camp.
 

Sample Input
3 2 51 2 32 12 3I 1 3 5Q 2D 1 2 2Q 1 Q 3
 

Sample Output
748
Hint
1.The number of enemies may be negative.2.Huge input, be careful.
 

Source
2011 Multi-University Training Contest 13 - Host by HIT


有一棵树,每个点有点权。现在让你实现两种操作:

1.修改某两个点之间所有路径点权,使区间增加或减少一个值

2.询问一个点的点权


树链剖分的入门题。

树链剖分的实质:把点权和边权通过dfs的方式,获得编号,再应用合适的数据结构处理。


#include <cstdio>#include <iostream>#include <string.h>#include <string> #include <map>#include <queue>#include <vector>#include <set>#include <algorithm>#include <math.h>#include <cmath>#include <stack>#define mem0(a) memset(a,0,sizeof(a))#define meminf(a) memset(a,0x3f,sizeof(a))using namespace std;typedef long long ll;typedef long double ld;const int maxn=100005,inf=0x3f3f3f3f;  const ll llinf=0x3f3f3f3f3f3f3f3f,mod=1e9+7;   const ld pi=acos(-1.0L); int head[maxn]; int size[maxn],son[maxn],fa[maxn],dep[maxn],top[maxn],dfn[maxn];ll a[maxn];ll f[maxn];bool visit[maxn];int n,m,num;char s[5];struct Edge {int from,to,pre;};Edge edge[maxn*2];void addedge(int from,int to) {edge[num]=(Edge){from,to,head[from]};head[from]=num++;edge[num]=(Edge){to,from,head[to]};head[to]=num++;}int dfs(int now,int step) {visit[now]=1;son[now]=-1;size[now]=1;dep[now]=step;int i;for (i=head[now];i!=-1;i=edge[i].pre) {int to=edge[i].to;if (!visit[to]) {fa[to]=now;size[now]+=dfs(to,step+1);if (son[now]==-1||size[to]>size[son[now]]) son[now]=to;}}return size[now];}void dfs2(int now,int gr) {top[now]=gr;dfn[now]=++num;visit[now]=1;if (son[now]==-1) return;dfs2(son[now],gr);int i;for (i=head[now];i!=-1;i=edge[i].pre) {int to=edge[i].to;if (!visit[to]) dfs2(to,to);}}int lowbit(int x) {return x&(-x);}ll getsum(int k) {ll ans=0;for (int i=k;i>0;i-=lowbit(i)) ans+=f[i];return ans;}void update(int k,ll val) {for (int i=k;i<=n;i+=lowbit(i)) f[i]+=val;}ll modify(int u,int v,ll val) {int x=top[u],y=top[v];while (x!=y) {if (dep[x]<dep[y]) swap(x,y),swap(u,v);update(dfn[x],val);update(dfn[u]+1,-val);u=fa[x];x=top[u];}if (dep[u]<dep[v]) swap(u,v);update(dfn[v],val);update(dfn[u]+1,-val);}int main() {while (scanf("%d%d%d",&n,&m,&m)!=EOF) {int i,x,y;ll d;num=0;mem0(f);memset(head,-1,sizeof(head));for (i=1;i<=n;i++) scanf("%lld",&a[i]);for (i=1;i<n;i++) {scanf("%d%d",&x,&y);addedge(x,y);}mem0(visit);dfs(1,0);num=0;mem0(visit);dfs2(1,1);for (i=1;i<=n;i++) {update(dfn[i],a[i]);update(dfn[i]+1,-a[i]);}for (i=1;i<=m;i++) {scanf("%s",s);if (s[0]=='Q') {scanf("%d",&x);ll ans=getsum(dfn[x]);printf("%lld\n",ans);} else {scanf("%d%d%lld",&x,&y,&d);if (s[0]=='D') d=-d;modify(x,y,d);}}}return 0;}



阅读全文
0 0
原创粉丝点击