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 ...
随机推荐
- 你的第一个自动化测试:Appium 自动化测试
前言: 这是让你掌握 App 自动化的文章 一.前期准备 本文版权归作者和博客园共有,原创作者:http://www.cnblogs.com/BenLam,未经作者同意必须在文章页面给出原文连接. 1 ...
- RAID介绍和实现
RAID的全称是廉价磁盘冗余阵列(Redundant Array of Inexpensive Disks),于1987年由美国Berkeley 大学的两名工程师提出的. RAID出现的,最初目的是将 ...
- linux备忘录-vi和vim
知识点 vi的三种模式 一般模式 按 ESC 可回到一般模式 相关按键 j 代表 向下按钮 k 代表 向上按钮 h 代表 向左按钮 l 代表 向右按钮 20j 等代表 向下移动20行 Ctrl + f ...
- hadoop-eclipse环境搭建(二)
Eclipse插件配置 第一步:把我们的"hadoop-eclipse-plugin-1.0.0.jar"放到Eclipse的目录的"plugins"中,然后重 ...
- usb host和device的关系-ARM 论坛 - 21ic电子技术论坛
usb host和device的关系 疑问1:我们通常所用的u盘应该是usb device吧?我想这个不用多说,呵呵. ===============恩.============== 疑问2:我们通常 ...
- eclipse 运行错误:在类XXX中找不到 main 方法, 请将 main 方法定义为: public static void main(String[] args) 否则 JavaFX 应用程序类必须扩展javafx.application.Application
新建了一个类Hello: 代码: 第一次运行报错: 点击关闭该类的界面时出现: 点击是,然后再次打开,可以正确执行,结果为: 这是为什么....,后来发现了原因:是每次运行或调试前没有自动保存编辑的内 ...
- Linux运维文档之nginx
NGINX安装配置1.检查并且安装依赖组件检查安装nginx的依赖性,nginx的模块需要第三方库的支持,检查是否安装下列库:zlib.zlib-devel.openssl.openssl-devel ...
- JS正则表达式 简单应用
知识点: 先生成一个正则规则的对象,使用test()对传入的字符串进行验证,返回布尔类型 代码: <!doctype html><html><head> <m ...
- Codeforces 433 Div.2(A、B、C、D)
A. Fraction 暴力遍历1-1000,取组成的真分数比值最大且分子分母gcd为1时更新答案 代码: #include <stdio.h> #include <algorith ...
- [CF482B]Interesting Array
题目大意:构造一个序列$S$,有$m$条限制,每条为$l\;r\;q$,表示$\&_{i=l}^r S_i=q$ 题解:每条限制就把$[l,r]$内的数或上$q$,最后判断就行了 卡点:我又写 ...