HDU Aragorn's Story -树链剖分
Posted 心之所向 素履以往
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HDU Aragorn's Story -树链剖分相关的知识,希望对你有一定的参考价值。
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.
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 5
1 2 3
2 1
2 3
I 1 3 5
Q 2
D 1 2 2
Q 1
Q 3
Sample Output
7
4
8
Hint
1.The number of enemies may be negative.
2.Huge input, be careful.
Source
不说话,先上图
惨痛的教训。。。。
以后再也不输入一个字符了。。
1 #include <cstdio> 2 #include <ctype.h> 3 #include <cstring> 4 5 const int MAXN=50002; 6 7 int n,m,q,inr; 8 9 int son[MAXN],siz[MAXN],fa[MAXN],id[MAXN]; 10 int rank[MAXN],a[MAXN],dep[MAXN],top[MAXN]; 11 12 struct SegmentTree { 13 int l,r; 14 int tag,sum; 15 }; 16 SegmentTree tr[MAXN<<2]; 17 18 struct Edge { 19 int to; 20 int next; 21 }; 22 Edge e[MAXN<<1]; 23 24 int head[MAXN<<1],tot; 25 26 inline void read(int&x) { 27 int f=1;register char c=getchar(); 28 for(x=0;!isdigit(c);c==\'-\'&&(f=-1),c=getchar()); 29 for(;isdigit(c);x=x*10+c-48,c=getchar()); 30 x=x*f; 31 } 32 33 inline void add(int x,int y) { 34 e[++tot].to=y; 35 e[tot].next=head[x]; 36 head[x]=tot; 37 } 38 39 inline void init() { 40 for(int i=0;i<(inr<<2);i++) tr[i].tag=0; 41 memset(head,-1,sizeof head); 42 memset(son,-1,sizeof son); 43 memset(dep,0,sizeof dep); 44 memset(fa,0,sizeof fa); 45 tot=0;inr=0; 46 } 47 48 inline void swap(int&a,int&b) { 49 int t=a;a=b;b=t; 50 } 51 52 void Dfs_1(int now,int f) { 53 dep[now]=dep[f]+1; 54 fa[now]=f; 55 siz[now]=1; 56 for(int i=head[now];i!=-1;i=e[i].next) { 57 int to=e[i].to; 58 if(to==f) continue; 59 Dfs_1(to,now); 60 siz[now]+=siz[to]; 61 if(son[now]==-1||siz[son[now]]<son[to]) son[now]=to; 62 } 63 return; 64 } 65 66 void Dfs_2(int now,int tp) { 67 id[now]=++inr; 68 rank[inr]=now; 69 top[now]=tp; 70 if(son[now]==-1) return; 71 Dfs_2(son[now],tp); 72 for(int i=head[now];i!=-1;i=e[i].next) { 73 int to=e[i].to; 74 if(to==fa[now]||to==son[now]) continue; 75 Dfs_2(to,to); 76 } 77 return; 78 } 79 80 inline void down(int now) { 81 tr[now<<1].tag+=tr[now].tag; 82 tr[now<<1|1].tag+=tr[now].tag; 83 tr[now<<1].sum+=(tr[now<<1].r-tr[now<<1].l+1)*tr[now].tag; 84 tr[now<<1|1].sum+=(tr[now<<1|1].r-tr[now<<1|1].l+1)*tr[now].tag; 85 tr[now].tag=0; 86 } 87 88 void build_tree(int now,int l,int r) { 89 tr[now].l=l;tr[now].r=r; 90 if(l==r) { 91 tr[now].sum=a[rank[l]]; 92 return; 93 } 94 int mid=(l+r)>>1; 95 build_tree(now<<1,l,mid); 96 build_tree(now<<1|1,mid+1,r); 97 tr[now].sum=tr[now<<1].sum+tr[now<<1|1].sum; 98 return; 99 } 100 101 void modify(int now,int l,int r,int v) { 102 if(l<=tr[now].l&&r>=tr[now].r) { 103 tr[now].tag+=v; 104 tr[now].sum+=(tr[now].r-tr[now].l+1)*v; 105 return; 106 } 107 if(tr[now].tag) down(now); 108 int mid=(tr[now].l+tr[now].r)>>1; 109 if(l<=mid) modify(now<<1,l,r,v); 110 if(r>mid) modify(now<<1|1,l,r,v); 111 tr[now].sum=tr[now<<1].sum+tr[now<<1|1].sum; 112 return; 113 } 114 115 int query(int now,int pos) { 116 if(tr[now].l==tr[now].r) return tr[now].sum; 117 if(tr[now].tag) down(now); 118 int mid=(tr[now].l+tr[now].r)>>1; 119 if(pos<=mid) return query(now<<1,pos); 120 else return query(now<<1|1,pos); 121 } 122 123 inline void Pre(int x,int y,int v) { 124 while(top[x]!=top[y]) { 125 if(dep[top[x]]<dep[top[y]]) swap(x,y); 126 modify(1,id[top[x]],id[x],v); 127 x=fa[top[x]]; 128 } 129 if(dep[x]>dep[y]) swap(x,y); 130 modify(1,id[x],id[y],v); 131 } 132 133 int hh() { 134 while(scanf("%d%d%d",&n,&m,&q)!=EOF) { 135 init(); 136 char c[10];int x,y,v; 137 for(int i=1;i<=n;++i) read(a[i]); 138 for(int i=1;i<=m;++i) 139 read(x),read(y),add(x,y),add(y,x); 140 Dfs_1(1,0);Dfs_2(1,1); 141 build_tree(1,1,inr); 142 for(int i=1;i<=q;++i) { 143 scanf("%s",c);read(x); 144 if(c[0]==\'I\') { 145 read(y);read(v); 146 Pre(x,y,v); 147 } 148 else if(c[0]==\'D\') { 149 read(y);read(v); 150 Pre(x,y,-v); 151 } 152 else { 153 int ans=query(1,id[x]); 154 printf("%d\\n",ans); 155 } 156 } 157 } 158 return 0; 159 } 160 161 int sb=hh(); 162 int main() {;}
以上是关于HDU Aragorn's Story -树链剖分的主要内容,如果未能解决你的问题,请参考以下文章
AC日记——Aragorn's Story HDU 3966
HDU-3966 Aragorn's Story(树链剖分+线段树)