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

Problem Description
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.
 
Input
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.

 
Output
For each query, you need to output the actually number
of enemies in the specified camp.
 
Sample Input
3 2 5
1 2 3
2 1
2 3
I 1 3 5
Q 2
D 1 2 2
Q 1
Q 3
 
Sample Output
7
4
8

Hint

1.The number of enemies may be negative.

2.Huge input, be careful.

 
Source
 
Recommend
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+; 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 树链剖分 按点的更多相关文章

  1. HDU 3966 Aragorn's Story 树链剖分+树状数组 或 树链剖分+线段树

    HDU 3966 Aragorn's Story 先把树剖成链,然后用树状数组维护: 讲真,研究了好久,还是没明白 树状数组这样实现"区间更新+单点查询"的原理... 神奇... ...

  2. Hdu 3966 Aragorn's Story (树链剖分 + 线段树区间更新)

    题目链接: Hdu 3966 Aragorn's Story 题目描述: 给出一个树,每个节点都有一个权值,有三种操作: 1:( I, i, j, x ) 从i到j的路径上经过的节点全部都加上x: 2 ...

  3. HDU 3966 Aragorn's Story(树链剖分)(线段树区间修改)

    Aragorn's Story Time Limit: 10000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  4. HDU 3966 Aragorn's Story 树链剖分+BIT区间修改/单点询问

    Aragorn's Story Description Our protagonist is the handsome human prince Aragorn comes from The Lord ...

  5. HDU 3966 Aragorn's Story 树链剖分

    Link: http://acm.hdu.edu.cn/showproblem.php?pid=3966 这题注意要手动扩栈. 这题我交g++无限RE,即使手动扩栈了,但交C++就过了. #pragm ...

  6. hdu 3966 Aragorn's Story : 树链剖分 O(nlogn)建树 O((logn)²)修改与查询

    /** problem: http://acm.hdu.edu.cn/showproblem.php?pid=3966 裸板 **/ #include<stdio.h> #include& ...

  7. HDU 3966 Aragorn's Story (树链剖分入门题)

    树上路径区间更新,单点查询. 线段树和树状数组都可以用于本题的维护. 线段树: #include<cstdio> #include<iostream> #include< ...

  8. HDU 3966 Aragorn's Story (树链点权剖分,成段修改单点查询)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3966 树链剖分的模版,成段更新单点查询.熟悉线段树的成段更新的话就小case啦. //树链剖分 边权修 ...

  9. HDU 3966 Aragorn's Story 树链拋分

    一.写在前面 终于开始开坑link-cut-tree这个了,对于网上找到的大佬的前进路线,进行了一番研发,发现实际上可以实现对于树链拋分的制作.经历了若干长时间之后终于打了出来(为什么每次学什么东西都 ...

随机推荐

  1. C++重载operator的示例

    #include<iostream>#include<vector>using namespace std; class test{public:     int v;   / ...

  2. 读取文件内容fopen,fgets,fclose

    <?php //首先采用“fopen”函数打开文件,得到返回值的就是资源类型.$file_handle = fopen("/data/webroot/resource/php/f.tx ...

  3. 邮件格式(HTML/TXT),TXT为文本邮件

    <?phpclass smtp{/* Public Variables */var $smtp_port;var $time_out;var $host_name;var $log_file;v ...

  4. IOS第七天(5:UiTableView 汽车品牌,复杂模型分组展示,A-Z索要列表) (2015-08-05 14:03)

    复杂模型分组展示 #import "HMViewController.h" #import "HMCarGroup.h" #import "HMCar ...

  5. 【iCore3 双核心板_ uC/OS-III】例程四:时间管理

    实验指导书及代码包下载: http://pan.baidu.com/s/1pKWKuBT iCore3 购买链接: https://item.taobao.com/item.htm?id=524229 ...

  6. String类StringBuffer类与StringBuilder类gc垃圾回收

    String类的特点 直接赋值和new调用构造方法两种, 直接赋值时会将字符串常量入内存池,当其他变量再赋相同值时,不再在堆空间开辟内存 new构造方法会开辟两块堆内存空间,可以使用intern手工入 ...

  7. Jquery的命名冲突

    $是Jquery的别名,为了编码方便,我们可以使用$符号来调用Jquery的函数.然而,当我们引入多个JS库的时候,如果另外一个库中也引用了$符号作为别名的话,那么我们在使用$符号的时候,由于同一个作 ...

  8. Yii源码阅读笔记(二十九)

    动态模型DynamicModel类,用于实现模型内数据验证: namespace yii\base; use yii\validators\Validator; /** * DynamicModel ...

  9. SaveData Functions

    Here are some save function for some situations: Yes/No /// <summary> ///保存数据到WCF /// </sum ...

  10. jQuery 控制表单和表格

    这里总结了jQuery中对表格和表单的一些常用操作, 通过这里的实例和操作肯定对jQuery的掌握有一个新得提高, 希望大家耐心看完, 多实践. <!DOCTYPE html PUBLIC &q ...