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 ...
随机推荐
- 适可而止:YAGNI原则
适可而止:You Ain't Gonna Need It YAGNI原则指的是只需要将应用程序必需的功能包含进来,而不要试图添加任何其他你认为可能需要的功能. 在一个软件项目中,往往80%的时间花费在 ...
- C#------DateTime自定义格式
var text = Convert.ToString(DateTime.Now.ToString("yyyy/MM/dd"));
- JS左右栏目添加器
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 9月8日HTML表单元素(form、文本、按钮、选择)
表单元素 一.form form代表表单,功能:用于申明表单,定义采集数据的范围,也就是<form>和</form>里面包含的数据将被提交到服务器或者电子邮件里.<for ...
- Java数据库——ResultSet接口
使用SQL中的SELECT语句可以查询出数据库的全部结果,在JDBC的操作中数据库的所有查询记录将使用ResultSet进行接收,并使用ResultSet显示内容. 从user表中查询数据 //=== ...
- 菜单each+hover
<script type="text/javascript"> $(document).ready(function(){ var src1= new Array('i ...
- C# 实现 Snowflake算法 ID生成
http://blog.csdn.net/w200221626/article/details/52064976 C# 实现 Snowflake算法 /// <summary> /// 动 ...
- MANIFEST.MF的用途(转载)
MANIFEST.MF的用途(转载) 可以用的上的有: 1. Main-Class 指定程序的入口,这样可以直接用java -jar xxx.jar来运行程序. 2. Class-Path 指定jar ...
- Linq_Lambda GroupBy使用笔记
今天看MVC遇到了GroupBY的Lambda表达式..有兴趣详细的看下去..得此笔记..记录之... 不罗嗦..上代码... //得到List<GroupEmail>对象 数据源 var ...
- 二叉排序树(Binary Sort Tree)
参考文章:http://blog.csdn.net/ns_code/article/details/19823463 不过博主的使用第一种方法操作后的树已经不是二叉排序树了,值得深思!! #inclu ...