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. 【非原创】tomcat 安装时出现 Failed to install Tomcat7 service

    tomcat 安装时出现 Failed to install Tomcat7 service 今天在安装tomcat时提示 Failed to install Tomcat7 service了,花了大 ...

  2. Codeforces Round #327 (Div2) A~E

    CodeForces 591A 题意:在距离为L的两端A,B,相向发射魔法,a(以P1的速度)-->B,A<--b(以P2的速度).假设a-->B,途中相遇,则返回到原点A<- ...

  3. java安全提交笔记【xmind图片】

  4. 【Python】- 第一行跟第二行的写法

    第一行:目的就是指出,你想要你的这个文件中的代码用什么可执行程序去运行它,就这么简单 #!/usr/bin/python:告诉操作系统执行这个脚本的时候,调用/usr/bin下的python解释器: ...

  5. linux sed讲解

    1.sed 查找替换 显示某一行或某几行##替换sed 's###g' oldboy.txtsed 's@@@g' oldboy.txt sed -i 's###g' oldboy.txtsed -i ...

  6. 基于linux操作系统安装、使用redis详解

    服务端安装 Redis的官方下载站是http://redis.io/download,可以去上面下载最新的安装程序下来,我写此文章时的的稳定版本是2.6.11. 步骤一: 下载Redis 进入软件安装 ...

  7. 多线程 线程池 ExecutorService

    package org.zln.thread; import java.util.Date; import java.util.concurrent.ExecutorService; import j ...

  8. poj 2253 Frogger (最短路径)

    Frogger Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 22557   Accepted: 7339 Descript ...

  9. 原来Java大数据才是真正的高富帅!

    大数据时代,中国IT环境也将面临重新洗牌,不仅仅是企业,更是程序员们转型可遇而不可求的机遇. 国内大多数大型互联网公司的程序员被称作研发工程师,但实际上国内几乎没有研发项目,只能叫做开发.开发程序员的 ...

  10. [poj] 2286 The Rotation Game || ID-DFS

    原题 有1234四个数字,每个数字八个.有八种方向的移动,使得操作后中间八个方块的数字相同,求最小操作步数. 对于这种求最小步数的看起来就是dfs的题,就ID-DFS就好了. //不知道为什么都是ID ...