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 ...
随机推荐
- 语义分割【semantic-segmentation】资料备忘
https://github.com/mrgloom/awesome-semantic-segmentation
- JFreeChart API 说明(转)
原地址 http://blog.csdn.net/mike_caoyong/article/details/7338160 JFreeChart目前是最好的java图形解决方案,基本能够解决目前的图形 ...
- FPGA设计中的float
在通常的设计中,不建议使用浮点数计算,因为浮点数计算会消耗FPGA大量的逻辑单元.但很多情况下,又需要使用浮点数进行计算提高精度. 所以需要有合适的方法计算浮点运算. 正常情况下FPGA只能以整形数据 ...
- 如何配置Python环境
(1) 下载:请在Python官网下载页面(https://www.python.org/downloads/)选择合适的版本(建议选择3.5.2版)的链接,在该版本的下载页面选择合适的安装文件:64 ...
- 了解IHttpModule接口事件执行顺便 获取Session
本文转载自:http://www.cnblogs.com/eflylab/archive/2008/05/29/1209767.html 最近公司一个项目让人SQL注入了-为了临时先解决这个问题,使攻 ...
- (转)win7+iis7.5+asp.net下 CS0016: 未能写入输出文件“c:\Windows\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files 解决方案
本文转载自:http://www.cnblogs.com/finesite/archive/2011/01/28/1946940.html 网上搜的解决方案但在我的环境下仍旧没有解决,我的方法如下: ...
- jquery Autocomplete函数
<!doctype html> <html lang="en"> <head> <meta charset="utf-8&quo ...
- 【UVA】10391 Compound Words(STL map)
题目 题目 分析 自认已经很简洁了,虽说牺牲了一些效率 代码 #include <bits/stdc++.h> using namespace std; set <s ...
- [安全分享]斗鱼&360补天沙龙分享-跨域资源那些事
[安全分享]斗鱼&360补天沙龙分享-跨域资源那些事 主要内容: 文件: http://scan.javasec.cn/补天&斗鱼-跨域资源那些事.pdf
- 深入浅出 Java Concurrency (10): 锁机制 part 5 闭锁 (CountDownLatch)
此小节介绍几个与锁有关的有用工具. 闭锁(Latch) 闭锁(Latch):一种同步方法,可以延迟线程的进度直到线程到达某个终点状态.通俗的讲就是,一个闭锁相当于一扇大门,在大门打开之前所有线程都被阻 ...