思路:树链剖分入门题,我这门入得好苦啊,程序很快写出来了,可是在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. 数据库 SQL :有关 NULL 值引发 TRUE、FALSE、UNKNOW 三值逻辑

    在 Java.C# 中,相信如果是 boolean 类型值,只有两种选择 true.false.然而,在 SQL 查询中,NULL 值的引入,使得新增了 UNKNOW ,因此,就产生了 TRUE.FA ...

  2. 用CSS让字体在一行内显示不换行(收藏)

    当一行文字超过DIV或者Table的宽度的时候,浏览器中默认是让它换行显示的,如果不想让他换行要怎么办呢? 用CSS让文字在一行内显示不换行的方法   一般的文字截断(适用于内联与块): .text- ...

  3. listView divider marginLeft marginRight

    要实现这样的效果: 新建drawable  用inset 进行实现.代码如下: <?xml version="1.0" encoding="utf-8"? ...

  4. C#实现XML文件数据库存储

    C#实现文件数据库 http://www.cnblogs.com/gaochundong/archive/2013/04/24/csharp_file_database.html#3100076 应用 ...

  5. OpenCV 图像处理学习笔记(一)

    解读IplImage结构 typedef struct _IplImage { int nSize;                    /* IplImage大小 */ int ID;       ...

  6. \r,\n,\t

    \r:回车符,返回到这一行的开头,return的意思. \n:换行符,到下一行的同一位置,纵坐标相同,new line的意思. \t:制表符,为了在不使用表格的情况下,上下对齐,table的意思. E ...

  7. Codeforces Round #146 (Div. 1) B. Let's Play Osu! dp

    B. Let's Play Osu! 题目连接: http://www.codeforces.com/contest/235/problem/B Description You're playing ...

  8. ExtJS grid tableGrid study

    Q:  How to color the text in the grid Try: http://dev.sencha.com/playpen/docs/output/Ext.grid.TableG ...

  9. 【ArcGIS 10.2新特性】Portal for ArcGIS新特性

    1.概述 经过各版本的积累和更新,Portal for ArcGIS在ArcGIS10.2中以正式产品的形态加入到了ArcGIS系列产品线中.它有3个主要定位:协同管理平台.在线制图平台以及内容管理平 ...

  10. GPS原始经纬度转百度经纬度

    protected void runTest() throws Throwable { try { BaiduLocation bl = new BaiduLocation(); bl.gpsx = ...