洛谷P1501 [国家集训队]Tree II(打标记lct)
题目描述
一棵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的余数。
输入输出格式
输入格式:
第一行两个整数n,q
接下来n-1行每行两个正整数u,v,描述这棵树
接下来q行,每行描述一个操作
输出格式:
对于每个/对应的答案输出一行
输入输出样例
说明
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
By (伍一鸣)
题解:这题其实没有什么很难的地方,主要考点就是lct的打标记(还是两个orz),显然链修改跟线段树的打标记是一样的
就是酱紫了~
结果%=写成%调了一个晚上2333
代码如下
#include<map>
#include<set>
#include<queue>
#include<cmath>
#include<cstdio>
#include<string>
#include<vector>
#include<cstring>
#include<iostream>
#include<algorithm>
#define N 100010
#define mod 51061
#define lson ch[x][0]
#define rson ch[x][1]
#define int long long
using namespace std; int tag1[N],tag2[N],tag3[N],sum[N],sz[N],f[N],ch[N][],w[N]; int not_root(int now)
{
int x=f[now];
return lson==now||rson==now;
} int push_up(int x)
{
sum[x]=(sum[lson]+sum[rson]+w[x])%mod;
sz[x]=sz[lson]+sz[rson]+;
} int rev(int x)
{
swap(lson,rson);
tag1[x]^=;
} int add(int x,int cost)
{
sum[x]+=cost*sz[x];
sum[x]%=mod;
w[x]+=cost;
w[x]%=mod;
tag2[x]+=cost;
tag2[x]%=mod;
} int mul(int x,int cost)
{
sum[x]*=cost;
sum[x]%=mod;
w[x]*=cost;
w[x]%=mod;
tag3[x]*=cost;
tag3[x]%=mod;
tag2[x]*=cost;
tag2[x]%=mod;
} int push_down(int x)
{
if(tag3[x]!=)
{
mul(lson,tag3[x]);
mul(rson,tag3[x]);
tag3[x]=;
}
if(tag2[x])
{
add(lson,tag2[x]);
add(rson,tag2[x]);
tag2[x]=;
}
if(tag1[x])
{
rev(lson);
rev(rson);
tag1[x]=;
}
} int rotate(int x)
{
int y=f[x],z=f[y],kd=ch[y][]==x,xs=ch[x][!kd];
if(not_root(y))
{
ch[z][ch[z][]==y]=x;
}
ch[x][!kd]=y;
ch[y][kd]=xs;
if(xs) f[xs]=y;
f[y]=x;
f[x]=z;
push_up(y);
} int push_all(int x)
{
if(not_root(x))
{
push_all(f[x]);
}
push_down(x);
} int splay(int x)
{
int y,z;
push_all(x);
while(not_root(x))
{
y=f[x],z=f[y];
if(not_root(y))
{
(ch[y][]==x)^(ch[z][]==y)?rotate(x):rotate(y);
}
rotate(x);
}
push_up(x);
} int access(int x)
{
for(int y=; x; y=x,x=f[x])
{
splay(x);
rson=y;
push_up(x);
}
} int make_root(int x)
{
access(x);
splay(x);
rev(x);
} int split(int x,int y)
{
make_root(x);
access(y);
splay(y);
} int link(int x,int y)
{
make_root(x);
f[x]=y;
} int cut(int x,int y)
{
split(x,y);
f[x]=ch[y][]=;
} char s[];
int n,m; signed main()
{
scanf("%lld %lld",&n,&m);
for(int i=; i<=n; i++)
{
sum[i]=w[i]=tag3[i]=sz[i]=;
}
for(int i=; i<n; i++)
{
int from,to;
scanf("%lld %lld",&from,&to);
link(from,to);
}
for(int i=; i<=m; i++)
{
scanf("%s",s);
if(s[]=='*')
{
int x,y,cost;
scanf("%lld %lld %lld",&x,&y,&cost);
split(y,x);
mul(x,cost);
}
if(s[]=='-')
{
int u1,v1,u2,v2;
scanf("%lld %lld %lld %lld",&u1,&v1,&u2,&v2);
cut(u1,v1);
link(u2,v2);
}
if(s[]=='+')
{
int x,y,cost;
scanf("%lld %lld %lld",&x,&y,&cost);
split(y,x);
add(x,cost);
}
if(s[]=='/')
{
int u,v;
scanf("%lld %lld",&u,&v);
split(u,v);
printf("%lld\n",sum[v]);
}
}
}
洛谷P1501 [国家集训队]Tree II(打标记lct)的更多相关文章
- 洛谷 P1501 [国家集训队]Tree II 解题报告
P1501 [国家集训队]Tree II 题目描述 一棵\(n\)个点的树,每个点的初始权值为\(1\).对于这棵树有\(q\)个操作,每个操作为以下四种操作之一: + u v c:将\(u\)到\( ...
- 洛谷P1501 [国家集训队]Tree II(LCT,Splay)
洛谷题目传送门 关于LCT的其它问题可以参考一下我的LCT总结 一道LCT很好的练习放懒标记技巧的题目. 一开始看到又做加法又做乘法的时候我是有点mengbi的. 然后我想起了模板线段树2...... ...
- 【刷题】洛谷 P1501 [国家集训队]Tree II
题目描述 一棵n个点的树,每个点的初始权值为1.对于这棵树有q个操作,每个操作为以下四种操作之一: + u v c:将u到v的路径上的点的权值都加上自然数c: - u1 v1 u2 v2:将树中原有的 ...
- 洛谷P1501 [国家集训队]Tree II(LCT)
题目描述 一棵n个点的树,每个点的初始权值为1.对于这棵树有q个操作,每个操作为以下四种操作之一: + u v c:将u到v的路径上的点的权值都加上自然数c: - u1 v1 u2 v2:将树中原有的 ...
- [洛谷P1501][国家集训队]Tree II
题目大意:给一棵树,有四种操作: $+\;u\;v\;c:$将路径$u->v$区间加$c$ $-\;u_1\;v_1\;u_2\;v_2:$将边$u_1-v_1$切断,改成边$u_2-v_2$, ...
- 洛谷 P1501 [国家集训队]Tree II
看来这个LCT板子并没有什么问题 #include<cstdio> #include<algorithm> using namespace std; typedef long ...
- 洛谷 P1501 [国家集训队]Tree II Link-Cut-Tree
Code: #include <cstdio> #include <algorithm> #include <cstring> #include <strin ...
- [洛谷P1501] [国家集训队]Tree II(LCT模板)
传送门 这是一道LCT的板子题,说白了就是在LCT上支持线段树2的操作. 所以我只是来存一个板子,并不会讲什么(再说我也不会,只能误人子弟2333). 不过代码里的注释可以参考一下. Code #in ...
- 洛谷.1501.[国家集训队]Tree II(LCT)
题目链接 日常zz被define里没取模坑 //标记下放同线段树 注意51061^2 > 2147483647,要开unsigned int //*sz[]别忘了.. #include < ...
随机推荐
- e8000051
Unable to install package (e8000051). Please check used certificate validity and provisioning.. Unab ...
- c++builder 解压缩
c++builder 解压缩 TZCompressionStream TZDecompressionStream #include <System.ZLib.hpp> void __ ...
- short i=1;short i=i+1对或错,错的理由;short i+=1对或错,错的理由
short i=1; i=i+1 i+=1 这是一个类型高级与低级的问题,前面的C是short型的,后面的1为int型的,short型与int型的相加得到short型是不可取,因为必须将int型转换为 ...
- iOS学习之Xcode 的Debug技巧
在Xcode中,Debug时,不能像eclipse ,或VS那些集成开发那样,能直接查看变量的值.那怎么在调试的时候查看XCode的变量呢? 有一些方法的. 1.新建一个Single View App ...
- 推荐十几款Firefox web开发插件(转载)
开发工具 Web Developer 1.1.8 https://addons.mozilla.org/en-US/firefox/addon/60by chrispederick The Web ...
- AspectJ、Spring与AOP的关系
- Linux buffer and cache
A buffer is something that has yet to be "written" to disk. A cache is something that has ...
- easyui图标大全
.icon-blank{ background:url('icons/blank.gif') no-repeat; } .icon-add{ background:url('icons/edit_ad ...
- Redis学习(1)——下载与配置[转]
Redis是一个开源的使用ANSI C语言编写.支持网络.可基于内存亦可持久化的日志型.Key-Value数据库,并提供多种语言的API.从2010年3月15日起,Redis的开发工作由VMware主 ...
- sql2008 安装提示重启失败
[转] https://www.cnblogs.com/chenshaogang/p/4313022.html