[bzoj2631]tree_LCT
tree bzoj-2631
题目大意:给定一个n个点的树,每个点的初始权值为1,支持:删边加边(这两个操作同时进行,保证操作之后还是一棵树),路径加,路径乘,查询路径和。
注释:$1\le n,q\le 10^5$,$1\le c \le 10^4$。
想法:暴力给的很优雅:15‘没有加边删边且保证是链,我们直接用线段树维护。50'没有加边删边,我们裸上树剖+线段树。满分的话我们用LCT维护加边删边。
具体地:我的习惯是先×后+,所以每一次乘法都先将加法标记也进行修改。询问的话直接makeroot,我们节点信息直接维护原树子树信息,然后对应的查询就是链上的信息。
最后,附上丑陋的代码... ...
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define N 100050
#define mod 51061
#define ls ch[p][0]
#define rs ch[p][1]
#define get(x) (ch[f[x]][1]==x)
typedef unsigned int ui;
int n,m,ch[N][2],f[N],rev[N];
ui sum[N],mul[N],add[N],siz[N],val[N];
char opt[10];
inline bool isrt(int x)
{
return ch[f[x]][0]!=x&&ch[f[x]][1]!=x;
}
void pushdown(int p)
{
if(mul[p]!=1)
{
ui u=mul[p];
sum[ls]=sum[ls]*u%mod; mul[ls]=mul[ls]*u%mod; add[ls]=add[ls]*u%mod; val[ls]=val[ls]*u%mod;
sum[rs]=sum[rs]*u%mod; mul[rs]=mul[rs]*u%mod; add[rs]=add[rs]*u%mod; val[rs]=val[rs]*u%mod;
mul[p]=1;
}
if(add[p])
{
ui d=add[p];
sum[ls]=(sum[ls]+siz[ls]*d%mod)%mod; add[ls]=(add[ls]+d)%mod; val[ls]=(val[ls]+d)%mod;
sum[rs]=(sum[rs]+siz[rs]*d%mod)%mod; add[rs]=(add[rs]+d)%mod; val[rs]=(val[rs]+d)%mod;
add[p]=0;
}
if(rev[p])
{
swap(ch[ls][0],ch[ls][1]);
swap(ch[rs][0],ch[rs][1]);
rev[ls]^=1; rev[rs]^=1;
rev[p]=0;
}
}
void pushup(int p)
{
siz[p]=siz[ls]+siz[rs]+1;
sum[p]=(sum[ls]+sum[rs]+val[p])%mod;
}
void update(int p)
{
if(!isrt(p)) update(f[p]);
pushdown(p);
}
void rotate(int x)
{
int y=f[x],z=f[y],k=get(x);
if(!isrt(y)) ch[z][ch[z][1]==y]=x;
ch[y][k]=ch[x][!k]; f[ch[y][k]]=y;
ch[x][!k]=y; f[y]=x; f[x]=z;
pushup(y); pushup(x);
}
void splay(int x)
{
update(x);
for(int fa;fa=f[x],!isrt(x);rotate(x))
if(!isrt(fa))
rotate(get(fa)==get(x)?fa:x);
}
void access(int p)
{
int t=0;
while(p) splay(p),rs=t,pushup(p),t=p,p=f[p];
}
void makeroot(int p)
{
access(p); splay(p); swap(ls,rs); rev[p]^=1;
}
void link(int x,int p)
{
makeroot(x); f[x]=p;
}
void cut(int x,int p)
{
makeroot(x); access(p); splay(p); ls=f[x]=0;
}
void split(int x,int p)
{
makeroot(x); access(p); splay(p);
}
int main()
{
scanf("%d%d",&n,&m);
int i,x,y,z,w;
for(i=1;i<=n;i++) val[i]=mul[i]=siz[i]=sum[i]=1;
for(i=1;i<n;i++)
{
scanf("%d%d",&x,&y); link(x,y);
}
for(i=1;i<=m;i++)
{
scanf("%s%d%d",opt,&x,&y);
int p=y;
if(opt[0]=='+')
{
scanf("%d",&z); split(x,p);
sum[p]=(sum[p]+siz[p]*z%mod)%mod;
val[p]=(val[p]+z)%mod;
add[p]=(add[p]+z)%mod;
}
else if(opt[0]=='-')
{
scanf("%d%d",&z,&w); cut(x,y); link(z,w);
}
else if(opt[0]=='*')
{
scanf("%d",&z); split(x,p);
sum[p]=sum[p]*z%mod;
val[p]=val[p]*z%mod;
add[p]=add[p]*z%mod;
mul[p]=mul[p]*z%mod;
}
else
{
split(x,p); printf("%u\n",sum[p]);
}
}
}
小结:LCT是个好东西。
[bzoj2631]tree_LCT的更多相关文章
- bzoj2631 3282
这两题都是link cut tree的裸题之前看Qtree的论文,只会在确定父子关系的情况下连边和删边如果在任意两个点连边删边怎么做呢?这时候我们不能随意的将一个点的父节点设为另一个点,因为其中某个点 ...
- bzoj2631: tree
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #i ...
- BZOJ2631——tree
1.题目大意:bzoj1798的lct版本 2.分析:这个把线段树改成splay就好 #include <stack> #include <cstdio> #include & ...
- 【bzoj2631】tree link-cut-tree
2016-06-01 08:50:36 题目:http://www.lydsy.com/JudgeOnline/problem.php?id=2631 注意加和乘的标记下传问题. 还有就是split后 ...
- bzoj2631: tree lct
要打mul和add的lct 50000+的mod用unsigned int好了TAT (坑爹没打pc('\n');(静态)调了好久,样例竟然只输出一个,orz,也不提示PE T_T) #include ...
- LCT模板(BZOJ2631)
用LCT实现路径加,路径乘,断开及加上一条边(保证是树),查询路径和. #include <cstdio> #include <algorithm> #define l(x) ...
- 【BZOJ2631】tree
Description 一棵n个点的树.每一个点的初始权值为1. 对于这棵树有q个操作,每一个操作为下面四种操作之中的一个: + u v c:将u到v的路径上的点的权值都加上自然数c: - u1 v1 ...
- bzoj2631
题解: lct+链上修改 每一次修改的时候记录lazy标记 如果有了乘法,加法的lazy标记也要相应的随之变化 代码: #pragma GCC optimize(2) #include<bits ...
- 【bzoj2631】tree LCT
题目描述 一棵n个点的树,每个点的初始权值为1.对于这棵树有q个操作,每个操作为以下四种操作之一:+ u v c:将u到v的路径上的点的权值都加上自然数c:- u1 v1 u2 v2:将树中原有的边( ...
随机推荐
- UESTC--1251--谕神的密码(贪心)
谕神的密码 Time Limit: 1000MS Memory Limit: 65535KB 64bit IO Format: %lld & %llu Submit Status ...
- Genesis 多边形闭轮廓填充算法
通过逐行扫描,计算得出直线与多边形相交点进行求解 原理图形如下所示: 相关函数: /// <summary> /// 求点P到线段L距离 /// </summary> /// ...
- sdut1269 走迷宫(dfs)
http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=1269 连着做了三个基本的dfs,终于弄懂了搜索 ...
- hdu5673-Robot
题目: http://acm.hdu.edu.cn/showproblem.php?pid=5673 好久没打BC,当时这场过了3题,hack了一个,马马虎虎吧,因为前三个题确实不难. 这个是那场的第 ...
- C# 设置百分比保留2位数
double a=50; double b=100; 没有小数部分Label1.Text = (a / (a+b)).ToString("0%") 两位小数Label2 ...
- 题解报告:hdu 1850 Being a Good Boy in Spring Festival(尼姆博弈)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1850 Problem Description 一年在外 父母时刻牵挂春节回家 你能做几天好孩子吗寒假里 ...
- 2018.10.9 上线发现elasticsearch写入速度超级慢,原来罪魁祸首是阿里云服务的OSS的锅
问题描述: 按照项目计划,今天上线部署日志系统(收集线上的所有日志,便于问题排查). 运维按照以前的部署过程,部署elasticsearch,部署结束之后,通过x-pack的monitor发现elas ...
- P3709 大爷的字符串题(50分)
题目背景 在那遥远的西南有一所学校 /*被和谐部分*/ 然后去参加该省省选虐场 然后某蒟蒻不会做,所以也出了一个字符串题: 题目描述 给你一个字符串a,每次询问一段区间的贡献 贡献定义: 每次从这个区 ...
- 在窗体中把DataGridView中的数据导出Excel
//DataGridView导出Excel private void bt_Excl_Click(object sender, EventArgs e) { SaveFileDialog saveFi ...
- matplotlib之pyplot 知识点滴
以下是一些常用地址链接,请参考 matplotlib 官方网址 plt.plot()函数细节 Matplotlib 中文用户指南 4.6 编写数学表达式 Python seaborn matplotl ...