bzoj 2631
lct 基础(' ' ) 就当个纪念吧(' ' ) 毕竟写了4h, cut 部分一直naive 总是想找谁是儿子,然后最后发现直接提根就好了啊(' ' )
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std; typedef long long ll;
const ll mod = 51061;
const ll maxn = 100010; ll pl(ll a, ll b) {
ll ret = a + b;
if(ret >= mod) ret %= mod;
return ret;
} ll mul(ll a, ll b) {
return a * b % mod;
} struct node {
ll ans, lm, lp, lr, size, data, p;
node *son[2], *fa;
}e[maxn]; ll ne = 0; void test(node* x) {
if(!x) return;
cout << x-> data <<" "<<x-> size <<" "<< x-> p << endl;
for(ll i = 0; i < 2; ++ i) test(x-> son[i]);
} void update(node* x) {
x-> ans = x-> data, x-> size = 1;
for(ll i = 0; i < 2; ++ i)
if(x-> son[i])
x-> ans = pl(x-> ans, x-> son[i]-> ans), x-> size = pl(x-> son[i]-> size, x-> size);
} void swap(node* &a, node* &b) {
node* mid = a; a = b, b = mid;
} void pushdown(node* x) {
if(!x || (!x-> lp && !x-> lr && x-> lm == 1)) return;
if(x-> lr) {
swap(x-> son[0], x-> son[1]);
for(ll i = 0; i < 2; ++ i) if(x-> son[i]) x-> son[i]-> lr ^= 1;
x-> lr = 0;
}
for(ll i = 0; i < 2; ++ i) {
if(x-> son[i]) {
x-> son[i]-> ans = pl(mul(x-> son[i]-> ans, x-> lm), mul(x-> son[i]-> size, x-> lp));
x-> son[i]-> data = pl(mul(x-> son[i]->data, x-> lm), x-> lp);
x-> son[i]-> lp = pl(mul(x-> son[i]-> lp, x-> lm), x-> lp);
x-> son[i]-> lm = mul(x-> son[i]-> lm, x-> lm);
}
}
x-> lm = 1, x-> lp = 0;
} void rotate(node* x, ll f) {
node* y = x-> fa;
if(y-> fa) {
if(y-> fa-> son[0] == y) y-> fa-> son[0] = x;
else y-> fa-> son[1] = x;
}
x-> fa = y-> fa; x-> size = y-> size; y-> fa = x; x-> p = y-> p;
x-> ans = y-> ans;
y-> son[f] = x-> son[!f];
if(x-> son[!f]) x-> son[!f]-> fa = y;
x-> son[!f] = y;
update(y);
} void splay(node* x, node* f) {
pushdown(x);
while(x-> fa != f) {
if(x-> fa-> fa == f) {
pushdown(x-> fa-> fa), pushdown(x-> fa), pushdown(x);
ll a = x-> fa-> son[0] == x ? 0 : 1;
rotate(x, a);
}
else {
node *y = x-> fa, *z = y-> fa;
pushdown(z), pushdown(y), pushdown(x);
ll a = z-> son[0] == y ? 0 : 1;
ll b = y-> son[0] == x ? 0 : 1;
if(a == b) rotate(y, a), rotate(x, b);
else rotate(x, b), rotate(x, a);
}
}
} void access(ll cur) {
node* x = e + cur; pushdown(x);
node* y; splay(x, NULL);
if(x-> son[1]) x-> son[1]-> p = cur, x-> son[1]-> fa = NULL, x-> son[1] = NULL;
update(x);
ll pp;
while((pp = x-> p)) {
y = e + pp;
splay(y, NULL);
if(y-> son[1]) y-> son[1]-> p = pp, y-> son[1]-> fa = NULL, y-> son[1] = NULL;
y-> son[1] = x;
x-> fa = y;
update(y);
splay(x, NULL);
}
} void reserve(ll x) {
access(x);
//test(x + e); cout << endl;
(e + x)-> lr ^= 1;
} ll n, m; void link(ll a, ll b) {
access(a);
reserve(b);
(e + b)-> p = a;
update(e + a), update(e + b);
} void cut(ll a, ll b) {
reserve(a), access(b);
//test(b + e); cout << endl;
(e + a)-> p = 0, (e + a)-> fa = NULL, (e + b)-> son[0] = NULL;
update(e + a), update(e + b);
} ll ll_get() {
ll x = 0; char c = (char)getchar();
while(!isdigit(c) && c != '-') c = (char)getchar();
bool f = 0; if(c == '-') f = 1, c = (char)getchar();
while(isdigit(c)) {
x = x * 10 + (ll)(c - '0');
c = (char)getchar();
}
if(f) x = -x;
return x;
} struct edge {
ll t; edge* next;
}se[maxn * 2], *head[maxn]; ll oe = 0; void addedge(ll f, ll t) {
se[oe].t = t, se[oe].next = head[f], head[f] = se + oe ++;
} bool vis[maxn]; void build(ll x, ll pre) {
vis[x] = 1;
(e + x)-> p = pre; (e + x)-> data = (e + x)-> ans = 1; (e + x)-> size = 1; (e + x)-> lm = 1;
for(edge* p = head[x]; p; p = p-> next) {
if(!vis[p-> t]) build(p-> t, x);
}
} void read() {
n = ll_get(), m = ll_get();
for(ll i = 1; i < n; ++ i) {
ll f = ll_get(), t = ll_get();
addedge(f, t), addedge(t, f);
}
memset(vis, 0, sizeof(vis));
build(1, 0);
} void sov() {
char s[10];
// for(int i = 1; i <= n; ++ i) test(e + i), cout << endl;
//cout << "****\n";
while(m --) {
scanf("%s", s + 1);
if(s[1] == '+') {
ll a = ll_get(), b = ll_get(), c = ll_get();
reserve(a); access(b);
node* x = (e + b);
x-> lp = pl(c, x-> lp), x-> ans = pl(x-> ans, mul(c, x-> size)), x-> data = pl(x-> data, c);
}
if(s[1] == '*') {
ll a = ll_get(), b = ll_get(), c = ll_get();
reserve(a);
access(b);
node* x = (e + b);
x-> lm = mul(x-> lm, c), x-> ans = mul(x-> ans, c), x-> data = mul(x-> data, c), x-> lp = mul(x-> lp, c);
}
if(s[1] == '-') {
ll a, b, c, d; a = ll_get(), b = ll_get(), c = ll_get(), d = ll_get();
cut(a, b);
//for(int i = 1; i <= n; ++ i) cout << (e + i)-> p << endl<< endl;
//for(int i = 1; i <= n; ++ i) test(e + i), cout << endl;
//cout << "****\n";
link(c, d); //for(int i = 1; i <= n; ++ i) test(e + i), cout << endl; }
if(s[1] == '/') {
ll a, b; a = ll_get(), b = ll_get();
reserve(a), access(b);
printf("%lld\n", (e + b)-> ans);
}
//for(int i = 1; i <= n; ++ i) test(e + i), cout << endl;
//cout << "****\n";
//cout << (e + 1)-> son[0] << endl;
}
} int main() {
//freopen("test.in", "r", stdin);
//freopen("test.out", "w", stdout);
read();
sov();
}
bzoj 2631的更多相关文章
- [BZOJ - 2631] tree 【LCT】
题目链接:BZOJ - 2631 题目分析 LCT,像线段树区间乘,区间加那样打标记. 这道题我调了一下午. 提交之后TLE了,我一直以为是写错了导致了死循环. 于是一直在排查错误.直到.. 直到我看 ...
- bzoj 2631: tree 动态树+常数优化
2631: tree Time Limit: 30 Sec Memory Limit: 128 MBSubmit: 1716 Solved: 576[Submit][Status] Descrip ...
- BZOJ 2631: tree( LCT )
LCT...略麻烦... -------------------------------------------------------------------------------- #inclu ...
- BZOJ 2631: tree [LCT splay区间]
2631: tree Time Limit: 30 Sec Memory Limit: 128 MBSubmit: 3854 Solved: 1292[Submit][Status][Discus ...
- BZOJ 2631 tree(动态树)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=2631 [题目大意] 要求支持链修改,链查询,边断开,连接操作 [题解] 链修改分乘和加 ...
- BZOJ 2631 [国家集训队]Tree II (LCT)
题目大意:给你一棵树,让你维护一个数据结构,支持 边的断,连 树链上所有点点权加上某个值 树链上所有点点权乘上某个值 求树链所有点点权和 (辣鸡bzoj又是土豪题,洛谷P1501传送门) LCT裸题, ...
- BZOJ 2631 Tree ——Link-Cut Tree
[题目分析] 又一道LCT的题目,LCT只能维护链上的信息. [代码] #include <cstdio> #include <cstring> #include <cs ...
- [BZOJ 2631]tree
裸LCT..QAQ写了三遍没写对 真是老了..QAQ 主要错的地方是 init: size[i] = sum[i] = val[i] = mul[i] = 1; pushdown: 注意判断左右儿子是 ...
- bzoj 2631: tree link-cut-tree
题目: Description 一棵n个点的树,每个点的初始权值为1.对于这棵树有q个操作,每个操作为以下四种操作之一: + u v c:将u到v的路径上的点的权值都加上自然数c: - u1 v1 u ...
- 洛谷 1501 [国家集训队]Tree II BZOJ 2631 Tree
[题解] 维护乘法标记和加法标记的LCT #include<cstdio> #include<algorithm> #define Mod (51061) #define N ...
随机推荐
- Solr安装步骤 + dataimport导入数据配置
一:安装solr服务器 1.导入安装包到 /usr/local/solr/singleSolr目录下 解压tomat命令:tar -zxvf apache-tomcat-7.0.47.tar.gz 解 ...
- Java中的多表&事务&DCL&一个多表操作例子
准备sql: 创建部门表 CREATE TABLE dept( id INT PRIMARY KEY AUTO_INCREMENT, NAME VARCHAR(20) ); INSERT INTO d ...
- docker 运行springboot jar包
1.将jar包移至自定义的/usr/jar目录下; 2.在/usr/jar目录下创建Dockerfile文件 文件如下: #FROM命令定义构建镜像的基础镜像,该条必须是dockerfile的首个命令 ...
- Vue 之指令篇
文件指令 <body> <div id="app"> <!-- 1) 插值表达式 --> <p>{ ...
- 【Flutter学习】事件处理与通知之通知(Notification)
一,概述 Notification是Flutter中一个重要的机制,在Widget树中,每一个节点都可以分发通知,通知会沿着当前节点(context)向上传递,所有父节点都可以通过Notificati ...
- PHP使用引用变量foreach时,切记其他循环不要使用同一个名字的变量
foreach ($log['data'] as $k => &$value) { if ($value['token'] != 0) { $value['change_num'] = ...
- Windows下 wamp下Apache配置虚拟域名
安装好wamp后 找到 找到 Include conf/extra/httpd-vhosts.conf 去掉前面的# 并保存 修改 DocumentRoot 和 ServerName ...
- Webx.0-Web3.0:Web3.0
ylbtech-Webx.0-Web3.0:Web3.0 Web3.0只是由业内人员制造出来的概念词语,最常见的解释是,网站内的信息可以直接和其他网站相关信息进行交互,能通过第三方信息平台同时对多家网 ...
- (转)SQL Server 数据类型映射
SQL Server 数据类型映射 SQL Server 和 .NET Framework 基于不同的类型系统. 例如,.NET Framework Decimal 结构的最大小数位数为 28,而 S ...
- Java数据类型简介
Java数据类型 以下两行Java代码定义了两个整数:num1和num2: num1和num2是两个int类型的变量. int关键字指定它的后面是变量的名称,并表示数据类型整数,例如:10,15,70 ...