HDU 2966 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.
 
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.
 
 
 
 
 
题意:有n个camp,初始时每个camp的人数给定,有些camp之间有边连接,并且恰好是一棵树。
 
现在支持3种操作:
1.I  a b c :把a到b的路径(包括a,b)经过的每一个camp的人数+c
2.D  a b c :把a到b的路径(包括a,b)经过的每一个camp的人数-c
3.Q  a:查询a这个camp有多少个人。
 
 
 
 
明显,树链剖分+线段树维护
 
这是我做的第一道树链剖分的题目。
 
其实树链剖分就是把树上的节点重新编号为1~n,然后操作就可以用数据结构维护了。
 
has[u]表示树上节点u重新编号后为has[u],类似于哈希hash。
 
遇上一个问题:
 
要操作树上的(a,b)这个路径的所有节点,但是这个路径不等于重新编号后的[has(a),has(b)]
 
而是由几个区间的和。
 
所以我们就是要找到,这条路径在has后是由哪部分区间的和组成的。
 
只要找到这2个点的在树上的最近公共祖先就可以了。
 
怎么找呢?
 
看代码里面的change() 函数。
 
不断边更新,边走完这条链。
 
 
  1 #pragma comment (linker,"/STACK:1024000000,1024000000")
  2 #include<cstdio>
  3 #include<cstring>
  4 #include<algorithm>
  5 
  6 using namespace std;
  7 
  8 const int maxn=50000+5;
  9 
 10 #define ll long long
 11 
 12 struct Edge
 13 {
 14     int to,next;
 15 }edge[maxn<<1];
 16 int head[maxn];
 17 int tot,num;
 18 int n;
 19 
 20 void addedge(int u,int v)
 21 {
 22     edge[tot].to=v;
 23     edge[tot].next=head[u];
 24     head[u]=tot++;
 25 }
 26 
 27 int fa[maxn];
 28 int siz[maxn];
 29 int top[maxn];
 30 int dep[maxn];
 31 int son[maxn];
 32 int val[maxn];
 33 int has[maxn];
 34 int ran[maxn];
 35 
 36 void dfs1(int u,int father,int d)
 37 {
 38     fa[u]=father;
 39     siz[u]=1;
 40     dep[u]=d;
 41     for(int i=head[u];~i;i=edge[i].next)
 42     {
 43         int v=edge[i].to;
 44         if(v==father)
 45             continue;
 46         dfs1(v,u,d+1);
 47         siz[u]+=siz[v];
 48         if(son[u]==-1||siz[v]>siz[son[u]])
 49         {
 50             son[u]=v;
 51         }
 52     }
 53 }
 54 
 55 void dfs2(int u,int tp)
 56 {
 57     top[u]=tp;
 58     has[u]=num;
 59     ran[num]=u;
 60     num++;
 61     if(son[u]==-1)
 62         return ;
 63     dfs2(son[u],tp);
 64 
 65     for(int i=head[u];~i;i=edge[i].next)
 66     {
 67         int v=edge[i].to;
 68         if(v==fa[u]||v==son[u])
 69             continue;
 70         dfs2(v,v);
 71     }
 72 }
 73 
 74 #define lson l,m,rt<<1
 75 #define rson m+1,r,rt<<1|1
 76 
 77 ll sum[maxn<<2];
 78 ll col[maxn<<2];
 79 
 80 void pushup(int rt)
 81 {
 82     sum[rt]=sum[rt<<1]+sum[rt<<1|1];
 83 }
 84 
 85 void pushdown(int l,int r,int rt)
 86 {
 87     if(col[rt])
 88     {
 89         int m=(l+r)>>1;
 90         col[rt<<1]+=col[rt];
 91         col[rt<<1|1]+=col[rt];
 92         sum[rt<<1]+=col[rt]*(m-l+1);
 93         sum[rt<<1|1]+=col[rt]*(r-m);
 94         col[rt]=0;
 95     }
 96 }
 97 
 98 void build(int l,int r,int rt)
 99 {
100     if(l==r)
101     {
102         sum[rt]=(ll)val[ran[l]];
103         return ;
104     }
105 
106     int m=(l+r)>>1;
107     build(lson);
108     build(rson);
109     pushup(rt);
110 }
111 
112 void update(int L,int R,int add,int l,int r,int rt)
113 {
114     if(L<=l&&R>=r)
115     {
116         col[rt]+=(ll)add;
117         sum[rt]+=(ll)add*(r-l+1);
118         return ;
119     }
120 
121     int m=(l+r)>>1;
122 
123     pushdown(l,r,rt);
124 
125     if(L<=m)
126         update(L,R,add,lson);
127     if(R>m)
128         update(L,R,add,rson);
129     pushup(rt);
130 }
131 
132 ll query(int p,int l,int r,int rt)
133 {
134     if(l==r)
135     {
136         return sum[rt];
137     }
138 
139     int m=(l+r)>>1;
140     pushdown(l,r,rt);
141 
142     ll ret=0;
143 
144     if(p<=m)
145         ret=query(p,lson);
146     else
147         ret=query(p,rson);
148 
149     pushup(rt);
150 
151     return ret;
152 }
153 
154 void init()
155 {
156     memset(head,-1,sizeof(head));
157     memset(son,-1,sizeof(son));
158     memset(col,0,sizeof(col));
159     tot=1;
160     num=1;
161 }
162 
163 void change(int u,int v,int w)
164 {
165     while(top[u]!=top[v])
166     {
167         if(dep[top[u]]<dep[top[v]])
168         {
169             swap(u,v);
170         }
171         update(has[top[u]],has[u],w,1,n,1);
172         u=fa[top[u]];
173     }
174     if(dep[u]>dep[v])
175         swap(u,v);
176     update(has[u],has[v],w,1,n,1);
177 }
178 
179 int main()
180 {
181     while(scanf("%d",&n)!=EOF)
182     {
183         init();
184 
185         int m,q;
186         scanf("%d%d",&m,&q);
187 
188         for(int i=1;i<=n;i++)
189             scanf("%d",&val[i]);
190 
191         for(int i=1;i<=m;i++)
192         {
193             int u,v;
194             scanf("%d%d",&u,&v);
195             addedge(u,v);
196             addedge(v,u);
197         }
198 
199         dfs1(1,1,1);
200         dfs2(1,1);
201 
202         build(1,n,1);
203 
204         for(int i=1;i<=q;i++)
205         {
206             char str[5];
207             scanf("%s",&str);
208             if(str[0]=='I'||str[0]=='D')
209             {
210                 int uu,vv,ww;
211                 scanf("%d%d%d",&uu,&vv,&ww);
212 
213                 if(str[0]=='D')
214                     ww=-ww;
215 
216                 change(uu,vv,ww);
217 
218             }
219             else
220             {
221                 int uu;
222                 scanf("%d",&uu);
223                 printf("%lld
",query(has[uu],1,n,1));
224             }
225         }
226 
227     }
228 
229     return 0;
230 }
View Code
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
原文地址:https://www.cnblogs.com/-maybe/p/4492901.html