HDU 3966(树链剖分+点修改+点查询)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3966
题目大意:营地的分布成树型。每个营地都有一些人,每次修改修改一条链上的所有营地的人数,每次查询单个点。
解题思路:
树链剖分基础题。
维护一个sum。
注意轻链修改时,点修改和边修改的不同。
由于树的结构与线段树点的顺序不太相同,因此需要做一个映射数组rank。故在线段树Build的时候,权值是camp[rank[l]],rank这步的映射在dfs2的时候完成,rank[w[u]]=u;
Query单点u的时候, w[u]是其在线段树中的位置。
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include "cstdio"
#include "vector"
#include "cstring"
using namespace std;
#define maxn 50005
int camp[maxn],s[maxn],dep[maxn],pos[maxn],son[maxn],top[maxn],fa[maxn],w[maxn],rank[maxn],cnt,n;
vector<int> G[maxn];
void dfs1(int u,int pre,int d)
{
s[u]=;fa[u]=pre;dep[u]=d;
for(int i=;i<G[u].size();i++)
{
int v=G[u][i];
if(v==pre) continue;
dfs1(v,u,d+);
s[u]+=s[v];
if(son[u]!=-||s[v]>s[son[u]]) son[u]=v;
}
}
void dfs2(int u,int tp)
{
w[u]=++cnt;top[u]=tp;rank[w[u]]=u;
if(son[u]==-) return;
dfs2(son[u],tp);
for(int i=;i<G[u].size();i++)
{
int v=G[u][i];
if(v!=son[u]&&v!=fa[u])
dfs2(v,v);
}
}
//Segment-Tree
#define lson l,mid,root<<1
#define rson mid+1,r,root<<1|1
int col[maxn<<],sum[maxn<<];
void PushUp(int root)
{
sum[root]=sum[root<<]+sum[root<<|];
}
void Build(int l,int r,int root)
{
col[root]=;
if(l==r)
{
sum[root]=camp[rank[l]];
return;
}
int mid=(l+r)>>;
Build(lson);
Build(rson);
PushUp(root);
}
void PushDown(int root,int m)
{
if(col[root])
{
col[root<<]+=col[root];
col[root<<|]+=col[root];
sum[root<<]+=(m-(m>>))*col[root];
sum[root<<|]+=(m>>)*col[root];
col[root]=;
}
}
void Update(int L,int R,int v,int l,int r,int root)
{
if(L<=l&&r<=R)
{
col[root]+=v;
sum[root]+=(r-l+)*v;
return;
}
PushDown(root,r-l+);
int mid=(l+r)>>;
if(L<=mid) Update(L,R,v,lson);
if(R>mid) Update(L,R,v,rson);
PushUp(root);
}
int Query(int p,int l,int r,int root)
{
if(l==r) return sum[root];
PushDown(root,r-l+);
int mid=(l+r)>>,ret=;
if(p<=mid) ret=Query(p,lson);
else ret=Query(p,rson);
return ret;
}
void Change(int x,int y,int v)
{
while(top[x]!=top[y])
{
if(dep[top[x]]<dep[top[y]]) swap(x,y);
Update(w[top[x]],w[x],v,,n,);
x=fa[top[x]];
}
if(dep[x]>dep[y]) swap(x,y);
Update(w[x],w[y],v,,n,);//与边修改不同
}
int main()
{
//freopen("in.txt","r",stdin);
int m,p,u,v,c;
while(scanf("%d%d%d",&n,&m,&p)!=EOF)
{
memset(son,-,sizeof(son));
for(int i=;i<=n;i++) G[i].clear();
cnt=;
for(int i=;i<=n;i++)
scanf("%d",&camp[i]);
for(int i=;i<=m;i++)
{
scanf("%d%d",&u,&v);
G[u].push_back(v);
G[v].push_back(u);
}
dfs1(,,);
dfs2(,);
Build(,n,);
char cmd[];
for(int i=;i<=p;i++)
{
scanf("%s",&cmd);
if(cmd[]=='I')
{
scanf("%d%d%d",&u,&v,&c);
Change(u,v,c);
}
if(cmd[]=='D')
{
scanf("%d%d%d",&u,&v,&c);
Change(u,v,-c);
}
if(cmd[]=='Q')
{
scanf("%d",&c);
printf("%d\n",Query(w[c],,n,));
}
}
} }
| 11757337 | 2014-09-29 16:51:29 | Accepted | 3966 | 2484MS | 8308K | 3299 B | C++ | Physcal |
HDU 3966(树链剖分+点修改+点查询)的更多相关文章
- hdu 3966 Aragorn's Story(树链剖分+区间修改+单点查询)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3966 题意:给一棵树,并给定各个点权的值,然后有3种操作: I C1 C2 K: 把C1与C2的路径上 ...
- HDU 3966 /// 树链剖分+树状数组
题意: http://acm.hdu.edu.cn/showproblem.php?pid=3966 给一棵树,并给定各个点权的值,然后有3种操作: I x y z : 把x到y的路径上的所有点权值加 ...
- HDU 3966 (树链剖分+线段树)
Problem Aragorn's Story (HDU 3966) 题目大意 给定一颗树,有点权. 要求支持两种操作,将一条路径上的所有点权值增加或减少ai,询问某点的权值. 解题分析 树链剖分模板 ...
- hdu 3966(树链剖分+线段树区间更新)
传送门:Problem 3966 https://www.cnblogs.com/violet-acmer/p/9711441.html 学习资料: [1]线段树区间更新:https://blog.c ...
- hdu 3966 树链剖分
思路:树链剖分入门题,我这门入得好苦啊,程序很快写出来了,可是在LCA过程中把update函数里的左右边界位置写反了,一直RE到死. #pragma comment(linker, "/ST ...
- HDU 3966 树链剖分后线段树维护
题意: 一棵树, 操作1.$path(a,b)$之间的点权$+k$ 操作2.单点查询 题解: 树链剖分即可,注意代码细节,双向映射 主要是记录一下板子 #include <string.h> ...
- POJ 2763 (树链剖分+边修改+边查询)
题目链接:http://poj.org/problem?id=2763 题目大意:某人初始在s点.有q次移动,每次移动沿着树上一条链,每经过一条边有一定花费,这个花费可以任意修改.问每次移动的花费. ...
- HDU 3966 树链剖分+树状数组 模板
Aragorn's Story Time Limit: 10000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- hdu 5274 树链剖分
Dylans loves tree Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Othe ...
随机推荐
- hMailServer+foxmail配置局域网邮件服务器
1.下载hMailServer并安装,请参考以下网址 https://www.hmailserver.org 2.安装foxmail,官网如下: http://www.foxmail.com/ 3.配 ...
- 03-VTK基础概念(2)
3.3 光照 剧场里有各式各样的灯光,三维渲染场景中也一样,可以有多个光照存在.光照和相机是三维渲染场景必备的因素,如果没有指定(像3.1.1_RenderCylinder例子,我们没有给Render ...
- Decompiled .class file,bytecode version:51.0(Java 7) Source for 'Android API 23 Platform' not found
今天在Android Studio中访问Java源码的时候,代码上方出现如下提示: 而且方法体中显示介样内容: throw new RuntimeException("Stub!" ...
- 《linux备份与恢复之二》3.19 dump(文件系统备份)
<Linux指令从初学到精通>第3章文件管理,本章介绍了许多常用命令,如cp.ln.chmod.chown.diff.tar.mv等,因为这些都与文件管理相关,在日常的使用中经常用到,因此 ...
- 在Python脚本中判断Python的版本
引自:http://segmentfault.com/q/1010000000127878 如果是给人读,用 sys.version,如果是给机器比较,用 sys.version_info,如果是判断 ...
- shell脚本批量生成配置文件
如果管理的站点和服务器较多的情况下,每次修改配置文件都相当痛苦.因而想到了用shell脚本来批量生成配置文件和配置数据.下面这个脚本是为了批量生成nagios监控配置文件的一个shell脚本程序.其原 ...
- Linux下配置JDK
下面以CentOS为例,详细说一下Linux下配置JDK的过程 首先按照约定俗成的习惯,将jdk放在/usr/local/java下,首先进入/usr/local然后新建一个目录java 然后我们需要 ...
- 字母排列_next_permutation_字典序函数_待解决
问题 B: 字母排列 时间限制: 1 Sec 内存限制: 64 MB提交: 19 解决: 5[提交][状态][讨论版] 题目描述 当给出一串字符时,我们逐个可以变换其字符,形成新的字符串.假如对这 ...
- codeforces B. Making Sequences is Fun 解题报告
题目链接:http://codeforces.com/problemset/problem/373/B 题目意思:给出w,m和k,需要找出从m开始,可以有多少个连续的数(m+1,m+2,...)(在添 ...
- codeforces B. Jeff and Periods 解题报告
题目链接:http://codeforces.com/problemset/problem/352/B 题目意思:给出一个长度为n的序列 a1, a2, ..., an(序号i,1 <= i ...