【bzoj2631】tree LCT
题目描述
一棵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的余数。
输入
接下来n-1行每行两个正整数u,v,描述这棵树
接下来q行,每行描述一个操作
输出
样例输入
3 2
1 2
2 3
* 1 3 4
/ 1 1
样例输出
4
题解
带点权的LCT
需要注意的是3标记的处理:先乘后加,同时乘标记,与翻转互不影响。
这题的坑点在于int会WA,long long会TLE,必须用unsigned int。
#include <cstdio>
#include <algorithm>
#define N 100010
#define MOD 51061
#define lson c[0][x]
#define rson c[1][x]
using namespace std;
int fa[N] , c[2][N] , si[N] , rev[N];
unsigned w[N] , sum[N] , add[N] , mul[N];
char str[5];
inline int read()
{
int ret = 0; char ch = getchar();
while(ch < '0' || ch > '9') ch = getchar();
while(ch >= '0' && ch <= '9') ret = (ret << 3) + (ret << 1) + ch - '0' , ch = getchar();
return ret;
}
void pushup(int x)
{
si[x] = si[lson] + si[rson] + 1;
sum[x] = (sum[lson] + sum[rson] + w[x]) % MOD;
}
void cal(int x , unsigned a , unsigned m , int r)
{
sum[x] = (sum[x] * m + si[x] * a) % MOD;
w[x] = (w[x] * m + a) % MOD;
mul[x] = (mul[x] * m) % MOD;
add[x] = (add[x] * m + a) % MOD;
if(r) swap(lson , rson) , rev[x] ^= 1;
}
void pushdown(int x)
{
cal(lson , add[x] , mul[x] , rev[x]);
cal(rson , add[x] , mul[x] , rev[x]);
add[x] = rev[x] = 0 , mul[x] = 1;
}
bool isroot(int x)
{
return c[0][fa[x]] != x && c[1][fa[x]] != x;
}
void update(int x)
{
if(!isroot(x)) update(fa[x]);
pushdown(x);
}
void rotate(int x)
{
int y = fa[x] , z = fa[y] , l = (c[1][y] == x) , r = l ^ 1;
if(!isroot(y)) c[c[1][z] == y][z] = x;
fa[x] = z , fa[y] = x , fa[c[r][x]] = y , c[l][y] = c[r][x] , c[r][x] = y;
pushup(y) , pushup(x);
}
void splay(int x)
{
update(x);
while(!isroot(x))
{
int y = fa[x] , z = fa[y];
if(!isroot(y))
{
if((c[0][y] == x) ^ (c[0][z] == y)) rotate(x);
else rotate(y);
}
rotate(x);
}
}
void access(int x)
{
int t = 0;
while(x) splay(x) , rson = t , pushup(x) , t = x , x = fa[x];
}
void makeroot(int x)
{
access(x) , splay(x);
swap(lson , rson) , rev[x] ^= 1;
}
void link(int x , int y)
{
makeroot(x) , fa[x] = y;
}
void cut(int x , int y)
{
makeroot(x) , access(y) , splay(y) , c[0][y] = fa[x] = 0 , pushup(y);
}
void split(int x , int y)
{
makeroot(y) , access(x) , splay(x);
}
int main()
{
int n , m , i , x , y;
unsigned z;
n = read() , m = read();
for(i = 1 ; i <= n ; i ++ ) si[i] = w[i] = sum[i] = mul[i] = 1;
for(i = 1 ; i < n ; i ++ ) x = read() , y = read() , link(x , y);
while(m -- )
{
scanf("%s" , str) , x = read() , y = read();
switch(str[0])
{
case '+': z = (unsigned)read() , split(x , y) , cal(x , z , 1 , 0); break;
case '-': cut(x , y) , x = read() , y = read() , link(x , y); break;
case '*': z = (unsigned)read() , split(x , y) , cal(x , 0 , z , 0); break;
default: split(x , y) , printf("%u\n" , sum[x]);
}
}
return 0;
}
【bzoj2631】tree LCT的更多相关文章
- 【bzoj2631】tree link-cut-tree
2016-06-01 08:50:36 题目:http://www.lydsy.com/JudgeOnline/problem.php?id=2631 注意加和乘的标记下传问题. 还有就是split后 ...
- 【BZOJ2631】tree
Description 一棵n个点的树.每一个点的初始权值为1. 对于这棵树有q个操作,每一个操作为下面四种操作之中的一个: + u v c:将u到v的路径上的点的权值都加上自然数c: - u1 v1 ...
- 【BZOJ3282】Tree LCT
1A爽,感觉又对指针重怀信心了呢= =,模板题,注意单点修改时splay就好,其实按吾本意是没写的也A了,不过应该加上能更好维护平衡性. ..还是得加上好= = #include <iostre ...
- 【BZOJ】【2631】Tree
LCT 又一道名字叫做Tree的题目…… 看到删边加边什么的……又是动态树问题……果断再次搬出LCT. 这题比起上道[3282]tree的难点在于需要像线段树维护区间那样,进行树上路径的权值修改&am ...
- 【Luogu1501】Tree(Link-Cut Tree)
[Luogu1501]Tree(Link-Cut Tree) 题面 洛谷 题解 \(LCT\)版子题 看到了顺手敲一下而已 注意一下,别乘爆了 #include<iostream> #in ...
- 【BZOJ3282】Tree (Link-Cut Tree)
[BZOJ3282]Tree (Link-Cut Tree) 题面 BZOJ权限题呀,良心luogu上有 题解 Link-Cut Tree班子提 最近因为NOIP考炸了 学科也炸了 时间显然没有 以后 ...
- 【POJ3237】Tree 树链剖分+线段树
[POJ3237]Tree Description You are given a tree with N nodes. The tree's nodes are numbered 1 through ...
- 【AtCoder3611】Tree MST(点分治,最小生成树)
[AtCoder3611]Tree MST(点分治,最小生成树) 题面 AtCoder 洛谷 给定一棵\(n\)个节点的树,现有有一张完全图,两点\(x,y\)之间的边长为\(w[x]+w[y]+di ...
- 【HDU5909】Tree Cutting(FWT)
[HDU5909]Tree Cutting(FWT) 题面 vjudge 题目大意: 给你一棵\(n\)个节点的树,每个节点都有一个小于\(m\)的权值 定义一棵子树的权值为所有节点的异或和,问权值为 ...
随机推荐
- jsp+spring+jquery+ajax的简单例子
初学b/s编程,花费了许多时间,进度颇慢! 不过终于完成了一个简单的例子: jsp代码 <%@ page language="java" contentType=" ...
- NFS网络文件系统详解
第1章 NFS基本概述 1.1 什么是nfs NFS是Network File System的缩写及网络文件系统. 主要功能是通过局域网络让不同的主机系统之间可以共享文件或目录. NFS系统和Wind ...
- Node.js(二)----安装Cnpm
---恢复内容开始--- 1.安装CNPM 因为天草的 Great Wall 导致下载速度龟速....所以安装Cnpm淘宝镜像 2.命令 2.1 如果版本合适 设置镜像地址 npm config se ...
- Python学习手册之Python介绍、基本语法(二)
在上一篇文章中,我们介绍了Python的一些基本语法,现在我们继续介绍剩下的Python基本语法.查看上一篇文章请点击:https://www.cnblogs.com/dustman/p/987193 ...
- python 装饰器 生成及原里
# 装饰器形成的过程 : 最简单的装饰器 有返回值的 有一个参数 万能参数 # 装饰器的作用 # 原则 :开放封闭原则 # 语法糖 :@ # 装饰器的固定模式 #不懂技术 import time # ...
- Qt——绘图
1.涉及类 QPainter QPaintEngine QPaintDevice 作为绘图的使用者,只需要关注 QPainter 和 QPaintDevice 2.QPainter 使用 QPain ...
- BZOJ:2038: [2009国家集训队]小Z的袜子(hose)(莫队算法模板)
题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=2038 解题心得: 第一次接触莫队算法,很神奇,很巧妙.莫队算法主要就是用来解决多次询问时 ...
- MOVE-PERCENTAGE(文字列の部分の代入)
以下の MOVE 命令のバリアントは.c 型項目についてのみ機能します. MOVE c1 TO c2 PERCENTAGE p [RIGHT]. 左寄せした (RIGHT オプションを指定した場合は右 ...
- 20145202 《网络对抗技术》 PC平台逆向破解
20145202 <网络对抗技术> PC平台逆向破解 准备工作 先将环境设置为:堆栈可执行.地址随机化关闭 参考http://git.oschina.net/wildlinux/NetSe ...
- [bzoj5158][Tjoi2014]Alice and Bob
好羞愧啊最近一直在刷水... 题意:给定序列$c$的$a_i$,构造出一个序列$c$使得$\sum b_i$最大. 其中$a_i$表示以$c_i$结尾的最长上升子序列长度,$b_i$表示以$c_i$为 ...