链接

luogu

思路

简单题

代码

#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的更多相关文章

  1. 洛谷P1501 [国家集训队]Tree II(LCT)

    题目描述 一棵n个点的树,每个点的初始权值为1.对于这棵树有q个操作,每个操作为以下四种操作之一: + u v c:将u到v的路径上的点的权值都加上自然数c: - u1 v1 u2 v2:将树中原有的 ...

  2. BZOJ 2631 tree / Luogu P1501 [国家集训队]Tree II (LCT,多重标记)

    题意 一棵树,有删边加边,有一条链加/乘一个数,有询问一条链的和 分析 LCT,像线段树一样维护两个标记(再加上翻转标记就是三个),维护size,就行了 CODE #include<bits/s ...

  3. BZOJ 2631 tree | Luogu P1501 [国家集训队]Tree II (LCT 多重标记下放)

    链接:https://www.luogu.org/problemnew/show/P1501 题面: 题目描述 一棵n个点的树,每个点的初始权值为1.对于这棵树有q个操作,每个操作为以下四种操作之一: ...

  4. LUOGU P1501 [国家集训队]Tree II (lct)

    传送门 解题思路 \(lct\),比较模板的一道题,路径加和乘的维护标记与线段树\(2\)差不多,然后剩下就没啥了.但调了我将近一下午.. 代码 #include<iostream> #i ...

  5. P1501 [国家集训队]Tree II(LCT)

    P1501 [国家集训队]Tree II 看着维护吧2333333 操作和维护区间加.乘线段树挺像的 进行修改操作时不要忘记吧每个点的点权$v[i]$也处理掉 还有就是$51061^2=2607225 ...

  6. 洛谷 P1501 [国家集训队]Tree II 解题报告

    P1501 [国家集训队]Tree II 题目描述 一棵\(n\)个点的树,每个点的初始权值为\(1\).对于这棵树有\(q\)个操作,每个操作为以下四种操作之一: + u v c:将\(u\)到\( ...

  7. 洛谷P1501 [国家集训队]Tree II(LCT,Splay)

    洛谷题目传送门 关于LCT的其它问题可以参考一下我的LCT总结 一道LCT很好的练习放懒标记技巧的题目. 一开始看到又做加法又做乘法的时候我是有点mengbi的. 然后我想起了模板线段树2...... ...

  8. 洛谷P1501 [国家集训队]Tree II(打标记lct)

    题目描述 一棵n个点的树,每个点的初始权值为1.对于这棵树有q个操作,每个操作为以下四种操作之一: + u v c:将u到v的路径上的点的权值都加上自然数c: - u1 v1 u2 v2:将树中原有的 ...

  9. [洛谷P1501] [国家集训队]Tree II(LCT模板)

    传送门 这是一道LCT的板子题,说白了就是在LCT上支持线段树2的操作. 所以我只是来存一个板子,并不会讲什么(再说我也不会,只能误人子弟2333). 不过代码里的注释可以参考一下. Code #in ...

随机推荐

  1. 转Tasklist(windows)

    Windows 进程 Tasklist查看 与 Taskkill结束   转自https://blog.csdn.net/wangmx1993328/article/details/80923829 ...

  2. k8s-架构中各个组件介绍

    参考链接:https://github.com/opsnull/follow-me-install-kubernetes-cluster kubernetes 概述 1.kubernetes 是什么 ...

  3. NET Core 导入web.config配置文件

    1.首先需要在NET Core项目中引入System.Configuration.ConfigurationManager,它可以读取web.config中的相关配置信息. 2. 将web.confi ...

  4. C#汉字转为Unicode编码

    主要用于生成json格式时,将汉字转成Unicoude编码,防止页面乱码. protected string GetUnicode(string text) { string result = &qu ...

  5. JDK8 stream用法

    forEach举例 public static void main(String[] args) { // TODO Auto-generated method stub List<Person ...

  6. 分享一篇最近新写的jquery注册页面表单校验的程序,仅供参考

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

  7. 关于IE环境下按回车键会自动触发button 按钮的点击事件的解决方案

    今天项目中遇到IE不兼容的问题,于是就根据问题进行修改,修改过程中发现,在输入框内用扫码枪扫描东西后会自动执行页面下面的button按钮,但是其它浏览不会出现这样的问题. 解决方案: 1.用a标签 2 ...

  8. Python卸载不干净?苹果电脑卸载python教程

    如今,Pyhon越来越火,屡次超越Java.C++成为编程语言排行榜第一的语言,国内的公司和程序员们也越来越喜欢使用Python.但是Python安装之后,散落在电脑各处,删除起来比较麻烦,很多小伙伴 ...

  9. Ubuntu 14.04更新为国内阿里源解决apt-get install无法执行的问题

    step01: sudo gedit /etc/apt/sources.list #将下面全部粘贴到sources.list中deb http://mirrors.aliyun.com/ubuntu/ ...

  10. PAT 乙级 1009.说反话 C++/Java

    1009 说反话 (20 分) 题目来源 给定一句英语,要求你编写程序,将句中所有单词的顺序颠倒输出. 输入格式: 测试输入包含一个测试用例,在一行内给出总长度不超过 80 的字符串.字符串由若干单词 ...