[bzoj2631]tree——lct
Brief Description
一棵n个点的树,每个点的初始权值为1。对于这棵树有q个操作,每个操作为以下四种操作之一:
- u v c:将u到v的路径上的点的权值都加上自然数c;
- u1 v1 u2 v2:将树中原有的边(u1,v1)删除,加入一条新边(u2,v2),保证操作完之后仍然是一棵树;
- u v c:将u到v的路径上的点的权值都乘上自然数c;
/ u v:询问u到v的路径上的点的权值和,求出答案对于51061的余数。
Algorithm Design
lct裸题。考察标记下传,先下传乘法,再下传加法。
Code
#include <algorithm>
#include <cctype>
#include <cstdio>
#define ll unsigned int
const int maxn = 100005;
#define mod 51061
inline int read() {
int x = 0, f = 1;
char ch = getchar();
while (!isdigit(ch)) {
if (ch == '-') {
f = -1;
}
ch = getchar();
}
while (isdigit(ch)) {
x = x * 10 + ch - '0';
ch = getchar();
}
return x * f;
}
int n, m, top, cnt;
int ch[maxn][2], fa[maxn], size[maxn];
bool rev[maxn];
ll sum[maxn], val[maxn], at[maxn], mt[maxn];
inline bool isroot(int x) { return ch[fa[x]][0] != x && ch[fa[x]][1] != x; }
inline void cal(int k, int m, int a) {
if (k) {
val[k] = (val[k] * m + a) % mod;
sum[k] = (sum[k] * m + a * size[k]) % mod;
at[k] = (at[k] * m + a) % mod;
mt[k] = (mt[k] * m) % mod;
}
}
inline void update(int k) {
int l = ch[k][0], r = ch[k][1];
sum[k] = (sum[l] + sum[r] + val[k]) % mod;
size[k] = (size[l] + size[r] + 1) % mod;
}
inline void pushdown(int k) {
int &l = ch[k][0], &r = ch[k][1];
if (rev[k]) {
rev[k] ^= 1, rev[l] ^= 1, rev[r] ^= 1;
std::swap(l, r);
}
int m = mt[k], a = at[k];
mt[k] = 1, at[k] = 0;
if (m != 1 || a != 0) {
cal(l, m, a);
cal(r, m, a);
}
}
inline void zig(int x) {
int y = fa[x], z = fa[y], l = (ch[y][1] == x), r = l ^ 1;
if (!isroot(y))
ch[z][ch[z][1] == y] = x;
fa[ch[y][l] = ch[x][r]] = y;
fa[ch[x][r] = y] = x;
fa[x] = z;
update(y);
update(x);
}
inline void splay(int x) {
int s[maxn], top = 0;
s[++top] = x;
for (int i = x; !isroot(i); i = fa[i])
s[++top] = fa[i];
while (top)
pushdown(s[top--]);
for (int y; !isroot(x); zig(x))
if (!isroot(y = fa[x]))
zig((ch[fa[y]][0] == y) == (ch[y][0] == x) ? y : x);
}
inline void access(int x) {
for (int t = 0; x; t = x, x = fa[x]) {
splay(x);
ch[x][1] = t;
update(x);
}
}
inline void makeroot(int x) {
access(x);
splay(x);
rev[x] ^= 1;
}
inline void split(int x, int y) {
makeroot(y);
access(x);
splay(x);
}
inline void link(int x, int y) {
makeroot(x);
fa[x] = y;
}
inline void cut(int x, int y) {
makeroot(x);
access(y);
splay(y);
ch[y][0] = fa[x] = 0;
}
int main() {
#ifndef ONLINE_JUDGE
freopen("input", "r", stdin);
#endif
n = read(), m = read();
int u, v, c;
for (int i = 1; i <= n; i++)
val[i] = sum[i] = size[i] = mt[i] = 1;
for (int i = 1; i < n; i++) {
u = read(), v = read();
link(u, v);
}
char st[5];
while (m--) {
scanf("%s", st);
u = read(), v = read();
if (st[0] == '+') {
c = read();
split(u, v);
cal(u, 1, c);
}
if (st[0] == '-') {
cut(u, v);
u = read(), v = read(), link(u, v);
}
if (st[0] == '*') {
c = read();
split(u, v);
cal(u, c, 0);
}
if (st[0] == '/') {
split(u, v);
printf("%d\n", sum[u]);
}
}
}
[bzoj2631]tree——lct的更多相关文章
- bzoj2631: tree lct
要打mul和add的lct 50000+的mod用unsigned int好了TAT (坑爹没打pc('\n');(静态)调了好久,样例竟然只输出一个,orz,也不提示PE T_T) #include ...
- bzoj2631 tree LCT 区间修改,求和
tree Time Limit: 30 Sec Memory Limit: 128 MBSubmit: 4962 Solved: 1697[Submit][Status][Discuss] Des ...
- 【bzoj2631】tree LCT
题目描述 一棵n个点的树,每个点的初始权值为1.对于这棵树有q个操作,每个操作为以下四种操作之一:+ u v c:将u到v的路径上的点的权值都加上自然数c:- u1 v1 u2 v2:将树中原有的边( ...
- BZOJ2631 tree(伍一鸣) LCT 秘制标记
这个题一看就是裸地LCT嘛,但是我wa了好几遍,这秘制标记...... 注意事项:I.*对+有贡献 II.先下传*再下传+(因为我们已经维护了+,不能再让*对+产生贡献)III.维护+用到size # ...
- [BZOJ2631]tree 动态树lct
2631: tree Time Limit: 30 Sec Memory Limit: 128 MBSubmit: 5171 Solved: 1754[Submit][Status][Discus ...
- BZOJ2631 tree 【LCT】
题目 一棵n个点的树,每个点的初始权值为1.对于这棵树有q个操作,每个操作为以下四种操作之一: + u v c:将u到v的路径上的点的权值都加上自然数c: - u1 v1 u2 v2:将树中原有的边( ...
- BZOJ2631: tree(LCT)
Description 一棵n个点的树,每个点的初始权值为1.对于这棵树有q个操作,每个操作为以下四种操作之一: + u v c:将u到v的路径上的点的权值都加上自然数c: - u1 v1 u2 v2 ...
- bzoj2631: tree
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #i ...
- BZOJ2631——tree
1.题目大意:bzoj1798的lct版本 2.分析:这个把线段树改成splay就好 #include <stack> #include <cstdio> #include & ...
随机推荐
- Spring研磨分析、Quartz任务调度、Hibernate深入浅出系列文章笔记汇总
Spring研磨分析.Quartz任务调度.Hibernate深入浅出系列文章笔记汇总 置顶2017年04月27日 10:46:45 阅读数:1213 这系列文章主要是对Spring.Quartz.H ...
- 【Luogu P1935】[国家集训队]圈地计划
题目 最近房地产商 GDOI (Group of Dumbbells Or Idiots) 从 NOI (Nuts Old Idiots) 手中得到了一块开发土地. 据了解,这块土地是一块矩形的区域, ...
- 【个人训练】(POJ3279)Fliptile
最近在刷kuangbin神犇的各种套题....感觉自己好弱啊.....还是要多多训练,跟上大神的脚步.最近的这十几题都比较水,记下来这一条我比较印象深刻.也比较难的题目吧(之后应该不会再有水题写了,珍 ...
- 使用Vue-cli 3.x搭建Vue项目
一.Vue-cli 3.x安装 Node 版本要求:Vue CLI 需要 Node.js 8.9 或更高版本 (推荐 8.11.0+) npm install -g @vue/cli 查版本是否正确 ...
- [HNOI2012]三角形覆盖问题
题面 二维平面中,给定 \(N\) 个等腰直角三角形(每个三角形的两条直角边分别平行于坐标轴,斜边从左上到右下).我们用三个非负整数 \((x, y, d)\) 来描述这样一个三角形,三角形三个顶点的 ...
- 从源码安装opencv
操作系统为Debian9,由于使用apt-get安装在/usr/lib目录下的opencv可能会造成一些项目上的头文件错误问题,所以选择了从源码安装. 选择opencv3.4.1, 进入https:/ ...
- OpenPAI:大规模人工智能集群管理平台介绍及任务提交指南
产品渊源: 随着人工智能技术的快速发展,各种深度学习框架层出不穷,为了提高效率,更好地让人工智能快速落地,很多企业都很关注深度学习训练的平台化问题.例如,如何提升GPU等硬件资源的利用率?如何节省硬件 ...
- Python 3 学习笔记之——变量作用域、模块和包
1. 变量作用域 Python 中,程序的变量并不是在哪个位置都可以访问的,访问权限决定于这个变量是在哪里赋值的.变量的作用域决定了在哪一部分程序可以访问哪个特定的变量名称.Python 的作用域一共 ...
- URAL 1741 Communication Fiend(最短路径)
Description Kolya has returned from a summer camp and now he's a real communication fiend. He spends ...
- WEBSTORM中html文件运行之后出现乱码的问题解决
出现如下问题: 解决方案: 1.点击"文件编码" 2.选择GBK 3.点击Reload. 4.此时,源代码中的中文字体会变成乱码,把这些乱码重新输入成原先的中文.然后运行html文 ...