BZOJ2631——tree
1、题目大意:bzoj1798的lct版本
2、分析:这个把线段树改成splay就好
#include <stack>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
using namespace std;
#define LL long long
namespace LinkCutTree{
struct Node{
Node *ch[2], *fa;
LL sum, num;
LL size;
bool rev;
LL mul, plu;
inline int which();
inline void reverse(){
if(this) rev ^= 1;
}
inline void pd();
inline void maintain(){
sum = (num + ch[0] -> sum + ch[1] -> sum) % 51061;
size = (1 + ch[0] -> size + ch[1] -> size) % 51061;
}
Node();
} *null = new Node, tree[100010], *pos[100010];
Node::Node(){
num = sum = 1;
rev = false;
ch[0] = ch[1] = fa = null;
mul = 1;
plu = 0;
size = 1;
}
inline void Node::pd(){
if(rev){
swap(ch[0], ch[1]);
ch[0] -> reverse();
ch[1] -> reverse();
rev = false;
}
if(ch[0] != null){
ch[0] -> mul *= mul;
ch[0] -> plu *= mul;
ch[0] -> plu += plu;
ch[0] -> num *= mul;
ch[0] -> num += plu;
ch[0] -> sum *= mul;
ch[0] -> sum += plu * ch[0] -> size;
ch[0] -> mul %= 51061;
ch[0] -> plu %= 51061;
ch[0] -> num %= 51061;
ch[0] -> sum %= 51061;
}
if(ch[1] != null){
ch[1] -> mul *= mul;
ch[1] -> plu *= mul;
ch[1] -> plu += plu;
ch[1] -> num *= mul;
ch[1] -> num += plu;
ch[1] -> sum *= mul;
ch[1] -> sum += plu * ch[1] -> size;
ch[1] -> mul %= 51061;
ch[1] -> plu %= 51061;
ch[1] -> num %= 51061;
ch[1] -> sum %= 51061;
}
mul = 1;
plu = 0;
}
inline int Node::which(){
if(fa == null || (this != fa -> ch[0] && this != fa -> ch[1])) return -1;
return this == fa -> ch[1];
}
inline void rotate(Node *o){
Node *p = o -> fa;
int l = o -> which(), r = l ^ 1;
o -> fa = p -> fa;
if(p -> which() != -1) p -> fa -> ch[p -> which()] = o;
p -> ch[l] = o -> ch[r];
if(o -> ch[r]) o -> ch[r] -> fa = p;
o -> ch[r] = p; p -> fa = o;
o -> ch[r] -> maintain();
o -> maintain();
}
inline void splay(Node *o){
static stack<Node*> st;
if(!o) return;
Node *p = o;
while(1){
st.push(p);
if(p -> which() == -1) break;
p = p -> fa;
}
while(!st.empty()){
st.top() -> pd(); st.pop();
}
while(o -> which() != -1){
p = o -> fa;
if(p -> which() != -1){
if(p -> which() ^ o -> which()) rotate(o);
else rotate(p);
}
rotate(o);
}
}
inline void Access(Node *o){
Node *y = null;
while(o != null){
splay(o);
o -> ch[1] = y;
o -> maintain();
y = o; o = o -> fa;
}
}
inline void MovetoRoot(Node *o){
Access(o);
splay(o);
o -> reverse();
}
inline Node* FindRoot(Node *o){
Access(o);
splay(o);
while(o -> ch[0] != null) o = o -> ch[0];
return o;
}
inline void Link(Node *x, Node *y){
MovetoRoot(x);
x -> fa = y;
}
inline void Cut(Node *x, Node *y){
MovetoRoot(x);
Access(y);
splay(y);
y -> ch[0] = x -> fa = null;
y -> maintain();
}
}
int main(){
using namespace LinkCutTree;
null -> mul = 1;
null -> size = 0;
null -> plu = 0;
null -> sum = 0;
null -> num = 0;
null -> ch[0] = null -> ch[1] = null -> fa = NULL;
int n, q;
scanf("%d%d", &n, &q);
for(int i = 1; i <= n; i ++) pos[i] = &tree[i];
for(int i = 1; i < n; i ++){
int u, v;
scanf("%d%d", &u, &v);
Link(pos[u], pos[v]);
}
char op[5];
int x1, y1, x2, y2, c;
while(q --){
scanf("%s", op);
if(op[0] == '+'){
scanf("%d%d%d", &x1, &y1, &c);
MovetoRoot(pos[x1]);
Access(pos[y1]);
splay(pos[y1]);
pos[y1] -> num += c;
pos[y1] -> num %= 51061;
pos[y1] -> sum += pos[y1] -> size * c;
pos[y1] -> sum %= 51061;
pos[y1] -> plu += c;
pos[y1] -> plu %= 51061;
}
else if(op[0] == '-'){
scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
Cut(pos[x1], pos[y1]);
Link(pos[x2], pos[y2]);
}
else if(op[0] == '*'){
scanf("%d%d%d", &x1, &y1, &c);
MovetoRoot(pos[x1]);
Access(pos[y1]);
splay(pos[y1]);
pos[y1] -> num *= c;
pos[y1] -> num %= 51061;
pos[y1] -> sum *= c;
pos[y1] -> sum %= 51061;
pos[y1] -> mul *= c;
pos[y1] -> mul %= 51061;
pos[y1] -> plu *= c;
pos[y1] -> plu %= 51061;
}
else{
scanf("%d%d", &x1, &y1);
MovetoRoot(pos[x1]);
Access(pos[y1]);
splay(pos[y1]);
pos[y1] -> sum %= 51061;
printf("%lld\n", pos[y1] -> sum);
}
}
return 0;
}
BZOJ2631——tree的更多相关文章
- bzoj2631: tree
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #i ...
- [BZOJ2631]tree 动态树lct
2631: tree Time Limit: 30 Sec Memory Limit: 128 MBSubmit: 5171 Solved: 1754[Submit][Status][Discus ...
- [Link-Cut-Tree][BZOJ2631]Tree
题面 Description: 一棵\(n\)个点的树,每个点的初始权值为\(1\).对于这棵树有\(q\)个操作,每个操作为以下四种操作之一: + u v c:将\(u\)到\(v\)的路径上的点的 ...
- bzoj2631 tree LCT 区间修改,求和
tree Time Limit: 30 Sec Memory Limit: 128 MBSubmit: 4962 Solved: 1697[Submit][Status][Discuss] Des ...
- bzoj2631: tree lct
要打mul和add的lct 50000+的mod用unsigned int好了TAT (坑爹没打pc('\n');(静态)调了好久,样例竟然只输出一个,orz,也不提示PE T_T) #include ...
- BZOJ2631 tree(伍一鸣) LCT 秘制标记
这个题一看就是裸地LCT嘛,但是我wa了好几遍,这秘制标记...... 注意事项:I.*对+有贡献 II.先下传*再下传+(因为我们已经维护了+,不能再让*对+产生贡献)III.维护+用到size # ...
- [bzoj2631]tree——lct
Brief Description 一棵n个点的树,每个点的初始权值为1.对于这棵树有q个操作,每个操作为以下四种操作之一: u v c:将u到v的路径上的点的权值都加上自然数c: u1 v1 u2 ...
- BZOJ2631 tree 【LCT】
题目 一棵n个点的树,每个点的初始权值为1.对于这棵树有q个操作,每个操作为以下四种操作之一: + u v c:将u到v的路径上的点的权值都加上自然数c: - u1 v1 u2 v2:将树中原有的边( ...
- BZOJ2631: tree(LCT)
Description 一棵n个点的树,每个点的初始权值为1.对于这棵树有q个操作,每个操作为以下四种操作之一: + u v c:将u到v的路径上的点的权值都加上自然数c: - u1 v1 u2 v2 ...
随机推荐
- Objective-C学习笔记之for( int )机制
NSArray *myArray = [NSArray arrayWithObjects:@"1",@"2",@"3",@"4&q ...
- kailli添加桌面快捷方式
kailli添加桌面快捷方式 /usr/share/applications/xxx.desktop 注意大小写要与Name对应 [Desktop Entry] Version=1.0 Name=Tu ...
- JS左右栏目添加器
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Android学习笔记——Button
该工程的功能是实现在activity中显示一个TextView和一个Button 以下代码是MainActivity中的代码 package com.example.button; import an ...
- SVN中Branch和Merge实践
参考资料:http://blog.csdn.net/eggcalm/article/details/6606520 branch主要用于新功能的开发,开发过程中不断从trunk merge revis ...
- Red Black Tree in C
http://web.mit.edu/~emin/www.old/source_code/red_black_tree/index.html
- CodeLite的姿势
在Mac上安装cscope 1.下载cscope的Zip压缩包 2.解压 3.打开终端,进入解压目录,运行 ./configure make make install 4.在CodeLite中,在Pl ...
- yourphp添加KindEditor编辑器
<tr> <td align="right">故障描述</td> <script charset="utf-8" sr ...
- PetaPoco 使用总结(二)
接着上一篇,上一篇主要介绍了PetaPoco 基本情况,优缺点和基本的查询功能,所以这篇主要介绍的是PetaPoco 的增,删,改等功能.PetaPoco提供了完整的增,删,改,查功能.是代替SqlH ...
- SSL/TLS加密传输与数字证书解读
什么是ssl? secure socket layer(ssl)协议最初由netscape企业发展,现已成为网络用来鉴别网站和网页浏览者身份,以及在浏览器使用者及网页服务器之间进行加密通讯的全球化标准 ...