_bzoj3223 Tyvj 1729 文艺平衡树【Splay】
传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=3223
裸的,打个标记。
#include <cstdio>
#include <algorithm> const int maxn = 100005; int n, m, stk[maxn], top, t1, t2;
int fa[maxn], ch[maxn][2], root, siz[maxn];
char rev[maxn]; inline void pushdown(int x) {
if (rev[x]) {
rev[x] = 0;
rev[ch[x][0]] ^= 1;
rev[ch[x][1]] ^= 1;
std::swap(ch[x][0], ch[x][1]);
}
}
inline void pushup(int x) {
siz[x] = siz[ch[x][0]] + siz[ch[x][1]] + 1;
}
inline void rotate(int x) {
int y = fa[x];
if (y == ch[fa[y]][0]) {
ch[fa[y]][0] = x;
}
else {
ch[fa[y]][1] = x;
}
fa[x] = fa[y];
int dir = x == ch[y][1];
ch[y][dir] = ch[x][dir ^ 1];
fa[ch[x][dir ^ 1]] = y;
ch[x][dir ^ 1] = y;
fa[y] = x;
pushup(y);
pushup(x);
}
inline void splay(int x, int rt) {
int p;
top = 0;
for (int i = x; i != rt; i = fa[i]) {
stk[top++] = i;
}
for (int i = top - 1; ~i; --i) {
pushdown(stk[i]);
}
while (fa[x] != rt) {
p = fa[x];
if (fa[p] == rt) {
rotate(x);
}
else {
if ((p == ch[fa[p]][1]) ^ (x == ch[p][1])) {
rotate(x);
}
else {
rotate(p);
}
rotate(x);
}
}
if (!rt) {
root = x;
}
}
inline int kth(int k) {
int x = root;
pushdown(x);
while (k != siz[ch[x][0]] + 1) {
if (k <= siz[ch[x][0]]) {
x = ch[x][0];
}
else {
k -= siz[ch[x][0]] + 1;
x = ch[x][1];
}
pushdown(x);
}
return x;
}
int make_tree(int left, int right) {
if (left > right) {
return 0;
}
int rt = (left + right) >> 1;
ch[rt][0] = make_tree(left, rt - 1);
fa[ch[rt][0]] = rt;
ch[rt][1] = make_tree(rt + 1, right);
fa[ch[rt][1]] = rt;
pushup(rt);
return rt;
}
void print(int r) {
if (!r) {
return;
}
pushdown(r);
print(ch[r][0]);
if (r != 1 && r != n + 2) {
printf("%d ", r - 1);
}
print(ch[r][1]);
} int main(void) {
//freopen("in.txt", "r", stdin);
scanf("%d%d", &n, &m);
root = make_tree(1, n + 2);
while (m--) {
scanf("%d%d", &t1, &t2);
++t1;
++t2;
t1 = kth(t1 - 1);
t2 = kth(t2 + 1);
splay(t1, 0);
splay(t2, root);
rev[ch[ch[root][1]][0]] ^= 1;
}
print(root);
return 0;
}
_bzoj3223 Tyvj 1729 文艺平衡树【Splay】的更多相关文章
- BZOJ3223: Tyvj 1729 文艺平衡树 [splay]
3223: Tyvj 1729 文艺平衡树 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 3595 Solved: 2029[Submit][Sta ...
- BZOJ 3223: Tyvj 1729 文艺平衡树(splay)
速度居然进前十了...第八... splay, 区间翻转,用一个类似线段树的lazy标记表示是否翻转 ------------------------------------------------- ...
- bzoj 3223: Tyvj 1729 文艺平衡树 (splay)
链接: https://www.lydsy.com/JudgeOnline/problem.php?id=3223 题面: 3223: Tyvj 1729 文艺平衡树 Time Limit: 10 S ...
- BZOJ 3223: Tyvj 1729 文艺平衡树-Splay树(区间翻转)模板题
3223: Tyvj 1729 文艺平衡树 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 6881 Solved: 4213[Submit][Sta ...
- bzoj 3223/tyvj 1729 文艺平衡树 splay tree
原题链接:http://www.tyvj.cn/p/1729 这道题以前用c语言写的splay tree水过了.. 现在接触了c++重写一遍... 只涉及区间翻转,由于没有删除操作故不带垃圾回收,具体 ...
- 【BZOJ3223】 Tyvj 1729 文艺平衡树 Splay
Description 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:翻转一个区间,例如原有序序列是5 4 3 2 1,翻转区间是[2,4]的话,结果是5 2 ...
- bzoj3223: Tyvj 1729 文艺平衡树 splay裸题
splay区间翻转即可 /************************************************************** Problem: 3223 User: walf ...
- BZOJ - 3223 Tyvj 1729 文艺平衡树 (splay/无旋treap)
题目链接 splay: #include<bits/stdc++.h> using namespace std; typedef long long ll; ,inf=0x3f3f3f3f ...
- BZOJ 3223 Tyvj 1729 文艺平衡树 | Splay 维护序列关系
题解: 每次reverse(l,r) 把l-1转到根,r+1变成他的右儿子,给r+1的左儿子打个标记就是一次反转操作了 每次find和dfs输出的时候下放标记,把左儿子和右儿子换一下 记得建树的时候建 ...
随机推荐
- ArcGIS engine中Display类库——Display
转自原文 ArcGIS engine中Display类库——Display Display类库包括了用于显示GIS数据的对象.除了负责实际输出图像的主要显示对象(display object)外,这 ...
- [转] Oracle数据库维护常用SQL语句集合
原文地址 进程相关: 1. 求当前会话的SID,SERIAL# SELECT Sid, Serial# FROM V$session WHERE Audsid = Sys_Context ...
- ArcEngine读取ShapeFile时,出现乱码的解决方案
ArcEngine读取ShapeFile时,如果用LicenseControl的话,字段中含有汉字时可以正常使用,当使用LicenseInitializer进行初始化时,读取含有汉字的字段时,就会出现 ...
- Activiti-5.3工作流引擎-源码解析(流程文档解析)
前面我们通过BPMN20.xsd和Activiti自定义的XML Schema文件初步了解了业务流程模型的定义,那么现在我们来了解一下流程文档的解析过程,这个过程主要是通过代码解析来完成. 代码解析过 ...
- M公司入职记
非常遗憾,我又跳槽了,到传说中的M公司,第一天就体会到了,神马叫差距. 要求9点30到,提前10分钟到了前台.前台MM懵懂的跟我说入职找人事,好吧. 电话联系相关人等,等到10点左右,被引导到一位不知 ...
- wpf 导出Excel Wpf Button 样式 wpf简单进度条 List泛型集合对象排序 C#集合
wpf 导出Excel 1 private void Button_Click_1(object sender, RoutedEventArgs e) 2 { 3 4 ExportDataGrid ...
- 基于docker容器搭建fastdfs分布式文件系统
本次环境的搭建参考了 https://blog.csdn.net/qq_43455410/article/details/84797814, 感谢博主. 主要流程如下: 1. 下载fastdfs镜像 ...
- Servlet的引入
一.分析 此模式有问题: 1.jsp需要呼叫javabean StudentService stuService = new StudentServiceImpl(); List<Student ...
- Java中有多少种设计模式?请简单画一下三种常见设计模式的类图?
转载:http://blog.csdn.net/longyulu/article/details/9159589 一.设计模式的分类 总体来说设计模式分为三大类: 创建型模式,共五种:工厂方法模式.抽 ...
- Trie(前缀树)和ternary trie和binary search tree
1 什么是trie trie是一棵多叉树,假如存放的是由26个字母(不区分大小写)构成的字符串的话,那么就是一棵26叉树. trie树是一棵前缀树,因为每个结点只保存字符串中的一个字符,整个字符串保存 ...