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

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<set>
#include<map>
#include<cmath>
#include<queue>
#include<cstdio>
#include<vector>
#include<string>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#define pb push_back
#define mp make_pair
#define Maxn 50010
#define Maxm 200010
#define LL __int64
#define Abs(x) ((x)>0?(x):(-x))
#define lson(x) (x<<1)
#define rson(x) (x<<1|1)
#define inf 100000
#define lowbit(x) (x&(-x))
#define clr(x,y) memset(x,y,sizeof(x))
#define Mod 1000000007
using namespace std;
int head[Maxn],vi[Maxn],dep[Maxn],w[Maxn],top[Maxn],son[Maxn],sz[Maxn],fa[Maxn],e,id;
int num[Maxn];
struct Edge{
int u,v,next;
}edge[Maxn*];
struct Tree{
int l,r,c;
int mid(){
return (l+r)>>;
}
}tree[Maxn*];
void init()
{
clr(head,-);clr(vi,);
e=;id=;
}
void add(int u,int v)
{
edge[e].u=u,edge[e].v=v,edge[e].next=head[u],head[u]=e++;
}
void BuildTree(int l,int r,int po)
{
tree[po].l=l,tree[po].r=r,tree[po].c=;
if(l==r)
return ;
int mid=tree[po].mid();
BuildTree(l,mid,lson(po));
BuildTree(mid+,r,rson(po));
}
void down(int po)
{
tree[lson(po)].c+=tree[po].c;
tree[rson(po)].c+=tree[po].c;
tree[po].c=;
}
void update(int l,int r,int c,int po)
{
if(l<=tree[po].l&&tree[po].r<=r){
tree[po].c+=c;
return ;
}
down(po);
int mid=tree[po].mid();
if(r<=mid)
update(l,r,c,lson(po));
else if(l>=mid+)
update(l,r,c,rson(po));
else {
update(l,mid,c,lson(po));
update(mid+,r,c,rson(po));
}
}
int getans(int i,int po)
{
if(tree[po].l==tree[po].r){
return tree[po].c;
}
down(po);
int mid=tree[po].mid();
if(i<=mid)
return getans(i,lson(po));
else
return getans(i,rson(po));
}
void dfs(int u)
{
vi[u]=;
int i,v;
son[u]=,sz[u]=;
for(i=head[u];i!=-;i=edge[i].next){
v=edge[i].v;
if(vi[v]) continue;
dep[v]=dep[u]+;
fa[v]=u;
dfs(v);
if(sz[v]>sz[son[u]])son[u]=v;
sz[u]+=sz[v];
}
}
void build(int u,int ti)
{
int i,v;
w[u]=++id;top[u]=ti;vi[u]=;
if(son[u]) build(son[u],ti);
for(i=head[u];i!=-;i=edge[i].next){
v=edge[i].v;
if(vi[v]||v==son[u]) continue;
build(v,v);
}
}
void calc(int u,int v,int c)
{
int f1=top[u],f2=top[v];
while(f1!=f2){
if(dep[f1]<dep[f2]){
swap(f1,f2),swap(u,v);
}
update(w[f1],w[u],c,);
u=fa[f1];f1=top[u];
}
if(dep[u]>dep[v])
swap(u,v);
update(w[u],w[v],c,);
return ;
}
int main()
{
int n,m,p,i,j,u,v,val;
char str[];
while(scanf("%d%d%d",&n,&m,&p)!=EOF){
init();
for(i=;i<=n;i++)
scanf("%d",num+i);
for(i=;i<=m;i++){
scanf("%d%d",&u,&v);
add(u,v);
add(v,u);
}
dfs();
clr(vi,);
build(,);
BuildTree(,id,);
int ans;
for(i=;i<=p;i++){
scanf("%s",str);
if(str[]=='I'){
scanf("%d%d%d",&u,&v,&val);
calc(u,v,val);
}
else if(str[]=='D'){
scanf("%d%d%d",&u,&v,&val);
calc(u,v,-val);
}else {
scanf("%d",&u);
ans=getans(w[u],);
printf("%d\n",num[u]+ans);
}
}
}
return ;
}

hdu 3966 树链剖分的更多相关文章

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

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

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

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

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

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

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

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

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

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

  6. hdu 5893 (树链剖分+合并)

    List wants to travel Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/O ...

  7. hdu 5052 树链剖分

    Yaoge’s maximum profit Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/ ...

  8. hdu 4897 树链剖分(重轻链)

    Little Devil I Time Limit: 16000/8000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others ...

  9. hdu 5274 树链剖分

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

随机推荐

  1. cocos2dx使用TiledMap创建斜45度地图场景

    做游戏,场景是一个很重要的部分,如果缺少这一步,很难做出好的游戏,对于cocos2dx来说,有很多2D的地图编辑器可以用,效果都还可以,其中Tiled是支持的比较好的,它支持Tiled编辑出来的几种模 ...

  2. poj 3134 Power Calculus(IDA*)

    题目大意: 用最小的步数算出  x^n 思路: 直接枚举有限步数可以出现的所有情况. 然后加一个A*   就是如果这个数一直平方  所需要的步骤数都不能达到最优   就剪掉 #include < ...

  3. (剑指Offer)面试题30:最小的k个数

    题目: 输入n个整数,找出其中最小的K个数.例如输入4,5,1,6,2,7,3,8这8个数字,则最小的4个数字是1,2,3,4,. 思路: 1.排序 把输入的n个整数排序,然后取前k个数: 时间复杂度 ...

  4. document.body为null的问题

    虽然body是JS中的DOM技术中所有浏览器支持的属性,但在我们的代码编写中,还是会碰到document.is null问题 例如:我们可以使用alert(document.body);的时候,就会提 ...

  5. PP常见数据表

    Table表    Short text短文本 AFFL    Work order sequence加工单顺序 AFFT    Order process instructions订单-流程指令 A ...

  6. hadoop集群扩展

    一.先改动master机上的host文件,新增内容 10.61.6.180 slaves15 二.改动master机上hadoop安装文件夹下的conf中的slaves文件.新增内容 slaves15 ...

  7. VS2012无法安装cocos2d-x-2.1.4 解决方法及VS2012新建coco2d-x项目(一)

    转自:http://www.cnblogs.com/wangpei/admin/EditPosts.aspx?opt=1 (注:此方法是可行,仅供参考,建议大家直接看我的 一见命令解决vs安装并创建c ...

  8. BootStrap 的随笔一二三

    正在学习中待补充 ..为自己日后查看留存,不喜误看 特别鸣谢,小霖,小涛,小海... Bootstrap总结 可实现的页面效果 导航.格栅分为12份,焦点图,按钮,响应式,下拉按钮,选项卡等 boot ...

  9. 基数树(radix tree)

    原文   基数(radix)树 Linux基数树(radix tree)是将指针与long整数键值相关联的机制,它存储有效率,并且可快速查询,用于指针与整数值的映射(如:IDR机制).内存管理等.ID ...

  10. DbHelperSQL 判断数据库表结构公用方法

    #region 公用方法        /// <summary>        /// 判断是否存在某表的某个字段        /// </summary>        ...