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 ...
随机推荐
- 从统计局采集最新的省市区镇数据,用js在浏览器中运行 V2
本文描述的是对国家统计局于2019-01-31发布的<2018年统计用区划代码和城乡划分代码(截止2018年10月31日)>的采集. 相对于用于和采集2016版.2017版的js代码做了比 ...
- HDU - 1255 扫描线+离散化进阶
这道题最开始我以为和HDU - 1542 那道题一样,只需要把cover次数改成2次即可,但是后面仔细一想,我们需要求的是覆盖次数大于等于2次的,这样的话,我们需要维护两个长度,HDU-1542 由于 ...
- C. Anton and Fairy Tale
链接 [https://codeforces.com/contest/785/problem/C] 题意 初始时有n,第1天先加m开始吃1,但总的不能超过n,第i天先加m开始吃i(如果不够或刚好就吃完 ...
- (第十三周)Final阶段成员贡献分
项目名:食物链教学工具 组名:奋斗吧兄弟 组长:黄兴 组员:李俞寰.杜桥.栾骄阳.王东涵 个人贡献分=基础分+表现分 基础分=5*5*0.5/5=2.5 成员得分如下: 成员 基础分 表现分 个人贡献 ...
- iOS开发——无网占位图的实现
https://www.jianshu.com/p/d537393fe247 https://github.com/wyzxc/CQPlaceholderViewhttps://github.com/ ...
- pinpoint vs druid
主流Java数据库连接池分析(C3P0,DBCP,TomcatPool,BoneCP,Druid) - ppjj - 博客园 https://www.cnblogs.com/nizuimeiabc1/ ...
- PostgreSQL 安装了contrib 之后 登录失败的问题
1. 自己之前只是安装了 pg 10.6 2. 开发同事 需要用到 一个extensions 叫做 uuid-ossp 3. 执行报错 详情见昨天的blog 4. 然后执行了升级操作 结果 pg10 ...
- liunx安装nginx
参考 https://blog.csdn.net/dyllove98/article/details/41120789 1,去官网下载最新的包 官网地址:http://nginx.org/downlo ...
- 对数log
a的x次方等于N(a>0,且a不等于1),那么数x叫做以a为底N的对数(logarithm),记作x=logaN.其中,a叫做对数的底数,N叫做真数.
- windos安装maven
1.下载好maven压缩包,并解压到相应位置,本次安装在D: 2.配置环境变量 MAVEN_HOME=D:\apache-maven-3.0.5 path=%MAVEN_HOME% 3.生成maven ...