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

hdu 3966 Aragorn's Story 树链剖分 按点

2014-11-04 09:25 337 查看

Aragorn's Story

Time Limit: 10000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3494 Accepted Submission(s):
973


[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
Hint

1.The number of enemies may be negative.

2.Huge input, be careful.

[align=left]Source[/align]
2011
Multi-University Training Contest 13 - Host by HIT

[align=left]Recommend[/align]
We have carefully selected several similar problems for
you: 3964 3965 3962 3963 3967

#pragma comment(linker,"/STACK:100000000,100000000")
#include<iostream>
#include<stdio.h>
#include<cstring>
#include<cstdlib>
using namespace std;
typedef __int64 LL;
const int maxn = 5e4+3;

int pos,cont;
int a[maxn];
int son[maxn];
int head[maxn];
int vis[maxn];
int w[maxn];
int dep[maxn];
int father[maxn];
int hxl[maxn];
int top[maxn];
struct Edge
{
int to;
int next;
}edge[maxn*2];

void init()
{
pos = cont = 0;
memset(son,-1,sizeof(son));
memset(head,-1,sizeof(head));
memset(hxl,0,sizeof(hxl));
}
void addedge(int u,int v)
{
edge[cont].to = v;
edge[cont].next = head[u];
head[u] = cont;
++cont;
}
void dfs1(int u,int fre,int deep)
{
father[u] = fre;
dep[u] = deep;
vis[u] = 1;
for(int i=head[u];i!=-1;i=edge[i].next)
{
int v = edge[i].to;
if(v!=fre)
{
dfs1(v,u,deep+1);
vis[u]=vis[u]+vis[v];
if(son[u] == -1 || vis[v] > vis[son[u]])
son[u] = v;
}
}
}
void dfs2(int u,int t)
{
top[u] = t;
if(son[u]!=-1)
{
w[u]=++pos;
dfs2(son[u],t);
}
else
{
w[u]=++pos;
return;
}
for(int i=head[u];i!=-1;i=edge[i].next)
{
int v = edge[i].to;
if(v!=son[u] && v!=father[u])
dfs2(v,v);
}
}
void add(int x,int n,int num1)
{
for(int i=x;i<=n;i=i+(i&(-i)))
hxl[i] = hxl[i] + num1;
}
LL query(int x)
{
if(x==0)return 0;
LL sum1 = 0;
while(x)
{
sum1=sum1+hxl[x];
x=x-(x&(-x));
}
return sum1;
}
void insert(int u,int v,int num1,int size1)
{
int topu=top[u],topv=top[v];
while(topu!=topv)
{
if(dep[topu]<dep[topv])
{
swap(u,v);
swap(topu,topv);
}
add(w[topu],pos,num1*size1);
add(w[u]+1,pos,-1*num1*size1);
u=father[topu];
topu = top[u];
}
if(dep[u]>dep[v]) swap(u,v);
add(w[u],pos,num1*size1);
add(w[v]+1,pos,-1*num1*size1);
}
int main()
{
int n,m,q,u,v,l,r;
while(scanf("%d%d%d",&n,&m,&q)>0)
{
init();
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
for(int i=1;i<=m;i++)
{
scanf("%d%d",&u,&v);
addedge(u,v);
addedge(v,u);
}
addedge(0,1);
addedge(1,0);
dfs1(1,0,0);
dfs2(1,1);
char str1[6];
while(q--)
{
scanf("%s",str1);
if(str1[0]=='I')
{
scanf("%d%d%d",&l,&r,&v);
insert(l,r,v,1);
}
else if(str1[0]=='D')
{
scanf("%d%d%d",&l,&r,&v);
insert(l,r,v,-1);
}
else if(str1[0]=='Q')
{
scanf("%d",&r);
printf("%I64d\n",query(w[r])+a[r]);
}
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: