题目链接

日常zz被define里没取模坑

//标记下放同线段树 注意51061^2 > 2147483647,要开unsigned int
//*sz[]别忘了。。
#include <cstdio>
#include <cctype>
#include <algorithm>
#define gc() getchar()
#define mod (51061)
typedef unsigned int uint;
const int N=1e5+5; inline int read()
{
int now=0;register char c=gc();
for(;!isdigit(c);c=gc());
for(;isdigit(c);now=now*10+c-'0',c=gc());
return now;
}
namespace LCT
{
#define lson son[x][0]
#define rson son[x][1]
#define ADD(x,v) (x)+=(v), (x)%=mod//直接这样最快
// #define ADD(x,v) (x)+=(v), (x)>=mod?(x)-=mod:0
// #define ADD(x,v) (x)+=(v); if((x)>=mod) (x)-=mod//比上一行快一点 int son[N][2],fa[N],sk[N],sz[N];
uint val[N],sum[N],m_tag[N],a_tag[N];
bool r_tag[N];
inline void Update(int x){
sz[x]=sz[lson]+sz[rson]+1;
sum[x]=(sum[lson]+sum[rson]+val[x])%mod;
}
inline void Rev(int x){
std::swap(lson,rson), r_tag[x]^=1;
}
inline void Mult(int x,uint v){
(sum[x]*=v)%=mod, (val[x]*=v)%=mod, (m_tag[x]*=v)%=mod, (a_tag[x]*=v)%=mod;
}
inline void Add(int x,uint v){
// sum[x]+=v*sz[x]%mod, sum[x]>=mod?sum[x]-=mod:0, val[x]+=v, val[x]>=mod?val[x]-=mod:0, a_tag[x]+=v, a_tag[x]>=mod?a_tag[x]-=mod:0;
// ADD(sum[x],v*sz[x]%mod/*2.3 ADD这要取模!*/); ADD(val[x],v); ADD(a_tag[x],v);
ADD(sum[x],v*sz[x]); ADD(val[x],v); ADD(a_tag[x],v);
}
void PushDown(int x){
if(m_tag[x]!=1) Mult(lson,m_tag[x]),Mult(rson,m_tag[x]),m_tag[x]=1;
if(a_tag[x]) Add(lson,a_tag[x]),Add(rson,a_tag[x]),a_tag[x]=0;
if(r_tag[x]) Rev(lson),Rev(rson),r_tag[x]=0;
}
inline bool n_root(int x){
return son[fa[x]][0]==x||son[fa[x]][1]==x;
}
void Rotate(int x)
{
int a=fa[x],b=fa[a],l=son[a][1]==x,r=l^1;
if(n_root(a)) son[b][son[b][1]==a]=x;
if(son[x][r]) fa[son[x][r]]=a;
fa[a]=x, fa[x]=b, son[a][l]=son[x][r], son[x][r]=a;
Update(a);
}
void Splay(int x)
{
int t=1,a=x,b; sk[1]=x;
while(n_root(a)) sk[++t]=a=fa[a];
while(t) PushDown(sk[t--]);
while(n_root(x))
{
a=fa[x],b=fa[a];
if(n_root(a)) Rotate(son[a][1]==x^son[b][1]==a?x:a);
Rotate(x);
}
Update(x);
}
void Access(int x){
for(int pre=0; x; x=fa[pre=x])
Splay(x), rson=pre, Update(x);
}
void Make_root(int x){
Access(x), Splay(x), Rev(x);
}
void Split(int x,int y){
Make_root(x), Access(y), Splay(y);
}
// int Find_root(int x){
// Access(x), Splay(x);
// while(lson) x=lson;
// return x;
// }
void Link(int x,int y){
Make_root(x), fa[x]=y;
}
void Cut(int x,int y){
Split(x,y), fa[x]=son[y][0]=0, Update(y);
// Make_root(x);
// if(Find_root(y)==x&&fa[x]==y&&!rson)
// fa[x]=son[y][0]=0, Update(y);
}
} int main()
{
int n=read(),q=read();
for(int i=1; i<=n; ++i) LCT::sz[i]=LCT::sum[i]=LCT::val[i]=LCT::m_tag[i]=1;
for(int u,v,i=1; i<n; ++i) u=read(),v=read(),LCT::Link(u,v);
int u,v,c,d; char opt[3];
while(q--)
{
scanf("%s",opt),u=read(),v=read();
if(opt[0]=='+') c=read(),LCT::Split(u,v),LCT::Add(v,c);
else if(opt[0]=='-') c=read(),d=read(),LCT::Cut(u,v),LCT::Link(c,d);
else if(opt[0]=='*') c=read(),LCT::Split(u,v),LCT::Mult(v,c);
else LCT::Split(u,v),printf("%d\n",LCT::sum[v]);
}
return 0;
}

洛谷.1501.[国家集训队]Tree II(LCT)的更多相关文章

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

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

  2. 洛谷 1501 [国家集训队]Tree II BZOJ 2631 Tree

    [题解] 维护乘法标记和加法标记的LCT #include<cstdio> #include<algorithm> #define Mod (51061) #define N ...

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

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

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

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

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

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

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

    传送门 这是一道LCT的板子题,说白了就是在LCT上支持线段树2的操作. 所以我只是来存一个板子,并不会讲什么(再说我也不会,只能误人子弟2333). 不过代码里的注释可以参考一下. Code #in ...

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

    题目描述 一棵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. 使用matplotlib绘制多个图形单独显示

    使用matplotlib绘制多个图形单独显示 一 代码 import numpy as np import matplotlib.pyplot as plt #创建自变量数组 x= np.linspa ...

  2. js 当前时区

    function formatDateTime(formatDate){ //13位时间戳,java js. (php时间戳为10位) var returnDate; if(formatDate == ...

  3. Linux input子系统简介

    1.前言 本文主要对Linux下的input子系统进行介绍 2. 软件架构 图 input子系统结构图 input子系统主要包括三个部分:设备驱动层.核心层和事件层.我们可以分别理解为:具体的输入设备 ...

  4. sqlserver中将查询结果拼接成字符串

    #for xml path(param)--将查询结果以xml格式输出 select id,name from table1 for xml path --id和name为table1的真实字段 - ...

  5. oracle进阶之connect by笔记

    本博客是自己在学习和工作途中的积累与总结,仅供自己参考,也欢迎大家转载,转载时请注明出处. http://www.cnblogs.com/king-xg/p/6794562.html 如果觉得对您有帮 ...

  6. LoadRunner性能测试入门教程

    javaweb性能测试那些事 一:什么是javaweb性能测试: 二:javaweb性能测试基本流程 三:javaweb性能测试常用指标: 1:响应时间:2-5-8 原则 2:吞吐量 3:资源使用率 ...

  7. 为什么js中0.1+0.2不等于0.3,怎样处理使之相等?(转载)

    为什么js中0.1+0.2不等于0.3,怎样处理使之相等? console.log(0.1+0.2===0.3)// true or false?? 在正常的数学逻辑思维中,0.1+0.2=0.3这个 ...

  8. hdu2476

    /* dp[l][r]表示将任意串的[l,r]刷成s2样子的最小代价 ans[i]表示将s1的前i位刷成s2的代价 按照区间dp的常用做法,dp[l][r]的状态由dp[l][k],dp[k+1][r ...

  9. 2017-2018-2 20155225《网络对抗技术》实验八 Web基础

    2017-2018-2 20155225<网络对抗技术>实验八 Web基础 1.Web前端HTML 输入命令apachectl start打开apahce,并使用netstat -aptn ...

  10. IntelliJ IDEA快捷键:Ctrl+Shift+空格

    The smart type code completion may be used after the new keyword,to instantiate an object of the exp ...