推荐几篇比较好的博客:

FlashHu 的 讲解比较好 : 传送门

Candy 的 代码~ : 传送门

以及神犇Angel_Kitty的 学习笔记: 传送门

Code

V 模板
 #include<cstdio>
#include<cstring>
#include<algorithm>
#define rd read()
#define ll long long
using namespace std; const int N = 1e5 + ;
const int mod = ; int n, m; int read() {
int X = , p = ; char c = getchar();
for (; c > '' || c < ''; c = getchar())
if (c == '-') p = -;
for (; c >= '' && c <= ''; c = getchar())
X = X * + c - '';
return X * p;
} namespace LCT {
int ch[N][], tun[N], f[N], size[N];
ll sum[N], val[N], tim[N], ad[N];
#define lc(x) ch[x][0]
#define rc(x) ch[x][1]
int isroot(int x) {
return lc(f[x]) != x && rc(f[x]) != x;
} int get(int x) {
return rc(f[x]) == x;
} void up(int x) {
sum[x] = val[x];
size[x] = ;
if (lc(x)) sum[x] += sum[lc(x)], size[x] += size[lc(x)];
if (rc(x)) sum[x] += sum[rc(x)], size[x] += size[rc(x)];
sum[x] %= mod;
} void time(int x, ll d) {
val[x] = val[x] * d % mod;
sum[x] = sum[x] * d % mod;
ad[x] = ad[x] * d % mod;
tim[x] = tim[x] * d % mod;
} void add(int x, ll d) {
sum[x] = (sum[x] + d * size[x]) % mod;
val[x] = (val[x] + d) % mod;
ad[x] = (ad[x] + d) % mod;
} void rev(int x) {
swap(lc(x), rc(x));
tun[x] ^= ;
} void pushdown(int x) {
if (tim[x] != ) {
if (lc(x)) time(lc(x), tim[x]);
if (rc(x)) time(rc(x), tim[x]);
tim[x] = ;
}
if (ad[x]) {
if (lc(x)) add(lc(x), ad[x]);
if (rc(x)) add(rc(x), ad[x]);
ad[x] = ;
}
if (tun[x]) {
if (lc(x)) rev(lc(x));
if (rc(x)) rev(rc(x));
tun[x] = ;
}
} void pd(int x) {
if (!isroot(x))
pd(f[x]);
pushdown(x);
} void rotate(int x) {
int old = f[x], oldf = f[old], son = ch[x][get(x) ^ ];
if (!isroot(old)) ch[oldf][get(old)] = x;
ch[x][get(x) ^ ] = old;
ch[old][get(x)] = son;
f[old] = x; f[x] = oldf; f[son] = old;
up(old); up(x);
} void splay(int x) {
pd(x);
for (; !isroot(x); rotate(x))
if(!isroot(f[x]))
rotate(get(f[x]) == get(x) ? f[x] : x);
} void access(int x) {
for (int y = ; x; y = x, x = f[x])
splay(x), ch[x][] = y, up(x);
} void mroot(int x) {
access(x); splay(x); rev(x);
} void split(int x, int y) {
mroot(x); access(y); splay(y);
} void link(int x, int y) {
mroot(x);
f[x] = y;
} void cut(int x, int y) {
split(x, y);
f[x] = ch[y][] = ;
up(y);
}
}
using namespace LCT; int main()
{
n = rd; m = rd;
for (int i = ; i <= n; ++i)
size[i] = tim[i] = val[i] = sum[i] = ;
for (int i = ; i < n; ++i) {
int u = rd, v = rd;
link(u, v);
}
for (; m; m--) {
char op[];
scanf("%s", op);
if (op[] == '+') {
int u = rd, v = rd, d = rd;
split(u, v);
add(v, d);
}
if (op[] == '-') {
int u = rd, v = rd;
cut(u, v);
u = rd; v = rd;
link(u, v);
}
if (op[] == '*') {
int u = rd, v = rd, d = rd;
split(u, v);
time(v, d);
}
if (op[] == '/') {
int u = rd, v = rd;
split(u, v);
printf("%lld\n", sum[v] % mod);
}
}
}

Luogu1501 - 链修改模板

Luogu 3690 LCT - 模板的更多相关文章

  1. [Luogu 3690]【模板】Link Cut Tree (动态树)

    Description 给定N个点以及每个点的权值,要你处理接下来的M个操作.操作有4种.操作从0到3编号.点从1到N编号. 0:后接两个整数(x,y),代表询问从x到y的路径上的点的权值的xor和. ...

  2. luogu 3690 【模板】 Link Cut Tree (动态树)

    原来的代码有一些问题. 主要是对于不一定存在的边如何去判断,首先要保证在一个splay里,然后保证彼此之间直接联通且x的右儿子是空的 #include<iostream> #include ...

  3. Luogu 3690 Link Cut Tree

    Luogu 3690 Link Cut Tree \(LCT\) 模板题.可以参考讲解和这份码风(个人认为)良好的代码. 注意用 \(set\) 来维护实际图中两点是否有直接连边,否则无脑 \(Lin ...

  4. LCT模板

    之前一直用的LCT模板,因为其实个人对LCT和Splay不是很熟,所以用起来总觉得略略的坑爹,过了一段时间就忘了,但事实上很多裸的LCT要改的东西是不多的,所以今天写了些注释,以后可能套起模板来会得心 ...

  5. Luogu 3390 【模板】矩阵快速幂 (矩阵乘法,快速幂)

    Luogu 3390 [模板]矩阵快速幂 (矩阵乘法,快速幂) Description 给定n*n的矩阵A,求A^k Input 第一行,n,k 第2至n+1行,每行n个数,第i+1行第j个数表示矩阵 ...

  6. Luogu 3375 【模板】KMP字符串匹配(KMP算法)

    Luogu 3375 [模板]KMP字符串匹配(KMP算法) Description 如题,给出两个字符串s1和s2,其中s2为s1的子串,求出s2在s1中所有出现的位置. 为了减少骗分的情况,接下来 ...

  7. LCT 模板及套路总结

    这一个月貌似已经考了无数次\(LCT\)了..... 保险起见还是来一发总结吧..... A. LCT 模板 \(LCT\) 是由大名鼎鼎的 \(Tarjan\) 老爷发明的. 主要是用来维护树上路径 ...

  8. Luogu 3371【模板】单源最短路径

    Luogu 3371[模板]单源最短路径 第一次写博客用图论题来试一试 接下来是正文部分 题目描述 如题,给出一个有向图,请输出从某一点出发到所有点的最短路径长度. 输入输出格式 输入格式: 第一行包 ...

  9. [luogu P3806] 【模板】点分治1

    [luogu P3806] [模板]点分治1 题目背景 感谢hzwer的点分治互测. 题目描述 给定一棵有n个点的树 询问树上距离为k的点对是否存在. 输入输出格式 输入格式: n,m 接下来n-1条 ...

随机推荐

  1. HTML 设置字体

    HTML,CSS,font-family:中文字体的英文名称 (宋体 微软雅黑)     宋体 SimSun 黑体 SimHei 微软雅黑 Microsoft YaHei 微软正黑体 Microsof ...

  2. html实现导航栏效果

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  3. jvm 几个invoke 指令

    invokestatic : 调用静态方法 invokespecial : 调用实例构造器<init>方法, 私有方法和父类方法 invokevirtual : 调用虚方法 invokei ...

  4. xnconvert 图片转换工具

    xnconvert是一款简单高效的图片转换工具.xnconvert能够批量地进行图片格式转换,并具有一定的图片处理功能,可以增加水印.特效,支持放大缩小.旋转等. xnconvert功能介绍: 你可以 ...

  5. slf4j + log4j 需要的依赖

    正确的依赖 <!-- slf4j 依赖包 --> <dependency> <groupId>org.slf4j</groupId> <artif ...

  6. RelativeLayout 相对布局

    根据父容器来定位: 想位于哪,哪个属性就设置为true 左对齐:android:layout_alighParentLeft 右对齐:android:layout_alighParentRight 顶 ...

  7. 公告栏添加时钟——利用canvas画出一个时钟

    前言 最近在学习HTML5标签,学到Canvas,觉得很有趣.便在慕课网找了个demo练手.就是Canvas时钟. 对于canvas,w3shcool上是这么描述的: HTML5 <canvas ...

  8. composer windows安装,使用新手入门

    一.前期准备: 1.下载安装包,https://getcomposer.org/download/ 2.在php.ini文档中打开extension=php_openssl.dll 3.下载php_s ...

  9. How to set the bash display to not show the vim text after exit?

    Xshell客户端在vim编辑文件保存退出,仍然显示文本内容,而不是回到shell terminal终端. 解决办法如下: User1 is using TERM=xterm, in this cas ...

  10. HDU 4027 Can you answer these queries? (线段树区间修改查询)

    描述 A lot of battleships of evil are arranged in a line before the battle. Our commander decides to u ...