HDU 3966 Aragorn's Story(树链剖分)(线段树区间修改)
2017-01-29 14:34
447 查看
Aragorn's Story
Time Limit: 10000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 10483 Accepted Submission(s): 2757
[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
[align=left]Sample Output[/align]
7
4
8
树链剖分入门
#include <iostream> #include <cstring> #include <cstdio> #include <algorithm> #include <cmath> #include <string> #include <map> #include <stack> #include <queue> #include <vector> #define inf 2e9 #define met(a,b) memset(a,b,sizeof a) typedef long long ll; using namespace std; const int N =2e5+5; const int M = 4e6+5; int n,sum ,m,tot,num,q; int tre[N*2],laz[N*2]; int dep ,siz ,fa ,id ,son ,val ,top ,c ; int head ; struct EDG{ int to,next; }edg ; void add(int u,int v){ edg[tot].to=v;edg[tot].next=head[u];head[u]=tot++; } void init(){ met(head,-1);met(tre,0); met(son,0);met(laz,0); tot=0;num=0; } void dfs1(int u, int f, int d) { dep[u] = d; siz[u] = 1; son[u] = 0; fa[u] = f; for (int i =head[u]; i !=-1; i=edg[i].next) { int ff = edg[i].to; if (ff == f) continue; dfs1(ff, u, d + 1); siz[u] += siz[ff]; if (siz[son[u]] < siz[ff]) son[u] = ff; } } void dfs2(int u, int tp) { top[u] = tp; id[u] = ++num; if (son[u]) dfs2(son[u], tp); for (int i =head[u]; i !=-1; i=edg[i].next) { int ff = edg[i].to; if (ff == fa[u] || ff == son[u]) continue; dfs2(ff, ff); } } void build(int l,int r,int pos){ if(l==r){ tre[pos]=val[l]; return; } int mid=(l+r)>>1; build(l,mid,pos<<1); build(mid+1,r,pos<<1|1); return; } void pushdown(int num) { if(laz[num]!=0) { tre[num*2]+=laz[num]; tre[num*2+1]+=laz[num]; laz[num*2]+=laz[num]; laz[num*2+1]+=laz[num]; laz[num]=0; } } void update(int num,int le,int ri,int x,int y,int p) { if(x<=le&&y>=ri) { tre[num]+=p; laz[num]+=p; return ; } pushdown(num); int mid=(le+ri)/2; if(x<=mid) update(num*2,le,mid,x,y,p); if(y>mid) update(num*2+1,mid+1,ri,x,y,p); } int query(int num,int le,int ri,int x) { if(le==ri) { return tre[num]; } pushdown(num); int mid=(le+ri)/2; if(x<=mid) return query(num*2,le,mid,x); else return query(num*2+1,mid+1,ri,x); } void Youngth(int u,int v,int p){ int tp1=top[u],tp2=top[v]; while(tp1!=tp2){ if(dep[tp1]<dep[tp2]){ swap(tp1,tp2);swap(u,v); } update(1,1,num,id[tp1],id[u],p); u=fa[tp1]; tp1=top[u]; } if(dep[u]>dep[v])swap(u,v); update(1,1,num,id[u],id[v],p); } int main() { int u,v,p; while(~scanf("%d%d%d",&n,&m,&q)) { init(); for(int i=1;i<=n;i++)scanf("%d",&c[i]); while(m--){ scanf("%d%d",&u,&v); add(u,v);add(v,u); } dfs1(1,0,1); dfs2(1,1); for(int i=1;i<=n;i++){ val[id[i]]=c[i]; } build(1,num,1); char str[100]; while(q--){ scanf("%s",str); if(str[0]=='I'){ scanf("%d%d%d",&u,&v,&p); Youngth(u,v,p); } else if(str[0]=='D'){ scanf("%d%d%d",&u,&v,&p); Youngth(u,v,-p); } else { scanf("%d",&u); printf("%d\n",query(1,1,num,id[u])); } } } return 0; }
相关文章推荐
- HDU 3966 Aragorn's Story 树链剖分+BIT区间修改/单点询问
- Hdu 3966 Aragorn's Story (树链剖分 + 线段树区间更新)
- HDU 3966 Aragorn's Story(树链剖分,区间修改,单点查询)
- HDU 3966 Aragorn's Story(树链剖分+线段树区间更新+手动扩大内存)
- HDU-3966 Aragorn's Story (树链剖分 树状数组 区间修改 点查询)(2011 Multi-University Training Contest 13)
- HDU 3966 Aragorn's Story 树链剖分
- 【HDU 3966 】Aragorn's Story 【树链剖分 基于点权,查询单点值,修改路径上的点权】
- HDU 3966 Aragorn's Story (简单树链剖分)
- HDU 3966 Aragorn's Story (树链剖分+树状数组)
- HDU 3966 Aragorn's Story 动态树 树链剖分
- HDU 3966 Aragorn's Story (树链剖分 区间更新,点查询)
- HDU 3966 Aragorn's Story(树链剖分 (基于点权,查询单点值,修改路径的上的点权)模板题)
- hdu 3966 Aragorn's Story 树链剖分 按点
- hdu 3966 Aragorn's Story 树链剖分
- hdu 3966 Aragorn's Story 树链剖分
- HDU 3966 Aragorn's Story (树链剖分 + 线段树)
- HDU 3966 Aragorn's Story 树链剖分+树状数组 或 树链剖分+线段树
- hdu_3966_Aragorn's Story(树链剖分裸题)
- HDU 3966 Aragorn's Story 树链剖分
- hdu 3966 Aragorn's Story 树链剖分