【bzoj3589】动态树 树链剖分+树链的并
题解:
树链剖分是显然的
问题在于求树链的并
比较简单的方法是
用线段树打标记覆盖,查询标记区间大小
Qlog^2n
代码:
#include <bits/stdc++.h>
using namespace std;
#define IL inline
#define rint register int
#define rep(i,h,t) for (rint i=h;i<=t;i++)
#define dep(i,t,h) for (rint i=t;i>=h;i--)
const int N=3e5;
const int N1=N*;
const int INF=1e9;
int n,l,cnt,dfn[N],size[N],fa[N],top[N],son[N],head[N],dep[N];
struct re{
int a,b;
}a[N*];
void arr(int x,int y)
{
a[++l].a=head[x];
a[l].b=y;
head[x]=l;
}
void dfs(int x,int y)
{
fa[x]=y; size[x]=;
int u=head[x];
dep[x]=dep[y]+;
while (u)
{
int v=a[u].b;
if (v!=y)
{
dfs(v,x);
if (size[v]>size[son[x]]) son[x]=v;
size[x]+=size[v];
}
u=a[u].a;
}
}
void dfs2(int x,int y)
{
dfn[x]=++cnt; top[x]=y;
if (!son[x]) return;
dfs2(son[x],y);
int u=head[x];
while (u)
{
int v=a[u].b;
if (v!=fa[x]&&v!=son[x]) dfs2(v,v);
u=a[u].a;
}
}
struct sgt{ int sum[N1],sum1[N1],lazy[N1],lazy1[N1];
#define mid ((h+t)/2)
IL void clear()
{
lazy1[]=-; sum1[]=;
}
IL int query()
{
return sum1[];
}
IL void down(int x,int h,int t)
{
if (lazy[x])
{
sum[x*]+=(mid-h+)*lazy[x];
sum[x*+]+=(t-mid)*lazy[x];
lazy[x*]+=lazy[x]; lazy[x*+]+=lazy[x];
lazy[x]=;
}
if (lazy1[x])
{
lazy1[x*]=lazy1[x*+]=lazy1[x];
if (lazy1[x]==) sum1[x*]=sum[x*],sum1[x*+]=sum[x*+];
else sum1[x*]=sum1[x*+]=;
lazy1[x]=;
}
}
IL void updata(int x)
{
sum[x]=sum[x*]+sum[x*+];
sum1[x]=sum1[x*]+sum1[x*+];
}
void change(int x,int h,int t,int h1,int t1,int k)
{
if (h1<=h&&t<=t1)
{
lazy[x]+=k; sum[x]+=(t-h+)*k; return;
}
down(x,h,t);
if (h1<=mid) change(x*,h,mid,h1,t1,k);
if (mid<t1) change(x*+,mid+,t,h1,t1,k);
updata(x);
}
void push(int x,int h,int t,int h1,int t1)
{
if (h1<=h&&t<=t1)
{
lazy1[x]=; sum1[x]=sum[x]; return;
}
down(x,h,t);
if (h1<=mid) push(x*,h,mid,h1,t1);
if (mid<t1) push(x*+,mid+,t,h1,t1);
updata(x);
}
}S;
void change(int x,int y)
{
int kk=dfn[x];
S.change(,,n,kk,kk+size[x]-,y);
}
void query(int x,int y)
{
int f1=top[x],f2=top[y];
while (top[x]!=top[y])
{
if (dep[f1]<dep[f2]) swap(f1,f2),swap(x,y);
S.push(,,n,dfn[f1],dfn[x]);
x=fa[f1]; f1=top[x];
}
if (dep[x]<dep[y]) swap(x,y);
S.push(,,n,dfn[y],dfn[x]);
}
int main()
{
freopen("1.in","r",stdin);
freopen("1.out","w",stdout);
ios::sync_with_stdio(false);
cin>>n;
rep(i,,n-)
{
int x,y; cin>>x>>y;
arr(x,y); arr(y,x);
}
dfs(,);
dfs2(,);
int m;
cin>>m;
rep(i,,m)
{
int kk,x,y,p;
cin>>kk;
if (!kk)
{
cin>>x>>y;
change(x,y);
} else
{
cin>>p;
rep(j,,p)
{
cin>>x>>y;
query(x,y);
}
int ans=S.query();
if (ans<) ans+=<<;
cout<<ans<<endl;
S.clear();
}
}
return ;
}
【bzoj3589】动态树 树链剖分+树链的并的更多相关文章
- hdu 3966 Aragorn's Story(树链剖分+树状数组/线段树)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3966 题意: 给出一棵树,并给定各个点权的值,然后有3种操作: I C1 C2 K: 把C1与C2的路 ...
- Aragorn's Story 树链剖分+线段树 && 树链剖分+树状数组
Aragorn's Story 来源:http://www.fjutacm.com/Problem.jsp?pid=2710来源:http://acm.hdu.edu.cn/showproblem.p ...
- 洛谷 P3384 【模板】树链剖分-树链剖分(点权)(路径节点更新、路径求和、子树节点更新、子树求和)模板-备注结合一下以前写的题目,懒得写很详细的注释
P3384 [模板]树链剖分 题目描述 如题,已知一棵包含N个结点的树(连通且无环),每个节点上包含一个数值,需要支持以下操作: 操作1: 格式: 1 x y z 表示将树从x到y结点最短路径上所有节 ...
- 4.12 省选模拟赛 LCA on tree 树链剖分 树状数组 分析答案变化量
LINK:duoxiao OJ LCA on Tree 题目: 一道树链剖分+树状数组的神题. (直接nQ的暴力有50. 其实对于树随机的时候不难想到一个算法 对于x的修改 暴力修改到根. 对于儿子的 ...
- (简单) POJ 3321 Apple Tree,树链剖分+树状数组。
Description There is an apple tree outside of kaka's house. Every autumn, a lot of apples will grow ...
- Codeforces Round #425 (Div. 2) Problem D Misha, Grisha and Underground (Codeforces 832D) - 树链剖分 - 树状数组
Misha and Grisha are funny boys, so they like to use new underground. The underground has n stations ...
- HDU 3966 Aragorn's Story 树链剖分+树状数组 或 树链剖分+线段树
HDU 3966 Aragorn's Story 先把树剖成链,然后用树状数组维护: 讲真,研究了好久,还是没明白 树状数组这样实现"区间更新+单点查询"的原理... 神奇... ...
- bzoj1146整体二分+树链剖分+树状数组
其实也没啥好说的 用树状数组可以O(logn)的查询 套一层整体二分就可以做到O(nlngn) 最后用树链剖分让序列上树 #include<cstdio> #include<cstr ...
- HDU 5044 (树链剖分+树状数组+点/边改查)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5044 题目大意:修改链上点,修改链上的边.查询所有点,查询所有边. 解题思路: 2014上海网赛的变 ...
随机推荐
- u3d 2D开发学习
http://gad.qq.com/article/detail/45365 https://blog.csdn.net/s556699/article/details/55224830 UI层设计 ...
- 解决Windows 10笔记本接显示器分屏后没有声音的问题
Windows 10 版本号:17763.292 1.首先右键点击任务栏托盘中的[扬声器]图标,选择[声音],如下图所示. 2.选择[播放],然后选择[扬声器],再点击[设为默认值],如下所示. 3. ...
- 专题:DP杂题1
A POJ 1018 Communication System B POJ 1050 To the Max C POJ 1083 Moving Tables D POJ 1125 Stockbroke ...
- oracle11g自动内存管理
Oracle一直不停的在为Orace数据库的自动化管理努力着,11G中的自动内存管理是Oracle数据库中又一新的里程碑,通过新参数MEMORY_TARGET 来代替PGA和SGA的配置,ORACLE ...
- 转-OWASP CSRFGuard使用细节
版权声明:不存在一劳永逸的技术 只存在不断学习的人.本文为博主原创文章,未经博主允许不得转载.交流联系QQ:1120121072 https://blog.csdn.net/u013474568/ar ...
- Java链接DB2的4种基本类型【转】
原文链接:http://doc.chinaunix.net/java/201002/776480.shtml 第一种:目前IBM一直都没有提供 TYPE 1的JDBC驱动程序. 第二种:类型2驱动:C ...
- iOS post提交数据有嵌套数组的处理方法
2017年11月21日17:11:43 解决办法, 修改iOS框架里的代码: http://www.jianshu.com/p/130daa0c2fe7 确实有效, 要不然, 内层的每一个键值对都会 ...
- C/C++ 获取文件大小
在C语言中测试文件的大小,主要使用二个标准函数. 1.fseek 函数原型:int fseek ( FILE * stream, long int offset, int origin ); 参数说明 ...
- HDU 1250
简单大数 (要压位,不然会超内存) #include<iostream> #include<cstdio> #include<cstring> #include&l ...
- python学习第2天
03 pycharm使用04 格式化输出05 while循环 why: 吃饭睡觉上课, 地球绕着太阳公转,单曲循环,列表循环. what: while how: while 条件: 循环体 where ...