Aragorn's Story
Time Limit: 10000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1321 Accepted Submission(s): 344
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.
1 2 3
2 1
2 3
I 1 3 5
Q 2
D 1 2 2
Q 1
Q 3
4
8
1.The number of enemies may be negative.
2.Huge input, be careful.
裸的树链剖分的入门题;
我是套的树状数组实现的
/* ***********************************************
Author :kuangbin
Created Time :2013/8/14 23:14:27
File Name :F:\2013ACM练习\专题学习\数链剖分\HDU3966.cpp
************************************************ */
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
const int MAXN = ;
struct Edge
{
int to,next;
}edge[MAXN*];
int head[MAXN],tot;
int top[MAXN];
int fa[MAXN];
int deep[MAXN];
int num[MAXN];
int p[MAXN];
int fp[MAXN];
int son[MAXN];
int pos;
void init()
{
tot = ;
memset(head,-,sizeof(head));
pos = ;//使用树状数组,编号从头1开始
memset(son,-,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] = ;
for(int i = head[u];i != -; i = edge[i].next)
{
int v = edge[i].to;
if(v != pre)
{
dfs1(v,u,d+);
num[u] += num[v];
if(son[u] == - || 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] == -) return;
getpos(son[u],sp);
for(int i = head[u];i != -;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[MAXN];
int n;
int sum(int i)
{
int s = ;
while(i > )
{
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)//u->v的路径上点的值改变val
{
int f1 = top[u], f2 = top[v];
int tmp = ;
while(f1 != f2)
{
if(deep[f1] < deep[f2])
{
swap(f1,f2);
swap(u,v);
}
add(p[f1],val);
add(p[u]+,-val);
u = fa[f1];
f1 = top[u];
}
if(deep[u] > deep[v]) swap(u,v);
add(p[u],val);
add(p[v]+,-val);
}
int a[MAXN];
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
int M,P;
while(scanf("%d%d%d",&n,&M,&P) == )
{
int u,v;
int C1,C2,K;
char op[];
init();
for(int i = ;i <= n;i++)
{
scanf("%d",&a[i]);
}
while(M--)
{
scanf("%d%d",&u,&v);
addedge(u,v);
addedge(v,u);
}
dfs1(,,);
getpos(,);
memset(c,,sizeof(c));
for(int i = ;i <= n;i++)
{
add(p[i],a[i]);
add(p[i]+,-a[i]);
}
while(P--)
{
scanf("%s",op);
if(op[] == 'Q')
{
scanf("%d",&u);
printf("%d\n",sum(p[u]));
}
else
{
scanf("%d%d%d",&C1,&C2,&K);
if(op[] == 'D')
K = -K;
Change(C1,C2,K);
}
}
}
return ;
}