P1501 [国家集训队]Tree II LCT
链接
思路
简单题
代码
#include <bits/stdc++.h>
#define ls c[x][0]
#define rs c[x][1]
using namespace std;
const int N = 1e5 + 7, mod = 51061;
int read() {
int x = 0, f = 1; char s = getchar();
for (;s > '9' || s < '0'; s = getchar()) if (s == '-') f = -1;
for (;s >= '0' && s <= '9'; s = getchar()) x = x * 10 + s - '0';
return x * f;
}
int f[N], c[N][2], w[N], siz[N], lazy[N], stak[N];
int add[N], mul[N], sum[N];
bool isroot(int x) {return c[f[x]][0] == x || c[f[x]][1] == x;}
void tag(int x) {swap(ls, rs), lazy[x] ^= 1;}
void tag1(int x, int ad) {
add[x] = (add[x] + ad) % mod;
sum[x] = (sum[x] + 1LL * ad * siz[x] % mod) % mod;
w[x] = (w[x] + ad) % mod;
}
void tag2(int x, int mu) {
mul[x] = 1LL * mul[x] * mu % mod;
add[x] = 1LL * add[x] * mu % mod;
sum[x] = 1LL * sum[x] * mu % mod;
w[x] = 1LL * w[x] * mu % mod;
}
void pushup(int x) {
sum[x] = (sum[ls] + sum[rs] + w[x]) % mod;
siz[x] = siz[ls] + siz[rs] + 1;
}
void pushdown(int x) {
if (lazy[x]) {
if (ls) tag(ls);
if (rs) tag(rs);
lazy[x] ^= 1;
}
if (mul[x] != 1) {
if (ls) tag2(ls, mul[x]);
if (rs) tag2(rs, mul[x]);
mul[x] = 1;
}
if (add[x]) {
if (ls) tag1(ls, add[x]);
if (rs) tag1(rs, add[x]);
add[x] = 0;
}
}
void rotate(int x) {
int y = f[x], z = f[y], k = c[y][1] == x, w = c[x][!k];
if (isroot(y)) c[z][c[z][1] == y] = x;
c[x][!k] = y;
c[y][k] = w;
if (w) f[w] = y;
f[x] = z;
f[y] = x;
pushup(y);
}
void splay(int x) {
int y = x, z = 0;
stak[++z] = y;
while (isroot(y)) stak[++z] = y = f[y];
while (z) pushdown(stak[z--]);
while (isroot(x)) {
y = f[x], z = f[y];
if (isroot(y)) rotate((c[y][0] == x)^(c[z][0] == y) ? x : y);
rotate(x);
}
pushup(x);
}
void access(int x) {
for (int y = 0; x;x = f[y = x])
splay(x), rs = y, pushup(x);
}
void makeroot(int x) {
access(x), splay(x), tag(x);
}
int findroot(int x) {
access(x), splay(x);
while(ls) pushdown(x), x = ls;
return x;
}
void split(int x, int y) {
makeroot(x), access(y), splay(y);
}
void link(int x, int y) {
makeroot(x);
if (findroot(y) != x) f[x] = y;
}
void cut(int x, int y) {
makeroot(x);
if (findroot(y) == x && f[x] == y && !rs) {
f[x] = c[y][0] = 0;
pushup(y);
}
}
int main() {
int n = read(), q = read();
for (int i = 1; i <= n; ++i) w[i] = mul[i] = 1;
for (int i = 1; i < n; ++i) {
int u = read(), v = read();
link(u, v);
}
char s[10];
for (int i = 1; i <= q; ++i) {
scanf("%s", s);
if (s[0] == '+') {
int u = read(), v = read(), c = read();
split(u, v), tag1(v, c);
}
if (s[0] == '-') {
int u = read(), v = read(), u1 = read(), v1 = read();
cut(u, v),link(u1, v1);
}
if (s[0] == '*') {
int u = read(), v = read(), c = read();
split(u, v), tag2(v, c);
}
if (s[0] == '/') {
int u = read(), v = read();
split(u, v), printf("%d\n", sum[v]);
}
}
return 0;
}
P1501 [国家集训队]Tree II LCT的更多相关文章
- 洛谷P1501 [国家集训队]Tree II(LCT)
题目描述 一棵n个点的树,每个点的初始权值为1.对于这棵树有q个操作,每个操作为以下四种操作之一: + u v c:将u到v的路径上的点的权值都加上自然数c: - u1 v1 u2 v2:将树中原有的 ...
- BZOJ 2631 tree / Luogu P1501 [国家集训队]Tree II (LCT,多重标记)
题意 一棵树,有删边加边,有一条链加/乘一个数,有询问一条链的和 分析 LCT,像线段树一样维护两个标记(再加上翻转标记就是三个),维护size,就行了 CODE #include<bits/s ...
- BZOJ 2631 tree | Luogu P1501 [国家集训队]Tree II (LCT 多重标记下放)
链接:https://www.luogu.org/problemnew/show/P1501 题面: 题目描述 一棵n个点的树,每个点的初始权值为1.对于这棵树有q个操作,每个操作为以下四种操作之一: ...
- LUOGU P1501 [国家集训队]Tree II (lct)
传送门 解题思路 \(lct\),比较模板的一道题,路径加和乘的维护标记与线段树\(2\)差不多,然后剩下就没啥了.但调了我将近一下午.. 代码 #include<iostream> #i ...
- P1501 [国家集训队]Tree II(LCT)
P1501 [国家集训队]Tree II 看着维护吧2333333 操作和维护区间加.乘线段树挺像的 进行修改操作时不要忘记吧每个点的点权$v[i]$也处理掉 还有就是$51061^2=2607225 ...
- 洛谷 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(打标记lct)
题目描述 一棵n个点的树,每个点的初始权值为1.对于这棵树有q个操作,每个操作为以下四种操作之一: + u v c:将u到v的路径上的点的权值都加上自然数c: - u1 v1 u2 v2:将树中原有的 ...
- [洛谷P1501] [国家集训队]Tree II(LCT模板)
传送门 这是一道LCT的板子题,说白了就是在LCT上支持线段树2的操作. 所以我只是来存一个板子,并不会讲什么(再说我也不会,只能误人子弟2333). 不过代码里的注释可以参考一下. Code #in ...
随机推荐
- DBA职业发展之路:去“IOE”等挑战之下,DBA将何去何从?
DBA职业发展之路:去“IOE”等挑战之下,DBA将何去何从? 摘要:随着近些年来,开源.自动化.云化的兴起,DBA职业也正悄然发生一些变化.面对一系列新的挑战,DBA将如何进行职业发展?本文将给出一 ...
- ImportBeanDefinitionRegistrar接口实现bean动态注入
借助ImportBeanDefinitionRegistrar接口实现bean的动态注入https://www.jianshu.com/p/2b993ced6a4c ImportBeanDefinit ...
- spark和深度学习集成调研
http://dy.163.com/v2/article/detail/E2TMAOTU0518KCLV.html http://www.elecfans.com/d/676451.html http ...
- 禁用浏览器自动给input填充账号和密码
如果input输入框type为text,设置autoComplete="off" <el-input v-model="ruleForm.loginId" ...
- DataPipeline丨LinkedIn元数据之旅的最新进展—Data Hub
作者:Mars Lan, Seyi Adebajo, Shirshanka Das 译者: DataPiepline yaran 作为全球最大的职场社交平台,LinkedIn的数据团队不断致力于扩展其 ...
- APS系统如何让企业实现“多赢”?看高博通信是怎么做的
高博通信(上海)有限公司凭籍在超精密产业中的技术积累, 强大的资金优势以及与一流大学的联合,使得其正成为国内超精密电子制造行业的领导者. 雄厚的技术实力和专业的团队赢得了波音,空客公司等国际航空器制造 ...
- Java深入学习(2):并发队列
并发队列: 在并发队列中,JDK有两套实现: ConcurrentLinkedQueue:非阻塞式队列 BlockingQueue:阻塞式队列 阻塞式队列非阻塞式队列的区别: 阻塞式队列入列操作的时候 ...
- OCR6:Custom Traineddata
参考:https://groups.google.com/forum/#!msg/tesseract-ocr/MSYezIbckvs/kO1VoNKMDMQJ V4版本代码示例 : import py ...
- Zebra-打印特殊字符
Zebra在打印一些特殊的字符时,会出异常. 在要打印的字符串前加 ^FH 然后将字符换成 ASCii编码或utf-8编码的16进制,在前面加_,如D094写成_DO_94 查看字符的编码 htt ...
- 如何将Android的AOSP仓库放置到自己的gitlab服务器上?
平台 Ubuntu 18.04 GitLab Community Edition 11.11.0 参考 https://source.android.google.cn/ 概述 git ...