很模板的树链剖分题

注意什么时候用线段树上的标号,什么时候用点的标号。

#pragma comment(linker, "/STACK:102400000,102400000")
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <vector> using namespace std;
const int maxn = ;
int val_pre[maxn],val[maxn],siz[maxn],son[maxn],id[maxn],fa[maxn],top[maxn],dep[maxn];
int topw;
int M,N,P; vector<int> G[maxn]; void dfs_1(int u,int f,int d)
{
siz[u] = ;
fa[u] = f;
dep[u] = d;
son[u] = ;
for(int i=;i<G[u].size();i++)
{
int v = G[u][i];
if(v == f) continue;
dfs_1(v,u,d+);
siz[u] += siz[v];
if(siz[son[u]] < siz[v])
son[u] = v;
}
} void dfs_2(int u,int tp)
{
top[u] = tp;
id[u] = ++topw;
if(son[u]) dfs_2(son[u],tp);
for(int i=;i<G[u].size();i++)
{
int v = G[u][i];
if(v == fa[u]|| v==son[u]) continue;
dfs_2(v,v);
}
} void debug()
{
for(int i=;i<=N;i++)
{
printf("%d siz:%d son:%d dep:%d fa:%d ",i,siz[i],son[i],dep[i],fa[i]);
printf("top:%d id:%d\n",top[i],id[i]);
}
} ////////////////////////////////////////
//Segment Tree #define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
int add[maxn<<];
void push_down(int rt)
{
if(add[rt])
{
add[rt<<] += add[rt];
add[rt<<|] += add[rt];
add[rt] = ;
}
} void build(int l,int r,int rt)
{
add[rt] = ;
if(l == r)
{
add[rt] = val[r];
return ;
}
int m = (l+r)>>;
build(lson);
build(rson);
} void update(int L,int R,int c,int l,int r,int rt)
{
if(L<=l && R>= r)
{
add[rt] += c;
return ;
}
push_down(rt);
int m = (l+r)>>;
if(L<=m) update(L,R,c,lson);
if(R> m) update(L,R,c,rson);
} int query(int x,int l,int r,int rt)
{
if(l == r)
{
return add[rt];
}
push_down(rt);
int m = (l+r)>>;
if(x <= m) query(x,lson);
else query(x,rson);
} void change(int u,int v,int add)
{
int fu = top[u],fv = top[v];
while(fu != fv)
{
//printf("fu:%d u:%d v:%d fv:%d \n",fu,u,v,fv);
if(dep[fu] < dep[fv])
{
swap(u,v);swap(fu,fv);
}
update(id[fu],id[u],add,,topw,);
u = fa[fu];
fu = top[u];
}
if(u == v)
{
update(id[u],id[v],add,,topw,);
return;
}
else{
if(dep[u] > dep[v]) swap(u,v);
update(id[u],id[v],add,,topw,);
return ;
}
} int main()
{
//freopen("input.in","r",stdin);
while(~scanf("%d%d%d",&N,&M,&P))
{
for(int i=;i<=N;i++) scanf("%d",&val_pre[i]);
for(int i=,a,b;i<=M;i++)
{
scanf("%d%d",&a,&b);
G[a].push_back(b);
G[b].push_back(a);
}
topw = ;
dfs_1(,,);
dfs_2(,);
for(int i=;i<=N;i++)
{
val[id[i]] = val_pre[i];
}
build(,topw,);
//debug(); char op[];
int a,b,c; for(int i=;i<P;i++)
{
scanf("%s",op);
if(op[] == 'I')
{
scanf("%d%d%d",&a,&b,&c);
change(a,b,c);
}
else if(op[] == 'D')
{
scanf("%d%d%d",&a,&b,&c);
change(a,b,-c);
}
else if(op[] == 'Q')
{
scanf("%d",&a);
int ans = query(id[a],,topw,);
printf("%d\n",ans);
}
}
for(int i=;i<maxn;i++) G[i].clear();
}
}

HDU3966-Aragorn's Story-树链剖分-点权的更多相关文章

  1. HDU3669 Aragorn's Story 树链剖分 点权

    HDU3669 Aragorn's Story 树链剖分 点权 传送门:http://acm.hdu.edu.cn/showproblem.php?pid=3966 题意: n个点的,m条边,每个点都 ...

  2. hdu3966 Aragorn's Story 树链剖分

    题目传送门 题目大意: 有n个兵营形成一棵树,给出q次操作,每一次操作可以使两个兵营之间的所有兵营的人数增加或者减少同一个数目,每次查询输出某一个兵营的人数. 思路: 树链剖分模板题,讲一下树链剖分过 ...

  3. BZOJ 1036 [ZJOI2008]树的统计Count (树链剖分 - 点权剖分 - 单点权修改)

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1036 树链剖分模版题,打的时候注意点就行.做这题的时候,真的傻了,单词拼错检查了一个多小时 ...

  4. 计蒜客 38229.Distance on the tree-1.树链剖分(边权)+可持久化线段树(区间小于等于k的数的个数)+离散化+离线处理 or 2.树上第k大(主席树)+二分+离散化+在线查询 (The Preliminary Contest for ICPC China Nanchang National Invitational 南昌邀请赛网络赛)

    Distance on the tree DSM(Data Structure Master) once learned about tree when he was preparing for NO ...

  5. POJ3237 Tree 树链剖分 边权

    POJ3237 Tree 树链剖分 边权 传送门:http://poj.org/problem?id=3237 题意: n个点的,n-1条边 修改单边边权 将a->b的边权取反 查询a-> ...

  6. POJ2763 Housewife Wind 树链剖分 边权

    POJ2763 Housewife Wind 树链剖分 边权 传送门:http://poj.org/problem?id=2763 题意: n个点的,n-1条边,有边权 修改单边边权 询问 输出 当前 ...

  7. HDU 3966 Aragorn's Story 树链剖分+树状数组 或 树链剖分+线段树

    HDU 3966 Aragorn's Story 先把树剖成链,然后用树状数组维护: 讲真,研究了好久,还是没明白 树状数组这样实现"区间更新+单点查询"的原理... 神奇... ...

  8. Aragorn's Story 树链剖分+线段树 && 树链剖分+树状数组

    Aragorn's Story 来源:http://www.fjutacm.com/Problem.jsp?pid=2710来源:http://acm.hdu.edu.cn/showproblem.p ...

  9. HDU 3966 Aragorn's Story(树链剖分)(线段树区间修改)

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

  10. Hdu 3966 Aragorn's Story (树链剖分 + 线段树区间更新)

    题目链接: Hdu 3966 Aragorn's Story 题目描述: 给出一个树,每个节点都有一个权值,有三种操作: 1:( I, i, j, x ) 从i到j的路径上经过的节点全部都加上x: 2 ...

随机推荐

  1. @vue/cli 3 运行支持报错 socket

    问题 /sockjs-node/info 无限报错 解决方案 原因是相关代理端不支持 ws,因此需要在代理处关闭 ws,即 ws: false,如下: vue.config.js const ds_p ...

  2. for 循环 以及基本的数据类型

    for 循环: for 关键字 i 变量(此处可以更改 更改规则参考变量命名规则) in 关键字 可迭代对象 (想要循环谁就放谁,注意:数字除外 因为数字不可迭代) for 循环内可以进行任意操作,可 ...

  3. Python学习第五篇——如何访问字典

    # the example_1 aim to tell how to use dctionary,and how to access list or dictionary infos={"f ...

  4. Linux登录MySQL时出现 Can't connect to local MySQL server through socket '/tmp/mysql.sock'解决方法

    在Linux上登录MySQL时出现如下提示,如下图: 通过查找资料了解到: MySQL有两种连接方式: (1)TCP/IP (2)socket 对mysql.sock来说,其作用是程序与mysqlse ...

  5. CNZZ友盟访问明细的采集办法

    www.cnzz.com是中文网站统计分析平台,很多站长需要获取网站提供的访问明细,以做分析. 直接采集这个网站的数据相当麻烦,通过浏览器或者fiddlercore就简单多了. 2.0新版,通过浏览器 ...

  6. Python_程序实现发红包

    发红包 200块钱  20个红包 将200块随机分成20份 基础版本: import random ret = random.sample(range(1, 200 * 100), 19) ret = ...

  7. rest-framework频率组件

    throttle(访问频率)组件 1.局部视图throttle from rest_framework.throttling import BaseThrottle VISIT_RECORD={} c ...

  8. Shell脚本2

      5 Shell传递参数 我们可以在执行 Shell 脚本时,向脚本传递参数, 脚本内获取参数的格式为:$n.n 代表一个数字,1 为执行脚本的第一个参数,2 为执行脚本的第二个参数,以此类推…… ...

  9. JSLinux

    JSLinuxhttps://bellard.org/jslinux/vm.html?url=https://bellard.org/jslinux/win2k.cfg&mem=192& ...

  10. MySQL 5.7默认ONLY_FULL_GROUP_BY语义介绍

    mysql 5.7版本 出现 ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corre ...