您的位置:首页 > 编程语言 > Go语言

HDU 3966 Aragorn's Story

2015-09-28 14:17 597 查看

Aragorn's Story

Time Limit: 3000ms
Memory Limit: 32768KB
This problem will be judged on HDU. Original ID: 3966
64-bit integer IO format: %I64d Java class name: Main

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.

Source

2011 Multi-University Training Contest 13 - Host by HIT

解题:树链剖分

#include <bits/stdc++.h>
using namespace std;
const int maxn = 50010;
struct node{
int lt,rt,val,lazy;
}tree[maxn<<2];
int n,m,p,val[maxn],fa[maxn],top[maxn],de[maxn];
int loc[maxn],siz[maxn],son[maxn],clk;
vector<int>g[maxn];
void FindHeavyEdge(int u,int father,int depth){
fa[u] = father;
de[u] = depth;
son[u] = -1;
siz[u] = 1;
for(int i = g[u].size()-1; i >= 0; --i){
if(g[u][i] == father) continue;
FindHeavyEdge(g[u][i],u,depth + 1);
siz[u] += siz[g[u][i]];
if(son[u] == -1 || siz[g[u][i]] > siz[son[u]])
son[u] = g[u][i];
}
}

void build(int lt,int rt,int v){
tree[v].lt = lt;
tree[v].rt = rt;
tree[v].lazy = tree[v].val = 0;
if(lt == rt) return;
int mid = (lt + rt)>>1;
build(lt,mid,v<<1);
build(mid + 1,rt,v<<1|1);
}
void pushdown(int v){
if(tree[v].lazy){
tree[v<<1].lazy += tree[v].lazy;
tree[v<<1|1].lazy += tree[v].lazy;
tree[v<<1].val += (tree[v<<1].rt - tree[v<<1].lt + 1)*tree[v].lazy;
tree[v<<1|1].val += (tree[v<<1|1].rt - tree[v<<1|1].lt + 1)*tree[v].lazy;
tree[v].lazy = 0;
}
}
void update(int lt,int rt,int val,int v){
if(lt <= tree[v].lt && rt >= tree[v].rt){
tree[v].lazy += val;
tree[v].val += (tree[v].rt - tree[v].lt + 1)*val;
return;
}
pushdown(v);
if(lt <= tree[v<<1].rt) update(lt,rt,val,v<<1);
if(rt >= tree[v<<1|1].lt) update(lt,rt,val,v<<1|1);
}
void modify(int u,int v,int val){
while(top[u] != top[v]){
if(de[top[u]] < de[top[v]]) swap(u,v);
update(loc[top[u]],loc[u],val,1);
u = fa[top[u]];
}
if(de[u] > de[v]) swap(u,v);
update(loc[u],loc[v],val,1);
}
int query(int pos,int v){
if(tree[v].lt == tree[v].rt) return tree[v].val;
pushdown(v);
if(pos <= tree[v<<1].rt) return query(pos,v<<1);
else return query(pos,v<<1|1);
}
void ConnectHeavyEdge(int u,int ancestor){
top[u] = ancestor;
loc[u] = clk++;
if(son[u] != -1) ConnectHeavyEdge(son[u],ancestor);
for(int i = g[u].size()-1; i >= 0; --i){
if(g[u][i] == fa[u] || son[u] == g[u][i]) continue;
ConnectHeavyEdge(g[u][i],g[u][i]);
}
}
int main(){
int u,v,w;
while(~scanf("%d%d%d",&n,&m,&p)){
for(int i = 0; i < maxn; ++i) g[i].clear();
for(int i = 1; i <= n; ++i) scanf("%d",val + i);
for(int i = clk = 0; i < m; ++i){
scanf("%d%d",&u,&v);
g[u].push_back(v);
g[v].push_back(u);
}
FindHeavyEdge(1,0,0);
ConnectHeavyEdge(1,1);
build(0,clk-1,1);
for(int i = 1; i <= n; ++i) update(loc[i],loc[i],val[i],1);
char op[10];
while(p--){
scanf("%s",op);
if(op[0] == 'I'){
scanf("%d%d%d",&u,&v,&w);
modify(u,v,w);
}else if(op[0] == 'D'){
scanf("%d%d%d",&u,&v,&w);
modify(u,v,-w);
}else if(op[0] == 'Q'){
scanf("%d",&u);
printf("%d\n",query(loc[u],1));
}
}
}
return 0;
}


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: