[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 & ...
随机推荐
- MinGW安装图文教程以及如何配置C语音编程环境
MinGW安装图文教程以及如何配置C语音编程环境 转载自:http://www.jb51.net/softjc/192017.html MinGW 是一组包含文件和端口库,其功能是允许控制台模式的程序 ...
- C#调用C++编写的dll
界面还是C#写的方便点,主要是有一个可视化的编辑器,不想画太多的时间在界面上.但是自己又对C++了解的多一些,所以在需要一个良好的界面的情况下,使用C++来写代码逻辑,将其编译成一个dll,然后用C# ...
- 「日常训练」Our Tanya is Crying Out Loud (CFR466D2B)
题意(Codeforces 940B) 对一个数字$x$,你有两个决策:花费$A$减一.或花费$B$除以$k$(但必须可以除尽).问使之到$1$的最少花费. 分析 贼鸡儿简单,但我花式犯蠢……如果除不 ...
- SSH公钥认证(码云)
开发者向码云版本库写入最常用到的协议是 SSH 协议,因为 SSH 协议使用公钥认证,可以实现无口令访问,而若使用 HTTPS 协议每次身份认证时都需要提供口令.使用 SSH 公钥认证,就涉及到公钥的 ...
- Vue学习(三):数据绑定语法
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Centos6 使用yum快速搭建LAMP环境
1.安装Apache [root@localhost ~]# yum -y install httpd # 开机自启动 [root@localhost ~]# chkconfig httpd on ...
- 【app.json】配置说明,不断更新中
app.json文件用来对微信小程序进行全局配置,决定页面文件的路径.窗口表现.设置网络超时时间.设置多 tab 等. 注意: 1) json配置中键名.键值必须使用双引号,不能使用单引号. 2) 以 ...
- su: Authentication failure
su: Authentication failure问题解决: su 命令切换失败,提示su: Authentication failure,只要你sudo passwd root过一次之后,下次再s ...
- ipfs02笔记
IPFS-day02 其他常用操作 添加文件并用文件夹包裹 ipfs add xxx -w 把內容快取到本地,并提供给他人.官网文档 ipfs pin add QmT7TX5vGmFz86V8cDkP ...
- 学习shell script
摘要:概述.script的编写.test命令.[]判断符号.默认变量($1...).if...then条件判断式. 一.概述 [什么是shell script] 针对shell所写的脚本,将多个命令汇 ...