思路:树链剖分入门题,我这门入得好苦啊,程序很快写出来了,可是在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. jedis提供的功能

    Sorting(排序) Connection handling(连接池) Commands operating on any kind of values Commands operating on ...

  2. CFileDialog 、CFile 如何进行文件操作 [转]

    如何进行文件操作 [1]显示对话框,取得文件名 CString FilePathName; CFileDialog dlg(TRUE);//TRUE为OPEN对话框,FALSE为SAVE AS对话框 ...

  3. Ext.grid.Panel 数据动态改变后刷新grid

    gridPanel中加载的数据分为两种:一种是本地数据加载,那另一种就是后台数据加载. 在表格中增.删.改.查 是必不可少的. 那么数据动态改变后怎样刷新表格中的数据呢. 一.后台取数据 var gr ...

  4. Segger RTT : Real Time Terminal SRAM 调试解决方法

    http://segger.com/jlink-real-time-terminal.html Real Time Terminal SEGGER's Real Time Terminal (RTT) ...

  5. Object-C基础

    cocoa 类: 传统的写法:Demo.h // // Demo.h // demoClass // // Created by 王 on 13-12-16. // Copyright (c) 201 ...

  6. 我的javascript学习之路(一)对象之基础

    做了2个多月的ajax项目,对js的学习觉得了深入了不少,把自己的一些学习的经验贴出来,希望对初学者有所帮助,也希望大家能指出我的错误之处或者不当之处. javascript 是基于对象的语言,为什么 ...

  7. hdu 4859 海岸线 最小割

    海岸线 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=4859 Description 欢迎来到珠海! 由于土地资源越来越紧张,使得许多海滨城市都只能 ...

  8. volley开源库乱码问题总结(持续更新)

    之前Android开发一直用的是多层封装的Final框架.最近开始学习使用小巧的volley. 在使用该框架的过程中,出现了数次乱码问题,再次做以总结. 分别是返回数据乱码和提交参数乱码两个问题: 一 ...

  9. MySQL通用优化 叶金荣!!!

    http://mp.weixin.qq.com/s?__biz=MjM5NDE0MjI4MA==&mid=208777870&idx=1&sn=6efddd6283e4deb3 ...

  10. open和fopen的区别

    open和fopen的区别: 1.缓冲文件系统缓冲文件系统的特点是:在内存开辟一个“缓冲区”,为程序中的每一个文件使用,当执行读文件的操作时,从磁盘文件将数据先读入内存“缓冲区”, 装满后再从内存“缓 ...