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. Maven入门系列(二)--设置中央仓库的方法

    原文地址:http://www.codeweblog.com/maven入门系列-二-设置中央仓库的方法/ Maven仓库放在我的文档里好吗?当然不好,重装一次电脑,意味着一切jar都要重新下载和发布 ...

  2. 20145337 《Java程序设计》第九周学习总结

    20145337 <Java程序设计>第九周学习总结 教材学习内容总结 数据库本身是个独立运行的应用程序 撰写应用程序是利用通信协议对数据库进行指令交换,以进行数据的增删查找 JDBC可以 ...

  3. CNUOJ 2104 Day6-例3

    http://privateoj.cnuschool.org.cn/oj/home/problem.htm?problemID=2104 题的网址 没什么好解释的,用DFS= = 最开始记着把f数组下 ...

  4. Handler的总结

    Handler的总结 我们创建的Service.Activity,Broadcast均是一个主线程处理,即UI线程, 但是进行耗时操作时,比如I/O读写的大文件,数据库操作及网络下载需要很长的时间,为 ...

  5. php使用strlen()判断中文汉字字符串长度

    php使用strlen()判断中文汉字字符串长度 对于含有中文情况,此时可以采用: iconv_strlen($str,"UTF-8"); iconv_strlen 是无论是何种编 ...

  6. DevExpress GridView对表格的部分说明

    1. int[] selects = this.m_grdView1.GetSelectedRows(); // 获取选中的行,可能是几行 2. this.m_grdView1.GetRowCellV ...

  7. CSS典型案例实践

    CSS案例实践 一.层布局:定位元素重叠 在CSS中可以通过z-index属性来确定定位元素的层叠等级.需要注意的是: z-index属性只有在元素的position属性取值为relative.abs ...

  8. logstash使用操作部分

    1.logstash的概念及特点.概念:logstash是一个数据采集.加工处理以及传输(输出)的工具.特点: - 所有类型的数据集中处理 - 不同模式和格式数据的正常化 - 自定义日志格式的迅速扩展 ...

  9. Inside Flask - json 处理

    Inside Flask - json 处理 在处理 web api 时,json 是非常好用的数据交换格式,它结构简单,基本上各种主流的编程语言都有良好的支持工具. flask 中处理 json 时 ...

  10. 帝国CMS内容模板IF判断

    [e:loop={'selfinfo',50,0,0,"jingshu=$navinfor[jingshu]","id"}]<?php$class1=&q ...