p1501 [国家集训队]Tree II
分析
lct板子题
单独维护一下加和乘的情况即可
维护方法和维护翻转差不多
代码
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<cctype>
#include<cmath>
#include<cstdlib>
#include<queue>
#include<ctime>
#include<vector>
#include<set>
#include<map>
#include<stack>
using namespace std;
#define add(x,y) x=(x+y)%mod;
#define mul(x,y) x=1ll*x*y%mod;
const int mod = ;
int n,q,fa[],son[][],sum[],siz[];
int cola[],colm[],a[],r[];
inline void up(int x){
sum[x]=(sum[son[x][]]+sum[son[x][]]+a[x])%mod;
siz[x]=(siz[son[x][]]+siz[son[x][]]+)%mod;
}
inline void rev(int x){swap(son[x][],son[x][]);r[x]^=;}
inline void doa(int x,int y){
add(a[x],y);
add(sum[x],1ll*siz[x]*y%mod);
add(cola[x],y);
}
inline void dom(int x,int y){
mul(a[x],y);
mul(sum[x],y);
mul(cola[x],y);
mul(colm[x],y);
}
inline void pd(int x){
if(colm[x]!=){
dom(son[x][],colm[x]);
dom(son[x][],colm[x]);
colm[x]=;
}
if(cola[x]){
doa(son[x][],cola[x]);
doa(son[x][],cola[x]);
cola[x]=;
}
if(r[x]){
if(son[x][])rev(son[x][]);
if(son[x][])rev(son[x][]);
r[x]=;
}
}
inline bool notroot(int x){return x==son[fa[x]][]||x==son[fa[x]][];}
inline void push_all(int x){if(notroot(x))push_all(fa[x]);pd(x);}
inline int gs(int x){return son[fa[x]][]==x;}
inline void rot(int x){
int y=fa[x],z=fa[y],b=gs(x),c=gs(y),d=son[x][!b];
if(notroot(y))son[z][c]=x;
fa[x]=z;
if(d)fa[d]=y;
son[y][b]=d;
son[x][!b]=y;
fa[y]=x;
up(y),up(x);
}
inline void splay(int x){
push_all(x);
while(notroot(x)){
int y=fa[x],z=fa[y];
if(notroot(y)){
if(gs(x)==gs(y))rot(y);
else rot(x);
}
rot(x);
}
}
inline void access(int x){
for(int y=;x;y=x,x=fa[x])
splay(x),son[x][]=y,up(x);
}
inline void makeroot(int x){
access(x);
splay(x);
rev(x);
}
inline int findroot(int x){
access(x);
splay(x);
while(son[x][])pd(x),x=son[x][];
return x;
}
inline void spt(int x,int y){
makeroot(x);
access(y);
splay(y);
}
inline void link(int x,int y){
makeroot(x);
if(findroot(y)!=x)fa[x]=y;
}
inline void cut(int x,int y){
makeroot(x);
if(findroot(y)==x&&fa[x]==y&&(!son[x][]))
fa[x]=son[y][]=,up(y);
}
int main(){
int i,j,k;
scanf("%d%d",&n,&q);
for(i=;i<=n;i++)siz[i]=sum[i]=a[i]=;
for(i=;i<n;i++){
int x,y;
scanf("%d%d",&x,&y);
link(x,y);
}
for(i=;i<=q;i++){
char s[];
int x,y,z,w;
scanf("%s",s);
if(s[]=='+'){
scanf("%d%d%d",&x,&y,&z);
spt(x,y);
doa(y,z);
}else if(s[]=='-'){
scanf("%d%d%d%d",&x,&y,&z,&w);
cut(x,y);
link(z,w);
}else if(s[]=='*'){
scanf("%d%d%d",&x,&y,&z);
spt(x,y);
dom(y,z);
}else {
scanf("%d%d",&x,&y);
spt(x,y);
printf("%d\n",sum[y]);
}
}
return ;
}
p1501 [国家集训队]Tree II的更多相关文章
- P1501 [国家集训队]Tree II(LCT)
P1501 [国家集训队]Tree II 看着维护吧2333333 操作和维护区间加.乘线段树挺像的 进行修改操作时不要忘记吧每个点的点权$v[i]$也处理掉 还有就是$51061^2=2607225 ...
- 洛谷 P1501 [国家集训队]Tree II 解题报告
P1501 [国家集训队]Tree II 题目描述 一棵\(n\)个点的树,每个点的初始权值为\(1\).对于这棵树有\(q\)个操作,每个操作为以下四种操作之一: + u v c:将\(u\)到\( ...
- BZOJ 2631 tree | Luogu P1501 [国家集训队]Tree II (LCT 多重标记下放)
链接:https://www.luogu.org/problemnew/show/P1501 题面: 题目描述 一棵n个点的树,每个点的初始权值为1.对于这棵树有q个操作,每个操作为以下四种操作之一: ...
- 洛谷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(打标记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 ...
随机推荐
- 常见企业IT支撑【3、SVN代码版本控制系统】
代码版本控制系统有人喜欢用SVN,有人喜欢用GIT 1.环境 os:centos7 apache:2.4.6 python:2.7 --自带 submin2:2.2.1 2.安装ap ...
- 阿里云视频点播 php开发
先购买开通阿里云的<视频点播>服务,视频点播 可以购买套餐 ,我在项目中使用的是299套餐 开发前在<用户信息管理>生成Access Key Secret,开发密钥使用 阿里云 ...
- bfs判断子图是否连通
int judge() { int v[13] = { 0 }; queue<int> myq; myq.push(ans[0]); v[ans[0]] = 1; while (!myq. ...
- UE4 代码总结
1.创建关卡类 1.创建C++类继承LevelScriptActor 2.打开关卡蓝图 Class Settings->Parent Class 选择你之前创建好的C++类 遇到的问题: 1.T ...
- mysql 存储过程简单学习
转载自:http://blog.chinaunix.net/uid-23302288-id-3785111.html ■存储过程Stored Procedure 存储过程就是保存一系列SQL命令的集合 ...
- Linux无法登录,显示module is unknown,一闪而过
1.使用单用户模式登录系统(不作介绍) 2.查看日志:vim /var/log/secure 3.记忆起曾经配置oracle添加过该参数 vim/etc/pam.d/login中加入了: sessio ...
- 排除maven jar冲突 maven tomcat插件启动报错 filter转换异常
最近在搞一个ssm+shiro的整合 用的maven tomcat插件 启动的时候报错,提示 maven org.springframework.web.filter.CharacterEncodin ...
- python开发_自己开发的一个小游戏
先看看游戏的运行效果: 看完游戏的运行情况,你可能对游戏有了一定了了解: #运行游戏后,玩家首先要进行语音的选择,1选择英语,2选择汉语,其他则默认选择英语 #根据玩家选择的语音,进入不同的语音环境 ...
- ubuntu下安装stm32开发环境
在windowns下开发stm32刚开始学最烦的就是创建工程模板,都不知道为什么要那样设置,而且步骤繁多.现在我告诉大家一个好消息,在linux下配置stm32开发环境包括创建工程,使用JLink仿真 ...
- 5、数据类型三:hash
Hash数据类型使用很普遍,它同样是key-value的方式来组织的,只是其value又包含多个field-fieldValue对.想要获取某个fieldValue,可以通过key-field联合来定 ...