[洛谷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$,保证数据合法
- $*\;u\;v\;c:$将路径$u->v$区间乘$c$
- $/\;u\;v:$询问路径$u->v$区间和
题解:$LCT$乱搞
卡点:无
C++ Code:
#include <cstdio>
#define maxn 100010
#define lc(rt) son[rt][0]
#define rc(rt) son[rt][1]
const long long mod = 51061;
int n, q;
long long V[maxn], s[maxn], tg[maxn], M[maxn], A[maxn];
int son[maxn][2], fa[maxn], sz[maxn];
inline void swap(int &a, int &b) {a ^= b ^= a ^= b;}
inline void swap(int x) {
swap(lc(x), rc(x));
tg[lc(x)] ^= 1, tg[rc(x)] ^= 1, tg[x] = 0;
}
inline void setmul(int x) {
long long &tmp = M[x];
M[lc(x)] = M[lc(x)] * tmp % mod, M[rc(x)] = M[rc(x)] * tmp % mod;
A[lc(x)] = A[lc(x)] * tmp % mod, A[rc(x)] = A[rc(x)] * tmp % mod;
V[lc(x)] = V[lc(x)] * tmp % mod, V[rc(x)] = V[rc(x)] * tmp % mod;
s[lc(x)] = s[lc(x)] * tmp % mod, s[rc(x)] = s[rc(x)] * tmp % mod;
tmp = 1;
}
inline void setadd(int x) {
long long &tmp = A[x];
A[lc(x)] = (A[lc(x)] + tmp) % mod, A[rc(x)] = (A[rc(x)] + tmp) % mod;
V[lc(x)] = (V[lc(x)] + tmp) % mod, V[rc(x)] = (V[rc(x)] + tmp) % mod;
s[lc(x)] = (s[lc(x)] + sz[lc(x)] * tmp) % mod, s[rc(x)] = (s[rc(x)] + sz[rc(x)] * tmp) % mod;
tmp = 0;
}
inline void pushdown(int x) {
if (tg[x]) swap(x);
if (M[x] != 1) setmul(x);
if (A[x]) setadd(x);
}
inline void update(int x) {
s[x] = (s[lc(x)] + s[rc(x)] + V[x]) % mod;
sz[x] = sz[lc(x)] + sz[rc(x)] + 1;
}
inline int get(int x) {return rc(fa[x]) == x;}
inline bool isrt(int x) {return lc(fa[x]) != x && rc(fa[x]) != x;}
inline void rotate(int x) {
int y = fa[x], z = fa[y], b = get(x);
if (!isrt(y)) son[z][get(y)] = x;
fa[son[y][b] = son[x][!b]] = y; son[x][!b] = y;
fa[y] = x, fa[x] = z;
update(y), update(x);
}
int S[maxn], top;
inline void splay(int x) {
S[top = 1] = x;
for (int y = x; !isrt(y); S[++top] = y = fa[y]);
for (; top; top--) pushdown(S[top]);
for (; !isrt(x); rotate(x)) if (!isrt(fa[x]))
get(x) ^ get(fa[x]) ? rotate(x) : rotate(fa[x]);
update(x);
}
inline void access(int x) {for (int t = 0; x; rc(x) = t, t = x, x = fa[x]) splay(x);}
inline void mkrt(int x) {access(x), splay(x), tg[x] ^= 1;}
inline void link(int x, int y) {mkrt(x), fa[x] = y;}
inline void split(int x, int y) {mkrt(x), access(y), splay(y);}
inline void cut(int x, int y) {split(x, y), lc(y) = fa[x] = 0;}
inline void add(int x, int y, long long num) {
split(x, y);
A[y] = (A[y] + num) % mod;
V[y] = (V[y] + num) % mod;
s[y] = (s[y] + sz[y] * num) % mod;
}
inline void mul(int x, int y, long long num) {
split(x, y);
A[y] = A[y] * num % mod;
V[y] = V[y] * num % mod;
M[y] = M[y] * num % mod;
s[y] = s[y] * num % mod;
}
inline long long query(int x, int y) {
split(x, y);
pushdown(y);
return s[y];
} int main() {
scanf("%d%d", &n, &q);
for (int i = 1; i <= n; i++) V[i] = 1, M[i] = 1, s[i] = 1;
for (int i = 1; i < n; i++) {
int a, b;
scanf("%d%d", &a, &b);
link(a, b);
}
while (q --> 0) {
int x, y;
long long z;
char op[10];
scanf("%s%d%d", op, &x, &y);
switch (*op) {
case '+': scanf("%lld", &z), add(x, y, z); break;
case '-': cut(x, y), scanf("%d%d", &x, &y), link(x, y); break;
case '*': scanf("%lld", &z), mul(x, y, z); break;
case '/': printf("%lld\n", query(x, y));
}
}
return 0;
}
[洛谷P1501][国家集训队]Tree II的更多相关文章
- 洛谷 P1501 [国家集训队]Tree II 解题报告
P1501 [国家集训队]Tree II 题目描述 一棵\(n\)个点的树,每个点的初始权值为\(1\).对于这棵树有\(q\)个操作,每个操作为以下四种操作之一: + u v c:将\(u\)到\( ...
- 洛谷P1501 [国家集训队]Tree II(LCT,Splay)
洛谷题目传送门 关于LCT的其它问题可以参考一下我的LCT总结 一道LCT很好的练习放懒标记技巧的题目. 一开始看到又做加法又做乘法的时候我是有点mengbi的. 然后我想起了模板线段树2...... ...
- 【刷题】洛谷 P1501 [国家集训队]Tree II
题目描述 一棵n个点的树,每个点的初始权值为1.对于这棵树有q个操作,每个操作为以下四种操作之一: + u v c:将u到v的路径上的点的权值都加上自然数c: - u1 v1 u2 v2:将树中原有的 ...
- 洛谷P1501 [国家集训队]Tree II(LCT)
题目描述 一棵n个点的树,每个点的初始权值为1.对于这棵树有q个操作,每个操作为以下四种操作之一: + u v c:将u到v的路径上的点的权值都加上自然数c: - u1 v1 u2 v2:将树中原有的 ...
- 洛谷P1501 [国家集训队]Tree II(打标记lct)
题目描述 一棵n个点的树,每个点的初始权值为1.对于这棵树有q个操作,每个操作为以下四种操作之一: + u v c:将u到v的路径上的点的权值都加上自然数c: - u1 v1 u2 v2:将树中原有的 ...
- 洛谷 P1501 [国家集训队]Tree II
看来这个LCT板子并没有什么问题 #include<cstdio> #include<algorithm> using namespace std; typedef long ...
- 洛谷 P1501 [国家集训队]Tree II Link-Cut-Tree
Code: #include <cstdio> #include <algorithm> #include <cstring> #include <strin ...
- [洛谷P1501] [国家集训队]Tree II(LCT模板)
传送门 这是一道LCT的板子题,说白了就是在LCT上支持线段树2的操作. 所以我只是来存一个板子,并不会讲什么(再说我也不会,只能误人子弟2333). 不过代码里的注释可以参考一下. Code #in ...
- 洛谷.1501.[国家集训队]Tree II(LCT)
题目链接 日常zz被define里没取模坑 //标记下放同线段树 注意51061^2 > 2147483647,要开unsigned int //*sz[]别忘了.. #include < ...
随机推荐
- 【图论 思维】cf715B. Complete The Graph加强
zzq讲的杂题 题目大意 有一张$n$个点$m$条边的简单正权无向图,$S$到$T$的最短路为$L$,现在有一些边的边权未知,请输出任意一种满足题意的方案. $n,m\le 500000$ ...
- Thinkphp 取消Url默认模块的现实
例子http://www.tp.com/home/index/index 想要现实的效果是:http://www.tp.com/index/index 1是通过配置路由来达到目的 2通过配置首页的入口 ...
- 图像压缩函数imagecopyresampled
<?php //制作缩略图.图像压缩 //参数1:目的地图像资源(通常指的是画布资源) $dst_image = imagecreatetruecolor(100, 100); $color = ...
- Swoole 创建服务
1: 创建TCP 服务器 $serv = new swoole_server(‘127.0.0.1’,9501); 2:创建UDP服务器 $serv = new swoole_server('127 ...
- swoole学习(一)----linux安装swoole
1.下载swoole 登录swoole官网 https://www.swoole.com/ 点击下载,找到github或者其他链接下载下来 放到虚拟机上. 也可以使用虚拟机下载 2.登录虚拟机 推荐使 ...
- 史上最强大的wordpress后台框架redux-framework安装及使用
redux-framework的相关链接 Redux的官方网站:https://reduxframework.com/ Redux文档查询:https://docs.reduxframework.co ...
- <Docker学习>6. docker使用网络
在容器中部署一个web应用,外部如何访问? 容器与容器间如何访问? 外部访问容器 容器可以运行一些网络应用,让外部也可以访问的话,需要进行服务器和容器的端口映射 -p 或者 -P -P默认会分配一个4 ...
- PHP.TP框架下商品项目的优化4-优化商品添加表单js
优化商品添加表单js 思路 1.制作五个按钮 2.下面五个table 3.全部隐藏,点击则显示 4.点击第几个按钮就显示第几个table 具体操作 1.添加按钮 2.添加五个table并添加class ...
- Android 判断屏幕方向一个大坑
正常的判断屏幕方向的代码: /** 获取屏幕是否是竖屏 * @return */ @SuppressLint("SwitchIntDef") public boolean isSc ...
- laravel5.5事件广播系统
目录 1. 定义广播事件 1.1 广播名称 1.2 广播数据 1.3 广播队列 1.4 广播条件 2. 频道授权 2.1 定义授权路由 2.2 定义授权回调 3. 对事件进行广播 3.1 可以使用ev ...