HDU 3966 Aragorn's Story(树链剖分)(线段树区间修改)
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
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.
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.
1 2 3
2 1
2 3
I 1 3 5
Q 2
D 1 2 2
Q 1
Q 3
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(树链剖分)(线段树区间修改)的更多相关文章
- Aragorn's Story 树链剖分+线段树 && 树链剖分+树状数组
Aragorn's Story 来源:http://www.fjutacm.com/Problem.jsp?pid=2710来源:http://acm.hdu.edu.cn/showproblem.p ...
- HDU 2460 Network(双连通+树链剖分+线段树)
HDU 2460 Network 题目链接 题意:给定一个无向图,问每次增加一条边,问个图中还剩多少桥 思路:先双连通缩点,然后形成一棵树,每次增加一条边,相当于询问这两点路径上有多少条边,这个用树链 ...
- 【BZOJ-2325】道馆之战 树链剖分 + 线段树
2325: [ZJOI2011]道馆之战 Time Limit: 40 Sec Memory Limit: 256 MBSubmit: 1153 Solved: 421[Submit][Statu ...
- 【BZOJ2243】[SDOI2011]染色 树链剖分+线段树
[BZOJ2243][SDOI2011]染色 Description 给定一棵有n个节点的无根树和m个操作,操作有2类: 1.将节点a到节点b路径上所有点都染成颜色c: 2.询问节点a到节点b路径上的 ...
- BZOJ2243 (树链剖分+线段树)
Problem 染色(BZOJ2243) 题目大意 给定一颗树,每个节点上有一种颜色. 要求支持两种操作: 操作1:将a->b上所有点染成一种颜色. 操作2:询问a->b上的颜色段数量. ...
- POJ3237 (树链剖分+线段树)
Problem Tree (POJ3237) 题目大意 给定一颗树,有边权. 要求支持三种操作: 操作一:更改某条边的权值. 操作二:将某条路径上的边权取反. 操作三:询问某条路径上的最大权值. 解题 ...
- bzoj4034 (树链剖分+线段树)
Problem T2 (bzoj4034 HAOI2015) 题目大意 给定一颗树,1为根节点,要求支持三种操作. 操作 1 :把某个节点 x 的点权增加 a . 操作 2 :把某个节点 x 为根的子 ...
- HDU4897 (树链剖分+线段树)
Problem Little Devil I (HDU4897) 题目大意 给定一棵树,每条边的颜色为黑或白,起始时均为白. 支持3种操作: 操作1:将a->b的路径中的所有边的颜色翻转. 操作 ...
- 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 ...
- 【POJ3237】Tree(树链剖分+线段树)
Description You are given a tree with N nodes. The tree’s nodes are numbered 1 through N and its edg ...
随机推荐
- Percona-Tookit工具包之pt-heartbeat
Preface Replication delay is a common issue in MySQL replications.Especially in those replic ...
- 自动化测试(三)如何用python写个双色球
写一个程序,输入N就产生N条双色球号码 红球 6 01-33 蓝球 1 01-16 产生的双色球号码不能重复,写到一个文件里面,每一行是一条 红球: 01 03 05 07 08 ...
- Python 3基础教程7-if语句
前面文章介绍的循环语句,这里开始介绍控制语句.直接看下面的demo.py例子 # 这里介绍 if语句 x = 5y = 8z = 4s = 5 if x < y: print('x is les ...
- 洛谷P1071潜伏者(提高组)
题目描述 R国和S国正陷入战火之中,双方都互派间谍,潜入对方内部,伺机行动.历尽艰险后,潜伏于S国的R 国间谍小C终于摸清了 S 国军用密码的编码规则: 1. S国军方内部欲发送的原信息经过加密后在网 ...
- KMP算法-Python版
KMP算法-Python版 传统法: 从左到右一个个匹配,如果这个过程中有某个字符不匹配,就跳回去,将模式串向右移动一位.这有什么难的? 我们可以 ...
- java web登录界面 源代码
大概流程: 在java web项目中 导入sqljdbc4的包 在java Resources中完成java代码 在webContent 下建立一个存放自己写jsp的文件夹 将sqljdbc4和jst ...
- [THUWC2017][bzoj5020] 在美妙的数学王国中畅游 [LCT+泰勒展开]
题面 LOJ传送门 思路 这里很重要 它提示我们,把给定的三个函数泰勒展开,并用LCT维护每一项泰勒展开式的值,维护十几项就满足了题目的精度要求 我们考虑一个函数在0位置的泰勒展开 $f(x)=\su ...
- 关于flink的时间处理不正确的现象复现&原因分析
跟朋友聊天,说输出的时间不对,之前测试没关注到这个,然后就在processing模式下看了下,发现时间确实不正确 然后就debug,看问题在哪,最终分析出了原因,记录如下: 最下面给出了复现方案 ...
- position:absolute和float隐式改变display为inline-block
不论之前是什么类型的元素(display:none除外), 只要设置了position:absolute或float, 都会让元素以display:inline-block的方式显示, 可以设置长宽, ...
- linux 某个路径创建快捷方式
ln -s /绝对路径 桌面名称