传送门

分析

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 ;
}

最新文章

  1. java ArrayList 实现
  2. Struts 2 Spring Hibernate三大框架的执行流程以及原理
  3. OpenGL的glPushMatrix和glPopMatrix矩阵栈顶操作函数详解
  4. linux http请求监控工具httpry---官方文档
  5. 部署MongoDB扩展并测试使用php简单连接操作之
  6. GNU Make 学习系列一:怎样写一个简单的Makefile
  7. Android 性能测试——Memory Monitor 工具
  8. JavaScript语法详解:JS简介&amp;变量
  9. Go语言基础之结构体
  10. matplotlib坐标轴刻度-【老鱼学matplotlib】
  11. REST POST PUT差别
  12. 40.SEO----前端该懂的seo技巧
  13. Skyline开发2-第一个程序
  14. (转)使用FFMPEG类库分离出多媒体文件中的H.264码流
  15. Extjs表单验证小结
  16. Eclipse警告:The serializable class XXX does not declare a static final serialVersionUID field of type long
  17. .net环境下的缓存技术-转载!
  18. 已使用 163 邮箱测试通过,且支持 SSL 连接。 发送邮件
  19. 关于Java中语句符号及格式的理解
  20. python学习之老男孩python全栈第九期_day016知识点总结

热门文章

  1. NETCTOSS - 中国电信运营支持系统-网络版_V-1.0
  2. golang的slice作为函数参数传值的坑
  3. linux(8)
  4. ngui自适应
  5. 前段基础之HTML
  6. (转)python virtual_env 的使用 + 将原来的虚拟环境部署到新环境
  7. GridControl 添加全选列
  8. python覆盖率统计
  9. CentOS7.6安装稳定版Nginx
  10. Mysql总结(二)