题目链接: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(树链剖分+点修改+点查询)的更多相关文章

  1. hdu 3966 Aragorn's Story(树链剖分+区间修改+单点查询)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3966 题意:给一棵树,并给定各个点权的值,然后有3种操作: I C1 C2 K: 把C1与C2的路径上 ...

  2. HDU 3966 /// 树链剖分+树状数组

    题意: http://acm.hdu.edu.cn/showproblem.php?pid=3966 给一棵树,并给定各个点权的值,然后有3种操作: I x y z : 把x到y的路径上的所有点权值加 ...

  3. HDU 3966 (树链剖分+线段树)

    Problem Aragorn's Story (HDU 3966) 题目大意 给定一颗树,有点权. 要求支持两种操作,将一条路径上的所有点权值增加或减少ai,询问某点的权值. 解题分析 树链剖分模板 ...

  4. hdu 3966(树链剖分+线段树区间更新)

    传送门:Problem 3966 https://www.cnblogs.com/violet-acmer/p/9711441.html 学习资料: [1]线段树区间更新:https://blog.c ...

  5. hdu 3966 树链剖分

    思路:树链剖分入门题,我这门入得好苦啊,程序很快写出来了,可是在LCA过程中把update函数里的左右边界位置写反了,一直RE到死. #pragma comment(linker, "/ST ...

  6. HDU 3966 树链剖分后线段树维护

    题意: 一棵树, 操作1.$path(a,b)$之间的点权$+k$ 操作2.单点查询 题解: 树链剖分即可,注意代码细节,双向映射 主要是记录一下板子 #include <string.h> ...

  7. POJ 2763 (树链剖分+边修改+边查询)

    题目链接:http://poj.org/problem?id=2763 题目大意:某人初始在s点.有q次移动,每次移动沿着树上一条链,每经过一条边有一定花费,这个花费可以任意修改.问每次移动的花费. ...

  8. HDU 3966 树链剖分+树状数组 模板

    Aragorn's Story Time Limit: 10000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  9. hdu 5274 树链剖分

    Dylans loves tree Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Othe ...

随机推荐

  1. C语言register关键字—最快的关键字 ---------------转自http://blog.sina.com.cn/s/blog_6a1837e90101128k.html

    register:这个关键字请求编译器尽可能的将变量存在CPU内部寄存器中,而不是通过内存寻址访问,以提高效率.注意是尽可能,不是绝对.你想想,一个CPU 的寄存器也就那么几个或几十个,你要是定义了很 ...

  2. Coursera台大机器学习技法课程笔记01-linear hard SVM

    极其淡腾的一学期终于过去了,暑假打算学下台大的这门机器学习技法. 第一课是对SVM的介绍,虽然之前也学过,但听了一次感觉还是很有收获的.这位博主总结了个大概,具体细节还是 要听课:http://www ...

  3. Linux 的shell 字符串截取很有用。有八种方法。

    一 Linux 的字符串截取很有用.有八种方法. 假设有变量 var=http://www.linuxidc.com/123.htm 1  # 号截取,删除左边字符,保留右边字符. echo ${va ...

  4. 把sql server 2000的用户表的所有者改成dbo

    怎么样把sql server 2000的用户表的所有者,改成dbo,而不是用户名. 推荐使用下面介绍的第二种方法,执行以下查询便可以了.sp_configure 'allow updates','1' ...

  5. 什么是mixin

    转自:http://guangboo.org/2013/01/28/python-mixin-programming http://en.wikipedia.org/wiki/Mixin http:/ ...

  6. Resumable uploads over HTTP. Protocol specification

    Valery Kholodkov <valery@grid.net.ru>, 2010 1. Introduction This document describes applicatio ...

  7. 【Python】Django 支持 restful 风格 url

    URL通配符示例: url(r'^file_download/(?P<filename>(.)*)$', views.FILE_DOWNLOAD_VIEW.as_view()), 代码示例 ...

  8. 深入了解PooledConnectionFactory CachingConnectionFactory Sin

    深入理解PooledConnectionFactory CachingConnectionFactory SingleConnectionFactory PooledConnectionFactory ...

  9. Java for LeetCode 065 Valid Number

    Validate if a given string is numeric. Some examples: "0" => true " 0.1 " =&g ...

  10. Java for LeetCode 029 Divide Two Integers

    Divide two integers without using multiplication, division and mod operator. If it is overflow, retu ...