[BZOJ2631]tree 动态树lct
2631: tree
Time Limit: 30 Sec Memory Limit: 128 MB
Submit: 5171 Solved: 1754
[Submit][Status][Discuss]
Description
一棵n个点的树,每个点的初始权值为1。对于这棵树有q个操作,每个操作为以下四种操作之一:
+ u v c:将u到v的路径上的点的权值都加上自然数c;
- u1 v1 u2 v2:将树中原有的边(u1,v1)删除,加入一条新边(u2,v2),保证操作完之后仍然是一棵树;
* u v c:将u到v的路径上的点的权值都乘上自然数c;
/ u v:询问u到v的路径上的点的权值和,求出答案对于51061的余数。
Input
接下来n-1行每行两个正整数u,v,描述这棵树
接下来q行,每行描述一个操作
Output
Sample Input
1 2
2 3
* 1 3 4
/ 1 1
Sample Output
HINT
数据规模和约定
10%的数据保证,1<=n,q<=2000
另外15%的数据保证,1<=n,q<=5*10^4,没有-操作,并且初始树为一条链
另外35%的数据保证,1<=n,q<=5*10^4,没有-操作
100%的数据保证,1<=n,q<=10^5,0<=c<=10^4
Source
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#define maxn 500005
#define ls(x) t[x].s[0]
#define rs(x) t[x].s[1]
#define ll long long
#define mod 51061
using namespace std;
inline ll read() {
ll x=,f=;char ch=getchar();
for(;!isdigit(ch);ch=getchar()) if(ch=='-') f=-;
for(;isdigit(ch);ch=getchar()) x=x*+ch-'';
return x*f;
}
struct data {
ll s[],fa,c,a,v,sum,size;
data(){c=;v=,a=,sum=,size=;}
bool rev;
}t[maxn];
bool isroot(int x) {return ls(t[x].fa)!=x&&rs(t[x].fa)!=x;}
void pushup(int x) {t[x].sum=t[ls(x)].sum+t[rs(x)].sum+t[x].v;t[x].sum%=mod;t[x].size=t[ls(x)].size+t[rs(x)].size+;}
void cal(int x,int ce,int add) {
t[x].sum=t[x].sum*ce+add*t[x].size;t[x].sum%=mod;
t[x].v=t[x].v*ce+add;t[x].v%=mod;
t[x].c*=ce;t[x].c%=mod;
t[x].a=t[x].a*ce+add;t[x].a%=mod;
}
void pushdown(int x) {
if(t[x].rev) {
swap(ls(x),rs(x));
t[ls(x)].rev^=;t[rs(x)].rev^=;t[x].rev^=;
}
int ce=t[x].c,add=t[x].a;
t[x].c=;t[x].a=;
cal(ls(x),ce,add);cal(rs(x),ce,add);
}
void rotate(int x) {
int y=t[x].fa,z=t[y].fa;
bool l=ls(y)!=x,r=l^;
if(!isroot(y)) t[z].s[t[z].s[]==y]=x;
t[x].fa=z;t[y].fa=x;t[y].s[l]=t[x].s[r];
t[t[x].s[r]].fa=y;t[x].s[r]=y;
pushup(y);pushup(x);
}
void pre(int x) {
if(!isroot(x)) pre(t[x].fa);
pushdown(x);
}
void splay(int x) {
pre(x);
while(!isroot(x)) {
int y=t[x].fa,z=t[y].fa;
if(!isroot(y)){
if(ls(y)==x^ls(z)==y) rotate(x);
else rotate(y);
}
rotate(x);
}
}
void access(int x) {for(int y=;x;y=x,x=t[x].fa) {splay(x);t[x].s[]=y;pushup(x);}}
void mroot(int x) {access(x);splay(x);t[x].rev^=;}
void link(int x,int y) {mroot(x);t[x].fa=y;}
void cut(int x,int y) {mroot(x);access(y);splay(y);t[y].s[]=t[x].fa=;pushup(y);}
int n,q;
int main() {
n=read(),q=read();
t[].a=t[].c=t[].sum=t[].size=;
for(int i=;i<n;i++) {int u=read(),v=read();link(u,v);}
for(int i=;i<=q;i++) {
char ch[];scanf("%s",ch);
if(ch[]=='+') {int u=read(),v=read();mroot(u);access(v);splay(v);ll tmp=read();cal(v,,tmp);}
if(ch[]=='-') {int u=read(),v=read();cut(u,v);u=read();v=read();link(u,v);}
if(ch[]=='*') {int u=read(),v=read();mroot(u);access(v);splay(v);ll tmp=read();cal(v,tmp,);}
if(ch[]=='/') {int u=read(),v=read();mroot(u);access(v);splay(v);printf("%lld\n",t[v].sum);}
}
return ;
}
/*
5 100
1 2 1 3 3 4 3 5
+ 4 5 3
/ 4 5
* 4 5 3
/ 4 5
- 3 4 4 5
/ 3 4
*/
[BZOJ2631]tree 动态树lct的更多相关文章
- hdu 5398 动态树LCT
GCD Tree Time Limit: 5000/2500 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Su ...
- hdu 5002 (动态树lct)
Tree Time Limit: 16000/8000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submi ...
- LCT总结——概念篇+洛谷P3690[模板]Link Cut Tree(动态树)(LCT,Splay)
为了优化体验(其实是强迫症),蒟蒻把总结拆成了两篇,方便不同学习阶段的Dalao们切换. LCT总结--应用篇戳这里 概念.性质简述 首先介绍一下链剖分的概念(感谢laofu的讲课) 链剖分,是指一类 ...
- HDU 4718 The LCIS on the Tree (动态树LCT)
The LCIS on the Tree Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Oth ...
- LCT(link cut tree) 动态树
模板参考:https://blog.csdn.net/saramanda/article/details/55253627 综合各位大大博客后整理的模板: #include<iostream&g ...
- 动态树LCT小结
最开始看动态树不知道找了多少资料,总感觉不能完全理解.但其实理解了就是那么一回事...动态树在某种意思上来说跟树链剖分很相似,都是为了解决序列问题,树链剖分由于树的形态是不变的,所以可以通过预处理节点 ...
- [模板] 动态树/LCT
简介 LCT是一种数据结构, 可以维护树的动态加边, 删边, 维护链上信息(满足结合律), 单次操作时间复杂度 \(O(\log n)\).(不会证) 思想类似树链剖分, 因为splay可以换根, 用 ...
- 动态树LCT(Link-cut-tree)总结+模板题+各种题目
一.理解LCT的工作原理 先看一道例题: 让你维护一棵给定的树,需要支持下面两种操作: Change x val: 令x点的点权变为val Query x y: 计算x,y之间的唯一的最短路径的点 ...
- BZOJ 2631 tree 动态树(Link-Cut-Tree)
题目大意:维护一种树形数据结构.支持下面操作: 1.树上两点之间的点权值+k. 2.删除一条边.添加一条边,保证加边之后还是一棵树. 3.树上两点之间点权值*k. 4.询问树上两点时间点的权值和. 思 ...
随机推荐
- PAT 1045 快速排序
https://pintia.cn/problem-sets/994805260223102976/problems/994805278589960192 著名的快速排序算法里有一个经典的划分过程:我 ...
- slf4j使用log4j学习笔记
一,介绍 SLF4J 简单日记门面(Facade)SLF4J是为各种loging APIs提供一个简单统一的接口,从而使得最终用户能够在部署的时候配置自己希望的loging APIs实现. Loggi ...
- JavaScript constructor 属性详解
对象的constructor属性用于返回创建该对象的函数,也就是我们常说的构造函数. 在JavaScript中,每个具有原型的对象都会自动获得constructor属性.除了arguments.Enu ...
- Luogu 3435 POI2006OKR-Periods of Words(kmp)
显然答案应该是Σi-next[next[……next[i]]] (next[next[……next[i]]]>0).递推即可. #include<iostream> #include ...
- thymeleaf 布局layout
以前写过一篇使用thymeleaf实现div中加载html 大部分内容都没问题,只是部分知识已经过时了. 重新记录: 依赖依然是 <dependency> <groupId>n ...
- [Leetcode] Longest consecutive sequence 最长连续序列
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
- 【17.12.22.B】
B 题面描述: 给一个长度为n的序列,a[1], a[2], ... , a[n], 选出连续的k个数,使得这k个数的最大值加这k个数的or值最大. 假设选出的数为a[l], a[l + 1], .. ...
- 适用于iview的表格转Excel插件
在网上找的一个表格转excel插件,经过修改后使其适用于iview中的table组件 let idTmr; const getExplorer = () => { let explorer = ...
- 生日蛋糕 POJ - 1190
7月17日是Mr.W的生日,ACM-THU为此要制作一个体积为Nπ的M层生日蛋糕,每层都是一个圆柱体. 设从下往上数第i(1 <= i <= M)层蛋糕是半径为Ri, 高度为Hi的圆柱.当 ...
- CSS样式实现溢出超出DIV边框宽度高度的内容自动隐藏方法
CSS样式实现溢出超出DIV边框宽度高度的内容自动隐藏方法 平时我们布局时候,有的文字内容多了会超过溢出我们限制的高度,有的图片会撑破DIV,让网页错位变乱. 这样我们就需要解决如何使用CSS来超出设 ...