AC日记——Aragorn's Story HDU 3966
Aragorn's Story
Time Limit: 10000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 10510 Accepted Submission(s):
2766
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.
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.
of enemies in the specified camp.
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.
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm> #define maxn 50001 using namespace std; struct TreeNodeType {
int l,r,dis,mid,flag; void clear()
{
l=,r=,dis=,mid=,flag=;
}
};
struct TreeNodeType tree[maxn<<]; struct EdgeType {
int to,next;
};
struct EdgeType edge[maxn<<]; int if_z,n,m,q,cnt,tot,Enum,deep[maxn],size[maxn],belong[maxn];
int flag[maxn],head[maxn],dis[maxn],dis_[maxn],f[maxn]; char Cget; inline void read_int(int &now)
{
now=,if_z=,Cget=getchar();
while(Cget<''||Cget>'')
{
if(Cget=='-') if_z=-;
Cget=getchar();
}
while(Cget>=''&&Cget<='')
{
now=now*+Cget-'';
Cget=getchar();
}
now*=if_z;
} inline void edge_add(int from,int to)
{
edge[++Enum].to=from,edge[Enum].next=head[to],head[to]=Enum;
edge[++Enum].to=to,edge[Enum].next=head[from],head[from]=Enum;
} void search(int now,int fa)
{
int pos=tot++;
deep[now]=deep[fa]+,f[now]=fa;
for(int i=head[now];i;i=edge[i].next)
{
if(edge[i].to==fa) continue;
search(edge[i].to,now);
}
size[now]=tot-pos;
} void search_(int now,int chain)
{
int pos=;
flag[now]=++tot,dis_[flag[now]]=dis[now];
belong[now]=chain;
for(int i=head[now];i;i=edge[i].next)
{
if(flag[edge[i].to]) continue;
if(size[edge[i].to]>size[pos]) pos=edge[i].to;
}
if(pos!=) search_(pos,chain);
else return ;
for(int i=head[now];i;i=edge[i].next)
{
if(flag[edge[i].to]) continue;
search_(edge[i].to,edge[i].to);
}
} inline void tree_up(int now)
{
tree[now].dis=tree[now<<].dis+tree[now<<|].dis;
} inline void tree_down(int now)
{
if(tree[now].l==tree[now].r) return ;
tree[now<<].dis+=(tree[now<<].r-tree[now<<].l+)*tree[now].flag;
tree[now<<].flag+=tree[now].flag;
tree[now<<|].dis+=(tree[now<<|].r-tree[now<<|].l+)*tree[now].flag;
tree[now<<|].flag+=tree[now].flag;
tree[now].flag=;
} void tree_build(int now,int l,int r)
{
tree[now].clear();
tree[now].l=l,tree[now].r=r;
if(l==r)
{
tree[now].dis=dis_[++tot];
return ;
}
tree[now].mid=(l+r)>>;
tree_build(now<<,l,tree[now].mid);
tree_build(now<<|,tree[now].mid+,r);
tree_up(now);
} void tree_change(int now,int l,int r,int x)
{
if(tree[now].l==l&&tree[now].r==r)
{
tree[now].dis+=(r-l+)*x;
tree[now].flag+=x;
return ;
}
if(tree[now].flag) tree_down(now);
if(l>tree[now].mid) tree_change(now<<|,l,r,x);
else if(r<=tree[now].mid) tree_change(now<<,l,r,x);
else
{
tree_change(now<<,l,tree[now].mid,x);
tree_change(now<<|,tree[now].mid+,r,x);
}
tree_up(now);
} int tree_query(int now,int to)
{
if(tree[now].l==tree[now].r&&tree[now].l==to)
{
return tree[now].dis;
}
if(tree[now].flag) tree_down(now);
tree_up(now);
if(to>tree[now].mid) return tree_query(now<<|,to);
else return tree_query(now<<,to);
} inline void solve_change(int x,int y,int z)
{
while(belong[x]!=belong[y])
{
if(deep[belong[x]]<deep[belong[y]]) swap(x,y);
tree_change(,flag[belong[x]],flag[x],z);
x=f[belong[x]];
}
if(deep[x]<deep[y]) swap(x,y);
tree_change(,flag[y],flag[x],z);
} int main()
{
while(scanf("%d%d%d",&n,&m,&q)==)
{
memset(head,,sizeof(head));
memset(size,,sizeof(size));
memset(flag,,sizeof(flag));
tot=,cnt=,Enum=;
for(int i=;i<=n;i++) read_int(dis[i]);
int from,to,cur;
for(int i=;i<=m;i++)
{
read_int(from),read_int(to);
edge_add(from,to);
}
search(,),tot=,search_(,);
tot=,tree_build(,,n);
char type;
for(int i=;i<=q;i++)
{
cin>>type;
if(type=='I')
{
read_int(from),read_int(to),read_int(cur);
solve_change(from,to,cur);
}
if(type=='D')
{
read_int(from),read_int(to),read_int(cur);
solve_change(from,to,-cur);
}
if(type=='Q')
{
read_int(from);
printf("%d\n",tree_query(,flag[from]));
}
}
}
return ;
}
最新文章
- Eclipse利用Axis2插件构建Web Service并测试
- Ajax中的get和post两种请求方式的异同
- Unity IoC Container创建对象过程
- C头文件之<;cstring>;
- 【海量视频】2013年上半年BPM厂商&#39;K2&#39;市场活动资料集锦
- css中的img和input标签
- 一步一步教你做ios推送
- 聊聊并发(六)——ConcurrentLinkedQueue的实现原理分析
- 2016-2017 CT S03E02: Codeforces Trainings Season 3 Episode 2
- IntelliJ Idea设置默认换行符
- 【bzoj4011 hnoi2015】落忆枫音
- 太原面经分享:如何用js实现返回斐波那契数列的第n个值的函数
- Spring理解IOC,DI,AOP作用,概念,理解。
- Python:Day05 作业
- chrome浏览器解决跨域问题
- soapUI工具使用方法、简介、接口测试
- python多行代码简化
- UML类图(Unified Modeling Language Class Diagrams)
- request应用实例
- 用js来实现银行家算法