HDU3966-Aragorn's Story-树链剖分-点权
很模板的树链剖分题
注意什么时候用线段树上的标号,什么时候用点的标号。
#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-树链剖分-点权的更多相关文章
- HDU3669 Aragorn's Story 树链剖分 点权
HDU3669 Aragorn's Story 树链剖分 点权 传送门:http://acm.hdu.edu.cn/showproblem.php?pid=3966 题意: n个点的,m条边,每个点都 ...
- hdu3966 Aragorn's Story 树链剖分
题目传送门 题目大意: 有n个兵营形成一棵树,给出q次操作,每一次操作可以使两个兵营之间的所有兵营的人数增加或者减少同一个数目,每次查询输出某一个兵营的人数. 思路: 树链剖分模板题,讲一下树链剖分过 ...
- BZOJ 1036 [ZJOI2008]树的统计Count (树链剖分 - 点权剖分 - 单点权修改)
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1036 树链剖分模版题,打的时候注意点就行.做这题的时候,真的傻了,单词拼错检查了一个多小时 ...
- 计蒜客 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 ...
- POJ3237 Tree 树链剖分 边权
POJ3237 Tree 树链剖分 边权 传送门:http://poj.org/problem?id=3237 题意: n个点的,n-1条边 修改单边边权 将a->b的边权取反 查询a-> ...
- POJ2763 Housewife Wind 树链剖分 边权
POJ2763 Housewife Wind 树链剖分 边权 传送门:http://poj.org/problem?id=2763 题意: n个点的,n-1条边,有边权 修改单边边权 询问 输出 当前 ...
- HDU 3966 Aragorn's Story 树链剖分+树状数组 或 树链剖分+线段树
HDU 3966 Aragorn's Story 先把树剖成链,然后用树状数组维护: 讲真,研究了好久,还是没明白 树状数组这样实现"区间更新+单点查询"的原理... 神奇... ...
- Aragorn's Story 树链剖分+线段树 && 树链剖分+树状数组
Aragorn's Story 来源:http://www.fjutacm.com/Problem.jsp?pid=2710来源:http://acm.hdu.edu.cn/showproblem.p ...
- HDU 3966 Aragorn's Story(树链剖分)(线段树区间修改)
Aragorn's Story Time Limit: 10000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- Hdu 3966 Aragorn's Story (树链剖分 + 线段树区间更新)
题目链接: Hdu 3966 Aragorn's Story 题目描述: 给出一个树,每个节点都有一个权值,有三种操作: 1:( I, i, j, x ) 从i到j的路径上经过的节点全部都加上x: 2 ...
随机推荐
- [程序员的业余生活]一周读完《高效能人士的七个习惯》Day1:这是不是一碗鸡汤?
提出问题 今天突然想聊聊最近对职场的一些感悟. 这段时间,小端一直在思考一个问题:作为一个程序员,怎么才能成为团队的核心? 还记得刚入职场那几年,小端一直觉得,技术过硬,经验丰富,敢打敢拼,就是答案. ...
- elasticsearch简单操作(二)
让我们建立一个员工目录,假设我们刚好在Megacorp工作,这时人力资源部门出于某种目的需要让我们创建一个员工目录,这个目录用于促进人文关怀和用于实时协同工作,所以它有以下不同的需求:1.数据能够包含 ...
- windows安装Redis和客户端
一.Windows安装Redis 1.下载安装包Redis-x64-3.0.504.zip到本地 2.解压 3.打开CMD,切换到解压后的redis目录,然后 C:\Users\Administrat ...
- Python练习-2
#1.使用while循环输入 1 2 3 4 5 6 8 9 10 count = 0 while count < 10: count += 1 # count = count + 1 if c ...
- 对Vuejs框架原理名词解读
渐进式()+虚拟Dom: vue-cli 遍历Dom:先序遍历DOM树的5种方法! 三层架构+m v c +mvp+m v vm()+MVC,MVP 和 MVVM 的图示 剖析vue MVVM实现原理 ...
- 多线程系列之七:Read-Write Lock模式
一,Read-Write Lock模式 在Read-Write Lock模式中,读取操作和写入操作是分开考虑的.在执行读取操作之前,线程必须获取用于读取的锁.在执行写入操作之前,线程必须获取用于写入的 ...
- 【问题解决方案】之 关于某江加密视频swf专用播放器仍无法播放的问题
前言: 从pt上下载了一些语言学习的视频之后一直打不开,百度谷歌了若干种方法仍然无解.无奈放弃. 某日从百度知道里又看到一个方法,试了一下,居然灵了.呜呼哀哉.赶紧记下来. 原方法链接:https:/ ...
- Migrate MySQL database using dump and restore
kaorimatz/mysqldump-loader: Load a MySQL dump file using LOAD DATA INFILEhttps://github.com/kaorimat ...
- linuxmint 搜狗输入法安装
1.下载搜狗输入法linux安装包 2.进入安装包目录终端键入 dpkg -i [软件包名字] 3.设置语言选项中选择fcitx 4.重启电脑
- Day5-1 面向对象和面向过程
摘要: 类的定义 类的增删改查 对象的增删改查 对象的查找和绑定 面向对象和面向过程的区别: 1.面向过程就像是工厂的流水线,按部就班的有序的工作. 优点:把复杂的问题简单化 缺点:可扩展性差.一个步 ...