传送门

分析

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的更多相关文章

  1. P1501 [国家集训队]Tree II(LCT)

    P1501 [国家集训队]Tree II 看着维护吧2333333 操作和维护区间加.乘线段树挺像的 进行修改操作时不要忘记吧每个点的点权$v[i]$也处理掉 还有就是$51061^2=2607225 ...

  2. 洛谷 P1501 [国家集训队]Tree II 解题报告

    P1501 [国家集训队]Tree II 题目描述 一棵\(n\)个点的树,每个点的初始权值为\(1\).对于这棵树有\(q\)个操作,每个操作为以下四种操作之一: + u v c:将\(u\)到\( ...

  3. BZOJ 2631 tree | Luogu P1501 [国家集训队]Tree II (LCT 多重标记下放)

    链接:https://www.luogu.org/problemnew/show/P1501 题面: 题目描述 一棵n个点的树,每个点的初始权值为1.对于这棵树有q个操作,每个操作为以下四种操作之一: ...

  4. 洛谷P1501 [国家集训队]Tree II(LCT,Splay)

    洛谷题目传送门 关于LCT的其它问题可以参考一下我的LCT总结 一道LCT很好的练习放懒标记技巧的题目. 一开始看到又做加法又做乘法的时候我是有点mengbi的. 然后我想起了模板线段树2...... ...

  5. 【刷题】洛谷 P1501 [国家集训队]Tree II

    题目描述 一棵n个点的树,每个点的初始权值为1.对于这棵树有q个操作,每个操作为以下四种操作之一: + u v c:将u到v的路径上的点的权值都加上自然数c: - u1 v1 u2 v2:将树中原有的 ...

  6. 洛谷P1501 [国家集训队]Tree II(LCT)

    题目描述 一棵n个点的树,每个点的初始权值为1.对于这棵树有q个操作,每个操作为以下四种操作之一: + u v c:将u到v的路径上的点的权值都加上自然数c: - u1 v1 u2 v2:将树中原有的 ...

  7. 洛谷P1501 [国家集训队]Tree II(打标记lct)

    题目描述 一棵n个点的树,每个点的初始权值为1.对于这棵树有q个操作,每个操作为以下四种操作之一: + u v c:将u到v的路径上的点的权值都加上自然数c: - u1 v1 u2 v2:将树中原有的 ...

  8. [洛谷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$, ...

  9. 洛谷 P1501 [国家集训队]Tree II

    看来这个LCT板子并没有什么问题 #include<cstdio> #include<algorithm> using namespace std; typedef long ...

随机推荐

  1. idea之jrebel热部署使用教程

    JRebel是一个J2EE热部署的工具.使用它可以减少浪费8-18%的开发时间在项目的构建和部署上.虽然Java也提供了HotSpot的JVM,但是如果你修改的类中有方法名称变动的话,HotSpot就 ...

  2. 第八章 搭建hadoop2.2.0集群,Zookeeper集群和hbase-0.98.0-hadoop2-bin.tar.gz集群

    安装配置jdk,SSH 一.首先,先搭建三台小集群,虚拟机的话,创建三个 下面为这三台机器分别分配IP地址及相应的角色:集群有个特点,三台机子用户名最好一致,要不你就创建一个组,把这些用户放到组里面去 ...

  3. Flask之模板之特殊变量和方法

    3.6 Flask中的特殊变量和方法: 在Flask中,有一些特殊的变量和方法是可以在模板文件中直接访问的. config 对象: config 对象就是Flask的config对象,也就是 app. ...

  4. 有关C#中的引用类型的内存问题

    对于一个类,如果定义后(记作对象a),将另外一个对象b直接赋值(“a = b”)给它,则相当于将地址赋值给了这个对象.当另外一个对象b不再对这块地址应用时,a由于对这块地址仍在使用,这块地址的指向的栈 ...

  5. Visual Studio 进行Excel相关开发,Microsoft.Office.Interop.Excel.dll库

    1. Interop.Excel.dll 的查找 本文中将 Microsoft.Office.Interop.Excel.dll库简称为Interop.Excel.dll库 其实在使用Visual S ...

  6. leetcode859

    class Solution { public: bool buddyStrings(string A, string B) { if (A.length() != B.length()) { ret ...

  7. DDoS攻防战 (四):CC攻击防御系统部署

    1. 系统效果 此DDOS应用层防御系统已经部署在了http://www.yfdc.org网站上(如果访问失败,请直接访问位于国内的服务器http://121.42.45.55进行在线测试). 此防御 ...

  8. Oracle11gR2导入导出实战之表空间传输

    Oracle11gR2导入导出实战之使用Datapump进行表空间传输 表空间检查 [oracle@localhost database]$ ps -ef|grep smon oracle 8981 ...

  9. Spring cloud Eureka高可用 - Windows 7 hosts文件立即生效

    hosts 文件所在位置 c:/windows/system32/drivers/etc/hosts 左下角 搜索框 搜索 cmd 弹出命令框 输入 ipconfig /displaydns 显示所有 ...

  10. sqlite在终端中输入命令不显示

    问题: 今天通过命令想访问我设备里面的db文件,但是进入到 sqlite> 后,输入命令都是不显示的,但是回车是可以执行的.如图 经过一番排查后,发现,因为我前面使用了su命令,不要使用su命令 ...