[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.询问树上两点时间点的权值和. 思 ...
随机推荐
- 【Python】- 如何使用Visual Studio 2013编写python?
安装Visual Studio 2013 1.VS2013下载安装略 安装python2.7 1.从官网下载python2.7,下载地址:https://www.python.org/getit/ ...
- awk,rsync,重启,maxdepth一层目录,登录,开机自启动
有100个日志文件,每个文件大约1G,每条日志都以 “H:i:s” 的时间格式开头,如: 05:02:04 xxx yyy zzz 因为是日志文件,所以肯定以时间为顺序的,现在可以确定的是,在某个文件 ...
- [剑指Offer] 34.第一个只出现一次的数
题目描述 在一个字符串(1<=字符串长度<=10000,全部由大写字母组成)中找到第一个只出现一次的字符,并返回它的位置 [思路]当一个字符第一次出现的位置和它最后一次出现的位置相同,那么 ...
- hdu 1053 Entropy (哈夫曼树)
Entropy Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Sub ...
- P1275 魔板
题目描述 有这样一种魔板:它是一个长方形的面板,被划分成n行m列的n*m个方格.每个方格内有一个小灯泡,灯泡的状态有两种(亮或暗).我们可以通过若干操作使魔板从一个状态改变为另一个状态.操作的方式有两 ...
- [Leetcode] Interger to roman 整数转成罗马数字
Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 t ...
- [Leetcode] merge sorted array 合并数组
Given two sorted integer arrays A and B, merge B into A as one sorted array. Note: You may assume th ...
- Website Collection
前一百个卡特兰数 Candy?的博弈论总结 杜教筛资料 线性基资料 (ex)BSGS资料 斐波那契数列前300项 斯特林数 STL标准库-容器-unordered_set C++ unordered_ ...
- 淡入淡出效果的js原生实现
淡入淡出效果,在日常项目中经常用到,可惜原生JS没有类似的方法,而有时小的页面并不值得引入一个jQuery库,所以就自己写了一个,已封装, 有用得着的朋友, 可以直接使用. 代码中另附有一个设置元素透 ...
- ionic安装遇到的一些问题
ionic = Cordova + Angular + ionic CSS // 安装(失败的话 Mac 尝试使用 sudo,Windows 尝试管理员身份运行 cmd)$ npm install - ...