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

Hdu 3966-Aragorn's Story LCT,动态树

2016-03-19 13:17 204 查看

题目:http://acm.hdu.edu.cn/showproblem.php?pid=3966

Aragorn's Story

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


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

题意:
给定一棵树和树上的点权,每次要区间加上一个数或减去一个数,查询某一个结点的权值。
题解:
直接用LCT区间修改即可。。。
查询前要先access一下。。。
多组数据每次要清空一下树。。。QAQ Tle了一发。。。

#include<bits/stdc++.h>
using namespace std;
#define MAXN 50010
struct node
{
int left,right,val;
}tree[MAXN];
int father[MAXN],rev[MAXN],tag[MAXN],Stack[MAXN];
int read()
{
int s=0,fh=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')fh=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){s=s*10+(ch-'0');ch=getchar();}
return s*fh;
}
int isroot(int x)
{
return tree[father[x]].left!=x&&tree[father[x]].right!=x;
}
void pushdown(int x)
{
int l=tree[x].left,r=tree[x].right;
if(rev[x]!=0)
{
rev[x]^=1;rev[l]^=1;rev[r]^=1;
swap(tree[x].left,tree[x].right);
}
if(tag[x]!=0)
{
tag[l]+=tag[x];tag[r]+=tag[x];
tree[l].val+=tag[x];tree[r].val+=tag[x];
tag[x]=0;
}
}
void rotate(int x)
{
int y=father[x],z=father[y];
if(!isroot(y))
{
if(tree[z].left==y)tree[z].left=x;
else tree[z].right=x;
}
if(tree[y].left==x)
{
father[x]=z;father[y]=x;tree[y].left=tree[x].right;tree[x].right=y;father[tree[y].left]=y;
}
else
{
father[x]=z;father[y]=x;tree[y].right=tree[x].left;tree[x].left=y;father[tree[y].right]=y;
}
}
void splay(int x)
{
int top=0,i,y,z;Stack[++top]=x;
for(i=x;!isroot(i);i=father[i])Stack[++top]=father[i];
for(i=top;i>=1;i--)pushdown(Stack[i]);
while(!isroot(x))
{
y=father[x];z=father[y];
if(!isroot(y))
{
if((tree[y].left==x)^(tree[z].left==y))rotate(x);
else rotate(y);
}
rotate(x);
}
}
void access(int x)
{
int last=0;
while(x!=0)
{
splay(x);
tree[x].right=last;
last=x;x=father[x];
}
}
void makeroot(int x)
{
access(x);splay(x);rev[x]^=1;
}
void link(int u,int v)
{
makeroot(u);father[u]=v;splay(u);
}
void cut(int u,int v)
{
makeroot(u);access(v);splay(v);father[u]=tree[v].left=0;
}
int findroot(int x)
{
access(x);splay(x);
while(tree[x].left!=0)x=tree[x].left;
return x;
}
int main()
{
int n,m,p,bb,ee,k,camp,i;
char fh[2];
while(scanf("%d %d %d",&n,&m,&p)!=EOF)
{
for(i=0;i<=n;i++)tree[i].val=tree[i].left=tree[i].right=0,tag[i]=0,father[i]=0,rev[i]=0;
for(i=1;i<=n;i++)tree[i].val=read();
for(i=1;i<=m;i++)
{
bb=read();ee=read();link(bb,ee);
}
for(i=1;i<=p;i++)
{
scanf("%s",fh);
if(fh[0]=='I')
{
bb=read();ee=read();k=read();
makeroot(bb);access(ee);splay(ee);
tag[ee]+=k;tree[ee].val+=k;
}
else if(fh[0]=='D')
{
bb=read();ee=read();k=read();
makeroot(bb);access(ee);splay(ee);
tag[ee]-=k;tree[ee].val-=k;
}
else
{
camp=read();
access(camp);
printf("%d\n",tree[camp].val);
}
}
}
fclose(stdin);
fclose(stdout);
return 0;
}


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