嘟嘟嘟




这道题其实还是挺基础的,只不过操作有点多。




区间乘和区间加按线段树的方式想。

那么就先要下放乘标记,再下放加标记。但这两个和反转标记是没有先后顺序的。

对于区间加,sum加的是区间长度\(*\)lazy标记。但是线段树区间固定,而lct不是,所以还要单独维护一个size。

还有一点,这个是splay的性质,就是当前节点的sum还要算上自己的权值,而不像线段树完全由子树信息合并而来。

最最最后一点,得开long long,包括点权。

#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<vector>
#include<stack>
#include<queue>
using namespace std;
#define enter puts("")
#define space putchar(' ')
#define Mem(a, x) memset(a, x, sizeof(a))
#define In inline
typedef long long ll;
typedef double db;
const int INF = 0x3f3f3f3f;
const db eps = 1e-8;
const int maxn = 1e5 + 5;
const ll mod = 51061;
inline ll read()
{
ll ans = 0;
char ch = getchar(), last = ' ';
while(!isdigit(ch)) last = ch, ch = getchar();
while(isdigit(ch)) ans = (ans << 1) + (ans << 3) + ch - '0', ch = getchar();
if(last == '-') ans = -ans;
return ans;
}
inline void write(ll x)
{
if(x < 0) x = -x, putchar('-');
if(x >= 10) write(x / 10);
putchar(x % 10 + '0');
} int n, q;
char c[2];
struct Tree
{
int ch[2], fa;
int siz, rev;
ll val, sum, add, mul;
}t[maxn]; In void c_rev(int now)
{
swap(t[now].ch[0], t[now].ch[1]);
t[now].rev ^= 1;
}
In void c_mul(int now, ll lzy)
{
t[now].sum *= lzy; t[now].sum %= mod;
t[now].val *= lzy; t[now].val %= mod;
t[now].mul *= lzy; t[now].mul %= mod;
t[now].add *= lzy; t[now].add %= mod;
}
In void c_add(int now, ll lzy)
{
t[now].sum += lzy * (ll)t[now].siz; t[now].sum %= mod;
t[now].val += lzy; t[now].val %= mod;
t[now].add += lzy; t[now].add %= mod;
}
In void pushdown(int now)
{
if(t[now].rev)
{
if(t[now].ch[0]) c_rev(t[now].ch[0]);
if(t[now].ch[1]) c_rev(t[now].ch[1]);
t[now].rev = 0;
}
if(t[now].mul != 1)
{
if(t[now].ch[0]) c_mul(t[now].ch[0], t[now].mul);
if(t[now].ch[1]) c_mul(t[now].ch[1], t[now].mul);
t[now].mul = 1;
}
if(t[now].add)
{
if(t[now].ch[0]) c_add(t[now].ch[0], t[now].add);
if(t[now].ch[1]) c_add(t[now].ch[1], t[now].add);
t[now].add = 0;
}
}
In void pushup(int now)
{
t[now].siz = t[t[now].ch[0]].siz + t[t[now].ch[1]].siz + 1;
t[now].sum = (t[t[now].ch[0]].sum + t[t[now].ch[1]].sum + t[now].val) % mod;
}
In bool n_root(int now)
{
return t[t[now].fa].ch[0] == now || t[t[now].fa].ch[1] == now;
}
In void rotate(int x)
{
int y = t[x].fa, z = t[y].fa, k = (t[y].ch[1] == x);
if(n_root(y)) t[z].ch[t[z].ch[1] == y] = x; t[x].fa = z;
t[y].ch[k] = t[x].ch[k ^ 1]; t[t[y].ch[k]].fa = y;
t[x].ch[k ^ 1] = y; t[y].fa = x;
pushup(y), pushup(x);
}
int st[maxn], top = 0;
In void splay(int x)
{
int y = x;
st[top = 1] = y;
while(n_root(y)) st[++top] = y = t[y].fa;
while(top) pushdown(st[top--]);
while(n_root(x))
{
int y = t[x].fa, z = t[y].fa;
if(n_root(y)) rotate(((t[y].ch[0] == x) ^ (t[z].ch[0] == y)) ? x : y);
rotate(x);
}
}
In void access(int x)
{
int y = 0;
while(x)
{
splay(x); t[x].ch[1] = y;
pushup(x);
y = x; x = t[x].fa;
}
}
In void make_root(int x)
{
access(x); splay(x);
c_rev(x);
}
In int find_root(int x)
{
access(x); splay(x);
while(t[x].ch[0]) pushdown(x), x = t[x].ch[0];
return x;
}
In void split(int x, int y)
{
make_root(x);
access(y); splay(y);
}
In void Link(int x, int y)
{
make_root(x);
if(find_root(y) != x) t[x].fa = y;
}
In void Cut(int x, int y)
{
make_root(x);
if(find_root(y) == x && t[x].fa == y && !t[x].ch[1])
t[y].ch[0] = t[x].fa = 0, pushup(y);
} int main()
{
n = read(); q = read();
for(int i = 1; i <= n; ++i) t[i].val = t[i].mul = t[i].siz = 1;
for(int i = 1, x, y; i < n; ++i) x = read(), y = read(), Link(x, y);
for(int i = 1; i <= q; ++i)
{
scanf("%s", c); int x = read(), y = read();
if(c[0] == '+') {int d = read(); split(x, y); c_add(y, d);}
else if(c[0] == '*') {int d = read(); split(x, y); c_mul(y, d);}
else if(c[0] == '/') split(x, y), write(t[y].sum), enter;
else
{
Cut(x, y);
x = read(), y = read();
Link(x, y);
}
}
return 0;
}

[国家集训队]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. 【洛谷 P1501】 [国家集训队]Tree II(LCT)

    题目链接 Tree Ⅱ\(=\)[模板]LCT+[模板]线段树2.. 分别维护3个标记,乘的时候要把加法标记也乘上. 还有就是模数的平方刚好爆\(int\),所以开昂赛德\(int\)就可以了. 我把 ...

  4. 【洛谷1501】[国家集训队] Tree II(LCT维护懒惰标记)

    点此看题面 大致题意: 有一棵初始边权全为\(1\)的树,四种操作:将两点间路径边权都加上一个数,删一条边.加一条新边,将两点间路径边权都加上一个数,询问两点间路径权值和. 序列版 这道题有一个序列版 ...

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

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

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

    题目链接 日常zz被define里没取模坑 //标记下放同线段树 注意51061^2 > 2147483647,要开unsigned int //*sz[]别忘了.. #include < ...

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

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

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

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

  9. p1501 [国家集训队]Tree II

    传送门 分析 lct板子题 单独维护一下加和乘的情况即可 维护方法和维护翻转差不多 代码 #include<iostream> #include<cstdio> #includ ...

随机推荐

  1. SpringBoot中动态加载(热部署)

    在常规的Java Web开发过程中,在修改完代码后,往往需要重启Tomcat来使得我们的修改生效,在SpringBoot中也需要从新启动SpringBoot来将修改部署.如果我们不希望重启tomcat ...

  2. iOS: Designated Initializer(指定初始化函数)

    偶然间看到“ Designated Initializer”一词,心里一惊,这是什么东西,怎么没听说过?难道是我道行太浅?真的是这样?(好伤心啊)一阵子我怀疑之后,果断上网查了一下这个 Designa ...

  3. 服务注册中心Eureka vs Zookeeper vs Consul

    前言 在现在云计算和大数据快速发展的今天,业务快速发展和变化.我们以前的单一应用难以应对这种快速的变化, 因此我们需要将以前单一的大应用不断进行差分,分成若干微小的应用或者服务,这就是微服务的思想.但 ...

  4. HDU2157(SummerTrainingDay05-F dp)

    How many ways?? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)T ...

  5. php+layui实现图片上传与预览

    端代码: <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <titl ...

  6. js-ES6学习笔记-Class(3)

    1.Class之间可以通过extends关键字实现继承. class ColorPoint extends Point { constructor(x, y, color) { super(x, y) ...

  7. es6 export与export default 的区别

    相同点: 均可用于导出常量.函数.文件.模块等 不同点: 1.在一个文件中export可以有多个,但export default 只能有一个: export var firstName = 'Mich ...

  8. Xrdp vnc password failed

    I try to login Ubuntu 16.04 xrdp through window 10 with mstsc. It was successful in 2019/01/09 night ...

  9. 完美实现Android的屏幕常亮功能

    笔者所在公司做的APP是股票类的,用户在查看股票报价页面的时候,往往需要开启盯盘模式,这个时候屏幕是不能黑屏的,黑屏会导致用户看不到一些关键报价涨跌,错过了买入卖出的最佳时机,就会给用户造成损失,这是 ...

  10. qt多线程信号槽传输方式

    //简单介绍一下QT信号与槽的连接方式:    //Qt::AutoConnection表示系统自动选择相应的连接方式,如果信号与槽在同一线程,就采用Qt::DirectConnection, //如 ...