HDU 3966 树链剖分+树状数组 模板
2017-10-10 21:26
316 查看
Aragorn's Story
Time Limit: 10000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 13587 Accepted Submission(s): 3623
[align=left]Problem Description[/align]
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.
[align=left]Input[/align]
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.
[align=left]Output[/align]
For each query, you need to output the actually number of enemies in the specified camp.
[align=left]Sample Input[/align]
3 2 5
1 2 3
2 1
2 3
I 1 3 5
Q 2
D 1 2 2
Q 1
Q 3
基于点权 单点查询 修改路径上的点权 模板
#include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #include<map> using namespace std; #define ll long long #define mod 998244353 const int N=50010; const int INF=0x3f3f3f3f; struct Edge { int to,next; } edge[2*N]; int head ; int top ; int fa ; int deep ; int num ; int p ; int fp ; int son ; int pos; int tot; void init() { tot=0; memset(head,-1,sizeof(head)); pos=1; memset(son,-1,sizeof(son)); } void addedge(int u,int v) { edge[tot].to=v; edge[tot].next=head[u]; head[u]=tot++; } void dfs1(int u,int pre,int d) { deep[u]=d; fa[u]=pre; num[u]=1; for(int i=head[u]; i!=-1; i=edge[i].next) { int v=edge[i].to; if(v!=pre) { dfs1(v,u,d+1); num[u]+=num[v]; if(son[u]==-1||num[v]>num[son[u]]) son[u]=v; } } } void getpos(int u,int sp) { top[u]=sp; p[u]=pos++; fp[p[u]]=u; if(son[u]==-1) return ; getpos(son[u],sp); for(int i=head[u]; i!=-1; i=edge[i].next) { int v=edge[i].to; if(v!=son[u]&&v!=fa[u]) getpos(v,v); } } int lowbit(int x) { return x&(-x); } int c ; int n; int sum(int i) { int s=0; while(i>0) { s+=c[i]; i-=lowbit(i); } return s; } void add(int i,int val) { while(i<=n) { c[i]+=val; i+=lowbit(i); } } void change(int u,int v,int val) { int f1=top[u],f2=top[v]; int tmp=0; while(f1!=f2) { if(deep[f1]<deep[f2]) { swap(f1,f2); swap(u,v); } add(p[f1],val); add(p[u]+1,-val); u=fa[f1]; f1=top[u]; } if(deep[u]>deep[v]) swap(u,v); add(p[u],val); add(p[v]+1,-val); } int a ; int main() { int M,P; while(scanf("%d %d %d",&n,&M,&P)!=EOF) { int u,v; int C1,C2,K; char op[10]; init(); for(int i=1; i<=n; i++) scanf("%d",&a[i]); while(M--) { scanf("%d %d",&u,&v); addedge(u,v); addedge(v,u); } dfs1(1,0,0); getpos(1,1); memset(c,0,sizeof(c)); for(int i=1; i<=n; i++) { add(p[i],a[i]); add(p[i]+1,-a[i]); } while(P--) { scanf("%s",op); if(op[0]=='Q') { scanf("%d",&u); printf("%d\n",sum(p[u])); } else { scanf("%d%d%d",&C1,&C2,&K); if(op[0]=='D') K=-K; change(C1,C2,K); } } } return 0; }
相关文章推荐
- HDU 3966 Aragorn's Story [树链剖分(点权)+树状数组]【数据结构】
- HDU-3966 Aragorn's Story (树链剖分 树状数组 区间修改 点查询)(2011 Multi-University Training Contest 13)
- HDU 3966 Aragorn's Story 树链剖分+树状数组 或 树链剖分+线段树
- 树状数组模板 hdu 2689
- HDU 2642(树状数组模板二维)
- HDU 1541 Stars(树状数组-水题,模板题)
- HDU 3966 Aragorn's Story 树链剖分模板
- HDU 3966 树链剖分模板
- Hdu 3966 Aragorn's Story【树链剖分模板题】模板记录
- HDU 1166 线段树模板&树状数组模板
- 整理的树状数组模板 & 敌兵布阵 HDU - 1166
- HDU 1166 线段树模板&树状数组模板
- hdoj 3966 Aragorn's Story 【树链剖分+线段树||树状数组】
- HDU 1166 敌兵布阵(树状数组模板)
- hdu 2642 二维树状数组 单点更新区间查询 模板水题
- HDU 1166 敌兵布阵 (我的树状数组加线段树点修改模板)
- See you~ (hdu1892 二维树状数组模板)
- HDU 2642 Stars(二维树状数组 模板题)
- HDU 3966 (树链剖分对点权值,模板)
- hdu 3584 二进制0,1反转 三维树状数组 及三维树状数组模板