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
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.
Q 3
1.The number of enemies may be negative.
2.Huge input, be careful.
#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+; 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*]; void init()
{
pos = cont = ;
memset(son,-,sizeof(son));
memset(head,-,sizeof(head));
memset(hxl,,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] = ;
for(int i=head[u];i!=-;i=edge[i].next)
{
int v = edge[i].to;
if(v!=fre)
{
dfs1(v,u,deep+);
vis[u]=vis[u]+vis[v];
if(son[u] == - || vis[v] > vis[son[u]])
son[u] = v;
}
}
}
void dfs2(int u,int t)
{
top[u] = t;
if(son[u]!=-)
{
w[u]=++pos;
dfs2(son[u],t);
}
else
{
w[u]=++pos;
return;
}
for(int i=head[u];i!=-;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==)return ;
LL sum1 = ;
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]+,pos,-*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]+,pos,-*num1*size1);
}
int main()
{
int n,m,q,u,v,l,r;
while(scanf("%d%d%d",&n,&m,&q)>)
{
init();
for(int i=;i<=n;i++)
scanf("%d",&a[i]);
for(int i=;i<=m;i++)
{
scanf("%d%d",&u,&v);
addedge(u,v);
addedge(v,u);
}
addedge(,);
addedge(,);
dfs1(,,);
dfs2(,);
char str1[];
while(q--)
{
scanf("%s",str1);
if(str1[]=='I')
{
scanf("%d%d%d",&l,&r,&v);
insert(l,r,v,);
}
else if(str1[]=='D')
{
scanf("%d%d%d",&l,&r,&v);
insert(l,r,v,-);
}
else if(str1[]=='Q')
{
scanf("%d",&r);
printf("%I64d\n",query(w[r])+a[r]);
}
}
}
return ;
}
hdu 3966 Aragorn's Story 树链剖分 按点的更多相关文章
-
HDU 3966 Aragorn&#39;s Story 树链剖分+树状数组 或 树链剖分+线段树
HDU 3966 Aragorn's Story 先把树剖成链,然后用树状数组维护: 讲真,研究了好久,还是没明白 树状数组这样实现"区间更新+单点查询"的原理... 神奇... ...
-
Hdu 3966 Aragorn&#39;s Story (树链剖分 + 线段树区间更新)
题目链接: Hdu 3966 Aragorn's Story 题目描述: 给出一个树,每个节点都有一个权值,有三种操作: 1:( I, i, j, x ) 从i到j的路径上经过的节点全部都加上x: 2 ...
-
HDU 3966 Aragorn&#39;s Story(树链剖分)(线段树区间修改)
Aragorn's Story Time Limit: 10000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
-
HDU 3966 Aragorn&#39;s Story 树链剖分+BIT区间修改/单点询问
Aragorn's Story Description Our protagonist is the handsome human prince Aragorn comes from The Lord ...
-
HDU 3966 Aragorn&#39;s Story 树链剖分
Link: http://acm.hdu.edu.cn/showproblem.php?pid=3966 这题注意要手动扩栈. 这题我交g++无限RE,即使手动扩栈了,但交C++就过了. #pragm ...
-
hdu 3966 Aragorn&#39;s Story : 树链剖分 O(nlogn)建树 O((logn)&#178;)修改与查询
/** problem: http://acm.hdu.edu.cn/showproblem.php?pid=3966 裸板 **/ #include<stdio.h> #include& ...
-
HDU 3966 Aragorn&#39;s Story (树链剖分入门题)
树上路径区间更新,单点查询. 线段树和树状数组都可以用于本题的维护. 线段树: #include<cstdio> #include<iostream> #include< ...
-
HDU 3966 Aragorn&#39;s Story (树链点权剖分,成段修改单点查询)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3966 树链剖分的模版,成段更新单点查询.熟悉线段树的成段更新的话就小case啦. //树链剖分 边权修 ...
-
HDU 3966 Aragorn&#39;s Story 树链拋分
一.写在前面 终于开始开坑link-cut-tree这个了,对于网上找到的大佬的前进路线,进行了一番研发,发现实际上可以实现对于树链拋分的制作.经历了若干长时间之后终于打了出来(为什么每次学什么东西都 ...
随机推荐
-
git 小结
git cherry-pick de0ec64 可将另一个分支中的提交 cherry-pick到当前分支来
-
C++STL学习笔记_(1)deque双端数组知识
#include<iostream> using namespace std; #include "deque" #include "algorithm&qu ...
-
数据仓库原理<;1>;:数据库与数据仓库
updated 2015.8.27 updated 2015.8.26 updated 2015.8.23 0. 说明 <数据仓库原理>系列博文,是笔者在学习数据仓库与商业智能时的读书笔记 ...
-
Oracle RAC环境下如何更新patch(Rolling Patch)
Oracle RAC数据库环境与单实例数据库环境有很多共性,也有很多异性.对于数据库补丁的更新同样如此,都可以通过opatch来完成.但RAC环境的补丁更新有几种不同的更新方式,甚至于可以在零停机的情 ...
-
Python 计算程序运行时间
import time def start_sleep(): time.sleep(3) if __name__ == '__main__': #The start time st ...
-
OpenJudge 2790 迷宫
1.链接地址: http://bailian.openjudge.cn/practice/2790/ 2.题目: 总时间限制: 3000ms 内存限制: 65536kB 描述 一天Extense在森林 ...
-
iOS UIKit:App
1.App生命周期 IOS架构是由许多设计模式实现,如model-view-controller 和 delegation模式. 1.1 main函数 与其它框架类似,IOS框架的入口也是从main函 ...
-
JS定时器设置、快速取消
1.首先定义自己的方法 function test() { alert("开始"); } 2.在定时器中使用 setInterval("test()",1000 ...
-
【Linux基础】Unix与Linux操作系统介绍
一.Unix和Linux操作系统概述 1.Unix是什么 UNIX是一个计算机操作系统,一个用来协调.管理和控制计算机硬件和软件资源的控制程序. 2.Unix特点 (1)多用户:在同一时刻可以有多个用 ...
-
FatMouse&#39; Trade(杭电ACM---1009)
FatMouse' Trade Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...