Aragorn's Story

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

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
树链剖分入门
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#define inf 2e9
#define met(a,b) memset(a,b,sizeof a)
typedef long long ll;
using namespace std;
const int N =2e5+;
const int M = 4e6+;
int n,sum[N],m,tot,num,q;
int tre[N*],laz[N*];
int dep[N],siz[N],fa[N],id[N],son[N],val[N],top[N],c[N];
int head[N];
struct EDG{
int to,next;
}edg[N];
void add(int u,int v){
edg[tot].to=v;edg[tot].next=head[u];head[u]=tot++;
}
void init(){
met(head,-);met(tre,);
met(son,);met(laz,);
tot=;num=;
}
void dfs1(int u, int f, int d) {
dep[u] = d;
siz[u] = ;
son[u] = ;
fa[u] = f;
for (int i =head[u]; i !=-; i=edg[i].next) {
int ff = edg[i].to;
if (ff == f) continue;
dfs1(ff, u, d + );
siz[u] += siz[ff];
if (siz[son[u]] < siz[ff])
son[u] = ff;
}
}
void dfs2(int u, int tp) {
top[u] = tp;
id[u] = ++num;
if (son[u]) dfs2(son[u], tp);
for (int i =head[u]; i !=-; i=edg[i].next) {
int ff = edg[i].to;
if (ff == fa[u] || ff == son[u]) continue;
dfs2(ff, ff);
}
}
void build(int l,int r,int pos){
if(l==r){
tre[pos]=val[l];
return;
}
int mid=(l+r)>>;
build(l,mid,pos<<);
build(mid+,r,pos<<|);
return;
}
void pushdown(int num) {
if(laz[num]!=) {
tre[num*]+=laz[num];
tre[num*+]+=laz[num];
laz[num*]+=laz[num];
laz[num*+]+=laz[num];
laz[num]=;
}
}
void update(int num,int le,int ri,int x,int y,int p) {
if(x<=le&&y>=ri) {
tre[num]+=p;
laz[num]+=p;
return ;
}
pushdown(num);
int mid=(le+ri)/;
if(x<=mid)
update(num*,le,mid,x,y,p);
if(y>mid)
update(num*+,mid+,ri,x,y,p);
}
int query(int num,int le,int ri,int x) {
if(le==ri) {
return tre[num];
}
pushdown(num);
int mid=(le+ri)/;
if(x<=mid)
return query(num*,le,mid,x);
else
return query(num*+,mid+,ri,x);
}
void Youngth(int u,int v,int p){
int tp1=top[u],tp2=top[v];
while(tp1!=tp2){
if(dep[tp1]<dep[tp2]){
swap(tp1,tp2);swap(u,v);
}
update(,,num,id[tp1],id[u],p);
u=fa[tp1];
tp1=top[u];
}
if(dep[u]>dep[v])swap(u,v);
update(,,num,id[u],id[v],p);
}
int main() {
int u,v,p;
while(~scanf("%d%d%d",&n,&m,&q)) {
init();
for(int i=;i<=n;i++)scanf("%d",&c[i]);
while(m--){
scanf("%d%d",&u,&v);
add(u,v);add(v,u);
}
dfs1(,,);
dfs2(,);
for(int i=;i<=n;i++){
val[id[i]]=c[i];
}
build(,num,);
char str[];
while(q--){
scanf("%s",str);
if(str[]=='I'){
scanf("%d%d%d",&u,&v,&p);
Youngth(u,v,p);
}
else if(str[]=='D'){
scanf("%d%d%d",&u,&v,&p);
Youngth(u,v,-p);
}
else {
scanf("%d",&u);
printf("%d\n",query(,,num,id[u]));
}
}
}
return ;
}

HDU 3966 Aragorn's Story(树链剖分)(线段树区间修改)的更多相关文章

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

    Aragorn's Story 来源:http://www.fjutacm.com/Problem.jsp?pid=2710来源:http://acm.hdu.edu.cn/showproblem.p ...

  2. HDU 2460 Network(双连通+树链剖分+线段树)

    HDU 2460 Network 题目链接 题意:给定一个无向图,问每次增加一条边,问个图中还剩多少桥 思路:先双连通缩点,然后形成一棵树,每次增加一条边,相当于询问这两点路径上有多少条边,这个用树链 ...

  3. 【BZOJ-2325】道馆之战 树链剖分 + 线段树

    2325: [ZJOI2011]道馆之战 Time Limit: 40 Sec  Memory Limit: 256 MBSubmit: 1153  Solved: 421[Submit][Statu ...

  4. 【BZOJ2243】[SDOI2011]染色 树链剖分+线段树

    [BZOJ2243][SDOI2011]染色 Description 给定一棵有n个节点的无根树和m个操作,操作有2类: 1.将节点a到节点b路径上所有点都染成颜色c: 2.询问节点a到节点b路径上的 ...

  5. BZOJ2243 (树链剖分+线段树)

    Problem 染色(BZOJ2243) 题目大意 给定一颗树,每个节点上有一种颜色. 要求支持两种操作: 操作1:将a->b上所有点染成一种颜色. 操作2:询问a->b上的颜色段数量. ...

  6. POJ3237 (树链剖分+线段树)

    Problem Tree (POJ3237) 题目大意 给定一颗树,有边权. 要求支持三种操作: 操作一:更改某条边的权值. 操作二:将某条路径上的边权取反. 操作三:询问某条路径上的最大权值. 解题 ...

  7. bzoj4034 (树链剖分+线段树)

    Problem T2 (bzoj4034 HAOI2015) 题目大意 给定一颗树,1为根节点,要求支持三种操作. 操作 1 :把某个节点 x 的点权增加 a . 操作 2 :把某个节点 x 为根的子 ...

  8. HDU4897 (树链剖分+线段树)

    Problem Little Devil I (HDU4897) 题目大意 给定一棵树,每条边的颜色为黑或白,起始时均为白. 支持3种操作: 操作1:将a->b的路径中的所有边的颜色翻转. 操作 ...

  9. Aizu 2450 Do use segment tree 树链剖分+线段树

    Do use segment tree Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://www.bnuoj.com/v3/problem_show ...

  10. 【POJ3237】Tree(树链剖分+线段树)

    Description You are given a tree with N nodes. The tree’s nodes are numbered 1 through N and its edg ...

随机推荐

  1. (转)KlayGE游戏引擎 :高效的GBUFFER管理方式

    转载请注明出处为KlayGE游戏引擎,本文的永久链接为http://www.klayge.org/?p=3304 个顶点.这样的数据对GPU来说是很头疼的.所以引擎往往需要在Buffer上做一些工作来 ...

  2. rtmp jwplayer简单应用

    在nginx下使用,放到nginx/html文件夹内,之后访问 首先是播放vod视频 <!DOCTYPE html> <html xmlns="http://www.w3. ...

  3. idea中maven项目放到包中的mapper的xml文件不发布的问题

    今天重新一下mybatis的基础,然后一直报错,提示的是 result map 找不到com.zm.model.User对象可是看 mapper的写法没问题.找了半天才发现 是mapper没扫描到 解 ...

  4. 团队Alpha版本(七)冲刺

    目录 组员情况 组员1(组长):胡绪佩 组员2:胡青元 组员3:庄卉 组员4:家灿 组员5:凯琳 组员6:翟丹丹 组员7:何家伟 组员8:政演 组员9:黄鸿杰 组员10:刘一好 组员11:何宇恒 展示 ...

  5. 启动Tomcat时的常见问题及解决办法

    问题一:环境变量 1.检查jdk 验证jdk的配置,在运行-cmd中输入 java -version 即表示安装成功. 如果jdk没有问题,还需要配置两个环境变量.找到jdk和jre的路径,配置JAV ...

  6. 简单的JS钟表计时

    思路:先写出简单的数字计时,根据时分秒的数值转换成度数,使用CSS3的transform进行div倾斜. 知识点:transform可以对div进行倾斜或旋转等效果.但是根据浏览器不同代码也不同,本代 ...

  7. asp.net 存储过程 输出参数 取不到值

    这是MSDN上的明确解释:当您将 Command 对象用于存储过程时,可以将 Command 对象的 CommandType 属性设置为 StoredProcedure.当 CommandType 为 ...

  8. HDU 4910 HDOJ Problem about GCD BestCoder #3 第四题

    首先 m = 1 时 ans = 0对于 m > 1 的 情况 由于 1 到 m-1 中所有和m互质的数字,在 对m的乘法取模 运算上形成了群 ai = ( 1<=a<m & ...

  9. python带header

    headers = { "Accept":"text/html,application/xhtml+xml,application/xml;", "A ...

  10. PHP AES128加密解密

    <?php /** * Class AES */ class AES { public static function encrypt($input, $key) { $size = mcryp ...