Codevs 4633 [Mz]树链剖分练习
4633 [Mz]树链剖分练习
时间限制: 1 s 空间限制: 64000 KB 题目等级 : 大师 Master
给定一棵结点数为n的树,初始点权均为0,有依次q个操作,每次操作有三个参数a,b,c,当a=1时,表示给b号结点到c号结点路径上的所有点(包括b,c,下同)权值都增加1,当a=2时,表示询问b号结点到c号结点路径上的所有点权值之和。
第一行,一个正整数n。
接下来n-1行,每行一对正整数x,y,表示x号结点和y号结点之间有一条边。
第n+1行,一个正整数q。
最后q行,每行一组正整数a,b,c,表示操作的三个参数。b和c可能相等。
保证数据都是合法的。
若干行,每行一个非负整数表示答案。
5
1 2
2 3
1 4
2 5
5
1 4 5
2 1 5
1 1 3
2 5 3
2 4 3
3
4
6
共有10个测试点,对于第i个测试点,当1<=i<=4时,n=q=10^i,当5<=i<=10时,n=q=10000*i。
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
#define maxn 10000*10
struct Edge{ int to,next,value; }e[maxn*+];
struct Node{ int l,r,sum,lazy; }tre[maxn*];
int dep[maxn],pos[maxn],size[maxn],son[maxn],fa[maxn];
int top[maxn],head[maxn],tot=,n,visx;
void Add_Edge(int u,int v){
e[++tot].to=v;e[tot].next=head[u];
head[u]=tot;
}
void DFS_First(int now,int pre,int deepth){
fa[now]=pre;size[now]=;dep[now]=deepth;
for(int i=head[now];i;i=e[i].next){
int v=e[i].to;
if(v!=pre&&!dep[v]){
DFS_First(v,now,deepth+);
size[now]+=size[v];
if(!son[now]||size[son[now]]<size[v])
son[now]=v;
}
}
}
void DFS_Second(int now,int Top){
pos[now]=++visx;top[now]=Top;
if(son[now])DFS_Second(son[now],Top);
for(int i=head[now];i;i=e[i].next){
int v=e[i].to;
if(v!=son[now]&&v!=fa[now])DFS_Second(v,v);
}
}
void Build(int now,int l,int r){
tre[now].l=l;tre[now].r=r;
if(l==r)return;
int mid=(l+r)>>;
Build(now<<,l,mid);Build(now<<|,mid+,r);
}
void down(int now){
int k=tre[now].lazy;
tre[now<<].lazy+=k;tre[now<<|].lazy+=k;
tre[now].lazy=;
tre[now<<].sum+=k*(tre[now<<].r-tre[now<<].l+);
tre[now<<|].sum+=k*(tre[now<<|].r-tre[now<<|].l+);
}
int query(int now,int l,int r){
if(l<=tre[now].l&&tre[now].r<=r)return tre[now].sum;
if(tre[now].lazy)down(now);
int ans=,mid=(tre[now].l+tre[now].r)>>;
if(mid>=l)ans+=query(now<<,l,r);
if(mid<r)ans+=query(now<<|,l,r);
tre[now].sum=tre[now<<].sum+tre[now<<|].sum;
return ans;
}
int QuerySum(int u,int v){
int ans=;
while(top[u]!=top[v]){
if(dep[top[u]] < dep[top[v]])swap(u,v);
ans+=query(,pos[top[u]],pos[u]);
u=fa[top[u]];
}
if(dep[u]>dep[v])swap(u,v);
ans+=query(,pos[u],pos[v]);
return ans;
}
void UpDate(int now,int l,int r){
if(l<=tre[now].l&&tre[now].r<=r){
tre[now].lazy++;
tre[now].sum+=tre[now].r-tre[now].l+;
return ;
}
if(tre[now].lazy)down(now);
int mid=(tre[now].l+tre[now].r)/;
if(mid>=l)UpDate(now<<,l,r);
if(mid<r)UpDate(now<<|,l,r);
tre[now].sum=tre[now<<].sum+tre[now<<|].sum;
return ;
}
void Change(int u,int v){
while(top[u]!=top[v]){
if(dep[top[u]]<dep[top[v]])swap(u,v);
UpDate(,pos[top[u]],pos[u]);
u=fa[top[u]];
}
if(dep[u]>dep[v])swap(u,v);
UpDate(,pos[u],pos[v]);
}
int main(){
scanf("%d",&n);
int q,x,y,z;
for(int i=,u,v;i<n;i++){
scanf("%d%d",&u,&v);
Add_Edge(u,v);Add_Edge(v,u);
}
DFS_First(,,);
DFS_Second(,);
Build(,,n); scanf("%d",&q);
while(q--){
scanf("%d%d%d",&x,&y,&z);
if(x==)Change(y,z);
else printf("%d\n",QuerySum(y,z));
}
return ;
}
Codevs 4633 [Mz]树链剖分练习的更多相关文章
- CODE[VS]4633:Mz树链剖分练习
Description 给定一棵结点数为n的树,初始点权均为0,有依次q个操作,每次操作有三个参数a,b,c,当a=1时,表示给b号结点到c号结点路径上的所有点(包括b,c,下同)权值都增加1,当a= ...
- codevs 1228 苹果树 树链剖分讲解
题目:codevs 1228 苹果树 链接:http://codevs.cn/problem/1228/ 看了这么多树链剖分的解释,几个小时后总算把树链剖分弄懂了. 树链剖分的功能:快速修改,查询树上 ...
- [CodeVS4633][Mz]树链剖分练习
思路: 轻重链剖分+线段树. #include<cstdio> #include<vector> #include<cstring> ; std::vector&l ...
- 【最近公共祖先】【树链剖分】CODEVS 1036 商务旅行
树链剖分求lca模板.O(log(n)),就是不倍增嘛~ #include<cstdio> #include<algorithm> using namespace std; # ...
- 【BZOJ-3589】动态树 树链剖分 + 线段树 + 线段覆盖(特殊的技巧)
3589: 动态树 Time Limit: 30 Sec Memory Limit: 1024 MBSubmit: 405 Solved: 137[Submit][Status][Discuss] ...
- NOIP2015 运输计划 - 二分 + 树链剖分 / (倍增 + 差分)
BZOJ CodeVS Uoj 题目大意: 给一个n个点的边带权树,给定m条链,你可以选择树中的任意一条边,将它置为0,使得最长的链长最短. 题目分析: 最小化最大值,二分. 二分最短长度mid,将图 ...
- 2020牛客NOIP赛前集训营-提高组(第三场) C - 牛半仙的妹子Tree (树链剖分)
昨天教练问我:你用树剖做这道题,怎么全部清空状态呢? 我:???不是懒标记就完了??? 教练:树剖不是要建很多棵线段树吗,不止log个,你要一个一个清? 我:为什么要建很多棵线段树? ...
- BZOJ 3626: [LNOI2014]LCA [树链剖分 离线|主席树]
3626: [LNOI2014]LCA Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 2050 Solved: 817[Submit][Status ...
- BZOJ 1984: 月下“毛景树” [树链剖分 边权]
1984: 月下“毛景树” Time Limit: 20 Sec Memory Limit: 64 MBSubmit: 1728 Solved: 531[Submit][Status][Discu ...
随机推荐
- js原生实现三级联动下拉菜单
js代码: <!doctype html> <html> <head> <meta charset="utf-8"> <tit ...
- 生产环境LAMP搭建 - 基于 fastcgi
生产环境LAMP搭建 - 基于 fastcgi 由于在module模式,php只是已http的模块形式存在,无形中加重了http的服务负载,通常在企业架构中,使用fastcgi的模式,将所有的服务都设 ...
- pymysql模块操作数据库及连接报错解决方法
import pymysql sql = "select host,user,password from user" #想要执行的MySQL语句 #sql = 'create da ...
- DevOps - 日志分析 -ELK
wget --no-check-certificate --no-cookies --header "Cookie: oraclelicense=accept-securebackup-co ...
- 水平垂直居中图片及文字(兼容IE6+)实例
直接看代码: <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <me ...
- 【Ecshop】修改处理用户购物车的行为
Ecshop v2.7.3的购物车处理方面在现在看来有比较反用户体验的设计: 用户未登录时加入购物车的商品,在用户登录后会被清空而不是加入到登录用户的购物车中: 用户登录后加入购物车的商品,在退出后会 ...
- exec , 元类,__new__, __call__ , 单例模式 , 异常
1,类也是对象 ''' 动态语言 可以在运行期间 动态生成类 修改对象属性 静态语言 ''''' ''' type(object_or_name, bases, dict) type(object) ...
- selenium中webdriver跳转新页面后定位置新页面的两种方式
刚刚在写Python爬虫的时候用到了selenium , 在跳转新页面时发现无法定位新页面 , 查找不到新页面的元素 一番查询后得到了解决方法 , 便记录下来备忘 , 也与大家分享 # 页面跳转代码. ...
- Django API 为 D3 提供数据
在工作中见过有的人即便使用了Django,依然还在采取json或geojson的文件形式为页面提供数据,相当于嵌入数据而非加载.下面是个简单有效的例子: 先从 model.py 开始 # models ...
- Android 多线程 打地鼠游戏
前言:最近比较喜欢多线程了,看到了一些线程案例,这里总结一下打地鼠游戏的整个过程. 1.首先是主活动要加载的布局,我一般就喜欢早点把这个写上,这样就好在主活动中玩弄这些控件了.闲话不多说,一个Fram ...