_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输出的时候下放标记,把左儿子和右儿子换一下 记得建树的时候建 ...
随机推荐
- 系统优化(一)Maven打包同一个jar有不同的:版本号+时间戳(解决思路)
解决:maven仓库的ear里面有非常多个同样的jar(仅仅是包括不同的:版本号+时间戳) 问题描写叙述: 发现ear里面有非常多个同样的jar,仅仅是包括不同的:版本号+时间戳,例如以下图所看到的: ...
- C/C++实现bmp文件读写
之前知道点bmp图的格式,然后对8位操作过,然后今天弄了一下24位真彩色的. C++读取.旋转和保存bmp图像文件编程实现 主要是理解bmp文件的格式8/24位的区别 8位图有调色板,24位在文件头和 ...
- CentOS里route命令详解
Route 功能简述:linux系统中的route命令能够用于IP路由表的显示和操作.它的主要作用是创建一个静态路由让指定一个主机或者一个网络通过一个网络接口,如eth0.当使用"add&q ...
- 【Mongodb教程 第一课 补加课】 Failed to connect to 127.0.0.1:27017, reason: errno:10061 由于目标计算机积极拒绝,无法连接
1:启动MongoDB 2014-07-25T11:00:48.634+0800 warning: Failed to connect to 127.0.0.1:27017, reason: errn ...
- Linux——系统调用笔记1
底层文件访问: 进程:运行中的程序,它有一些与值关联的文件描述符,有多少个文件描述符取决于系统配置情况. 当一个程序开始运行时,一般会打开三个文件描述符: 0:标准输入 ...
- sharpdevelop 调整代码字体显示大小
SharpDevelop中使用ctrl+鼠标滚轮可以调整代码的字体显示大小
- 【内存数据库】OracleTimesten连接DSN创建用户
************************************************************************ ****原文:blog.csdn.net/clark_ ...
- nhibernate的关系
用nhibernate,觉得比较难把握的是其中表间的关系. 我用的是Fluently Nhibernate,直接用代码,而不是XML来书写代码与数据表的映射.其中表间关系有3种: 1.Referenc ...
- 定时邮件 已经稳定运行10天+ 从局域网linux到外网邮箱
- JS Debug
任何一个编程者都少不了要去调试代码,不管你是高手还是菜鸟,调试程序都是一项必不可少的工作.一般来说调试程序是在编写代码之后或测试期修改Bug 时进行的,往往在调试代码期间更加能够体现出编程者的水平高低 ...