HDU - 3966

Time Limit: 3000MS   Memory Limit: 32768KB   64bit IO Format: %I64d & %I64u

Submit Status

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

 
 
题解:题目大意是给你一棵树,树有对应的点权,然后给你q个命令,可以进行对某条链上的所有点值进行加减,或者对某个点的权值进行查询。
这是一道典型的树链剖分问题。所谓树链剖分就是将树上的点按链的方式连续标号,类似离散化,这样我们就可以利用这个特性,讲问题改变为区间问题,就可以套用线段树或者树状数组去解决这一类问题。

如上图,经过树链剖分算法后,每个点离散后的id值为括号上的值。跑完树链剖分后,我们把树分成了四条链,分别为1-4-8-9-12-13,2-3,5-6-7,10-11,注意到链上的点都是连续的,当我们要进行区间操作时,可以套个线段树。

那树链剖分又是怎么进行的呢?首先要把树分成几条链,分链前先介绍一个概念——重儿子。我们先定义一个数组size保存当前结点的所有子树上的点的个数,如上图size[3]=1,size[2]=2,size[9]=5...而一个结点的重儿子就是该结点的儿子中size的值最大的那个,比如1的重儿子是4,4的重儿子是8。知道重儿子后,就可以分链了。我们只要遍历每一个结点,把每个结点和他的重儿子连接起来,这样就把链分开了,但因为我们要对他进行顺序标号,所以可以利用dfs标号

代码如下:(可以按照上图模拟一下,模拟几次就懂了)

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#define X first
#define Y second
#define clr(u,v); memset(u,v,sizeof(u));
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#pragma comment(linker, "/STACK:1024000000,1024000000")
using namespace std;
typedef pair<int,int> pii;
const int maxn=;
const int INF=0x3f3f3f3f;
struct Edge
{
int v,to;
}E[maxn*];
int n,m,q;
int head[maxn],cnt;
int cost[maxn];
int size[maxn],deep[maxn],F[maxn],son[maxn],tot,top[maxn],id[maxn];
int add[maxn<<],sum[maxn<<];
void init()
{
clr(head,-);
cnt = ;
clr(size,);
clr(son,);
clr(sum,);
clr(add,);
tot = ;
}
void addedge(int v,int u)
{
E[cnt].v=u,E[cnt].to=head[v];
head[v]=cnt++;
} //以下为树链剖分部分
void dfs1(int cur ,int pre,int d)
{
deep[cur] = d;//记录深度
size[cur] = ;//记录子树的所有结点数
F[cur] = pre;//记录该结点的父亲结点
for (int i=head[cur];~i;i=E[i].to)
{
int v=E[i].v;
if (v == pre) continue ;
dfs1(v,cur,d+);
size[cur]+=size[v];//回溯更新size值
}
if (size[son[pre]]<size[cur]) son[pre]=cur;//更新重儿子
}
void dfs2(int cur,int fa)//第一个参数代表当前结点,第二个参数代表该链上的祖先结点
{
top[cur] = fa;//top记录该点所在链上的祖先结点
id[cur] = tot++;//对每个结点进行标号
if (son[cur]) dfs2(son[cur],fa);//如果该结点有重儿子,继续深搜下去,由于还是那条链,所以还是传fa
for (int i=head[cur];~i;i=E[i].to)//遍历其他儿子结点
{
int v=E[i].v;
if (son[cur]!=v&&F[cur]!=v)
dfs2(v,v);//由于该儿子结点不在当前结点的链上,所以要新开一条链,该链的祖先结点设置为自己
}
} //以下为线段树部分
void PushUp(int rt)
{
sum[rt]=sum[rt<<]+sum[rt<<|];
}
void PushDown(int rt,int m)
{
if (add[rt])
{
add[rt<<]+=add[rt];
add[rt<<|]+=add[rt];
sum[rt<<]+=add[rt]*(m-(m>>));
sum[rt<<|]+=add[rt]*(m>>);
add[rt]=;
}
}
void update(int L,int R,int c,int l,int r,int rt)
{
if (L<=l&&r<=R)
{
add[rt]+=c;
sum[rt]+=c*(r-l+);
return ;
}
PushDown(rt,r-l+);
int m=(l+r)>>;
if (L<=m) update(L,R,c,lson);
if (R>m) update(L,R,c,rson);
PushUp(rt);
}
int query(int L,int R,int l,int r,int rt)
{
if (L<=l&&r<=R) return sum[rt];
PushDown(rt,r-l+);
int m=(l+r)>>;
int ret=;
if (L<=m) ret+=query(L,R,lson);
if (m<R) ret+=query(L,R,rson);
return ret;
} //以下为修改部分
void change(int x,int y,int val)
{
while (top[x]!=top[y])//判断两个结点是不是在同一条链上,如果不是,则进行下列操作
{
if (deep[top[x]]<deep[top[y]]) swap(x,y);//找一个深度深的点
update(id[top[x]],id[x],val,,n,);//更新改点到其链上祖先的点上的所有值
x=F[top[x]];//并把该点更新为该链上祖先结点的父亲结点
}
if (deep[x]>deep[y]) swap(x,y);//此时两个结点在同一条链上
update(id[x],id[y],val,,n,);//直接更新x~y上的所有点
}
int main()
{
#ifdef LOCAL
freopen("data","r",stdin);
#endif
while (~scanf("%d%d%d",&n,&m,&q))
{
init();
for (int i=;i<=n;i++)
scanf("%d",&cost[i]);
int u,v;
while (m--)
{
scanf("%d%d",&v,&u);
addedge(v,u);
addedge(u,v);
}
int root = ;
dfs1(root,,);
dfs2(root,root);
for (int i=;i<=n;i++)
update(id[i],id[i],cost[i],,n,);
char str[];
int a,b,c;
while (q--)
{
scanf("%s",str);
if (str[] == 'I')
{
scanf("%d%d%d",&a,&b,&c);
change(a,b,c);
}
else if (str[] == 'Q')
{
scanf("%d",&a);
int ans = query(id[a],id[a],,n,);
printf("%d\n",ans);
}
else
{
scanf("%d%d%d",&a,&b,&c);
change(a,b,-c);
}
}
}
return ;
}

HDU - 3966 Aragorn's Story(树链剖分入门+线段树)的更多相关文章

  1. HDU 4366 Successor(树链剖分+zkw线段树+扫描线)

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=4366 [题目大意] 有一个公司,每个员工都有一个上司,所有的人呈树状关系,现在给出每个人的忠诚值和 ...

  2. bzoj 4034 [HAOI2015] T2(树链剖分,线段树)

    4034: [HAOI2015]T2 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 1536  Solved: 508[Submit][Status] ...

  3. bzoj 1036 [ZJOI2008]树的统计Count(树链剖分,线段树)

    1036: [ZJOI2008]树的统计Count Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 10677  Solved: 4313[Submit ...

  4. poj 3237 Tree(树链剖分,线段树)

    Tree Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 7268   Accepted: 1969 Description ...

  5. bzoj 3626 [LNOI2014]LCA(离线处理+树链剖分,线段树)

    3626: [LNOI2014]LCA Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 1272  Solved: 451[Submit][Status ...

  6. bzoj 2243 [SDOI2011]染色(树链剖分,线段树)

    2243: [SDOI2011]染色 Time Limit: 20 Sec  Memory Limit: 512 MBSubmit: 4637  Solved: 1726[Submit][Status ...

  7. 【BZOJ3531】旅行(树链剖分,线段树)

    [BZOJ3531]旅行(树链剖分,线段树) 题面 Description S国有N个城市,编号从1到N.城市间用N-1条双向道路连接,满足 从一个城市出发可以到达其它所有城市.每个城市信仰不同的宗教 ...

  8. 【BZOJ5507】[GXOI/GZOI2019]旧词(树链剖分,线段树)

    [BZOJ5507][GXOI/GZOI2019]旧词(树链剖分,线段树) 题面 BZOJ 洛谷 题解 如果\(k=1\)就是链并裸题了... 其实\(k>1\)发现还是可以用类似链并的思想,这 ...

  9. [bzoj4196][Noi2015]软件包管理器_树链剖分_线段树

    软件包管理器 bzoj-4196 Noi-2015 题目大意:Linux用户和OSX用户一定对软件包管理器不会陌生.通过软件包管理器,你可以通过一行命令安装某一个软件包,然后软件包管理器会帮助你从软件 ...

随机推荐

  1. 看完这篇文章才对【GIT】有了大彻大悟的认识

    git定义:版本管理工具 历史:没有git的时候,我们修改完一个文件,要先把文件拷贝一个副本到备份文件夹中,还要建个文档来记录当前文件的信息和操作记录.当文件多的时候,人工操作起来将是个非常辛苦非常庞 ...

  2. JavaScript利用闭包实现模块化

    利用闭包的强大威力,但从表面上看,它们似乎与回调无关.下面一起来研究其中最强大的一个:模块. function foo() { var something = "cool"; va ...

  3. redis 学习笔记——数据同步、事务

    redis主从同步      redis支持简单易用的主从复制(master-slave replication)功能,该功能也是redis高可用性实现的基础.   redis复制原理      re ...

  4. .NET 同步与异步之锁(Lock、Monitor)(七)

    本随笔续接:.NET同步与异步之相关背景知识(六) 在上一篇随笔中已经提到.解决竞争条件的典型方式就是加锁 ,那本篇随笔就重点来说一说.NET提供的最常用的锁 lock关键字 和 Monitor. 一 ...

  5. 浅谈JAVA中字符串常量的储存位置

    在讲述这些之前我们需要一些预备知识: java中的内存被分成以下部分: 1.栈区:由编译器自动分配释放,具体方法执行结束后,系统自动释放JVM内存资源. 其作用有保存局部变量的值,包括:1.用来保存基 ...

  6. BitMap - leetcode [位运算]

    136. Single Number 因为A XOR A = 0,且XOR运算是可交换的,于是,对于实例{2,1,4,5,2,4,1}就会有这样的结果: (2^1^4^5^2^4^1) => ( ...

  7. Scala减少代码重复

    高阶函数可以把其它函数当作函数参数,帮助我们减少代码重复,例如: object FileMatcher { private def fileHere = (new File(".\\file ...

  8. LINQ 之Union All/Union/Intersect操作

    闪存 首页 新随笔 管理 订阅     Union All/Union/Intersect操作 适用场景:对两个集合的处理,例如追加.合并.取相同项.相交项等等. Concat(连接) 说明:连接不同 ...

  9. js作用域详解

    // 作用域:(1)域:空间.范围.区域……     (2) 作用:读.写 script 全局变量.全局函数 自上而下 函数 由里到外 浏览器: “JS解析器” 1)“找一些东西” :var func ...

  10. 使用python修改QQ密保(脚本)

    一.基于以下目的: 1.为了增加对Http协议理解能力,对QQ密保修改的请求进行了分析 2.为了锻炼python的编写能力 3.对web综合知识的理解 花了点时间写了这个脚本,下面介绍脚本的过程 二. ...