BZOJ 3223: Tyvj 1729 文艺平衡树
3223: Tyvj 1729 文艺平衡树
Time Limit: 10 Sec Memory Limit: 128 MB
Submit: 3628 Solved: 2052
[Submit][Status][Discuss]
Description
您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:翻转一个区间,例如原有序序列是5 4 3 2 1,翻转区间是[2,4]的话,结果是5 2 3 4 1
Input
第一行为n,m n表示初始序列有n个数,这个序列依次是(1,2……n-1,n) m表示翻转操作次数
接下来m行每行两个数[l,r] 数据保证 1<=l<=r<=n
Output
输出一行n个数字,表示原始序列经过m次变换后的结果
Sample Input
1 3
1 3
1 4
Sample Output
HINT
N,M<=100000
Source
小生的第三道伸展树板子题。初识Splay维护区间翻转操作。引用一位前辈的题解。、
splay的经典操作:翻转区间-->交换左右子树,注意打标记降低翻转次数。如何找到要操作的区间[l,r]:将当前排名(size)为l-1 +1 的节点转到根,将当前排名为r+2的节点转到根的右子树的根节点,则根的右子树的根节点的左子树为所求区间,直接打标记就可以了。
#include <bits/stdc++.h> class Splay {
public:
Splay(void) {
root = NULL;
for (top = ; top < siz; ++top)
stk[top] = tree + top;
} inline void auto_build(int n) {
for (int i = ; i <= n + ; ++i)
insert(i);
} inline void insert(int val) {
if (root == NULL)
root = newnode(val, NULL);
else {
node *t = root;
while (t->son[] != NULL)
t = t->son[];
splay(t, NULL);
t->son[] = newnode(val, t);
update(root); splay(t->son[], NULL);
}
} inline void reverse(int l, int r) {
++l, ++r;
splay(rnk(l - ), NULL);
splay(rnk(r + ), root);
reverse(root->son[]->son[]);
} inline void print(int n) {
for (int i = ; i <= n; ++i)
printf("%d ", rnk(i + )->value);
}
private:
const static int siz = 1e5 + ; struct node {
int size;
int value;
bool reverse;
node *father;
node *son[];
}*root; node tree[siz], *stk[siz]; int top; inline node *newnode(int v, node *f) {
node *ret = stk[--top];
ret->size = ;
ret->value = v;
ret->father = f;
ret->son[] = NULL;
ret->son[] = NULL;
ret->reverse = false;
return ret;
} inline void freenode(node *t) {
stk[top++] = t;
} inline int size(node *t) {
return t == NULL ? : t->size;
} inline void update(node *t) {
t->size = ;
t->size += size(t->son[]);
t->size += size(t->son[]);
} inline bool son(node *f, node *s) {
if (f == NULL)return false;
return f->son[] == s;
} inline bool tag(node *t) {
return t == NULL ? false : t->reverse;
} inline void reverse(node *t) {
if (t != NULL)
t->reverse ^= true;
} inline void pushdown(node *t) {
if (tag(t)) {
std::swap(t->son[], t->son[]);
reverse(t->son[]);
reverse(t->son[]);
t->reverse ^= true;
}
} inline void connect(node *f, node *s, bool k) {
if (f == NULL)
root = s;
else
f->son[k] = s;
if (s != NULL)
s->father = f;
} inline void rotate(node *t) {
node *f = t->father;
node *g = f->father;
bool a = son(f, t), b = !a;
connect(f, t->son[b], a);
connect(g, t, son(g, f));
connect(t, f, b);
update(f);
update(t);
} inline void splay(node *t, node *p) {if (t)
while (t->father != p) {
node *f = t->father;
node *g = f->father;
pushdown(g);
pushdown(f);
pushdown(t);
if (g == p)
rotate(t);
else {
if (son(g, f) ^ son(f, t))
rotate(t), rotate(t);
else
rotate(f), rotate(t);
}
}
} inline node *find(int val) {
node *ret = root;
while (ret != NULL && ret->value != val)
pushdown(ret), ret = ret->son[val >= ret->value];
return ret;
} inline node *rnk(int kth) {
for (node *t = root; t; ) {
pushdown(t);
if (size(t->son[]) < kth) {
kth -= size(t->son[]);
if (kth == )
return t;
else
--kth, t = t->son[];
}
else
t = t->son[];
}
}
}S; signed main(void) {
int n, m; scanf("%d%d", &n, &m);
S.auto_build(n);
for (int i = , l, r; i <= m; ++i)
scanf("%d%d", &l, &r), S.reverse(l, r);
S.print(n);
}
@Author: YouSiki
BZOJ 3223: Tyvj 1729 文艺平衡树的更多相关文章
- 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]文艺平衡树 & [CodeVS 3243]区间翻转
题目不说了,就是区间翻转 传送门:BZOJ 3223 和 CodeVS 3243 第一道题中是1~n的区间翻转,而第二道题对于每个1~n还有一个附加值 实际上两道题的思路是一样的,第二题把值对应到位置 ...
- fhq_treap || BZOJ 3223: Tyvj 1729 文艺平衡树 || Luogu P3391 【模板】文艺平衡树(Splay)
题面: [模板]文艺平衡树(Splay) 题解:无 代码: #include<cstdio> #include<cstring> #include<iostream> ...
- bzoj 3223/tyvj 1729 文艺平衡树 splay tree
原题链接:http://www.tyvj.cn/p/1729 这道题以前用c语言写的splay tree水过了.. 现在接触了c++重写一遍... 只涉及区间翻转,由于没有删除操作故不带垃圾回收,具体 ...
- BZOJ 3223 Tyvj 1729 文艺平衡树(Splay)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=3223 [题目大意] 给出一数列,问m次区间翻转后的结果. [题解] Splay 区间翻 ...
- 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输出的时候下放标记,把左儿子和右儿子换一下 记得建树的时候建 ...
随机推荐
- AFN3.0封装
总结了一下AFN3.0封装,也借鉴了其他人总结的,整理如下,希望多交流,互相进步 // // XJYSessionManager.h// // Created by XJY on 16/10/17. ...
- js 多选 反选
//$(".435__1").attr("checked", true); //$(".435__0").removeAttr(" ...
- 使用memadmin可视化监视我们的memcache
相信还是有很多项目使用memcache,可能有些人说有点out了,但是呢??? 项目上的东西不是你想换就能换的...谁都想多一事不如少 一事,大面积更换之后所面临的未知风险可能让你无法承受,但是呢, ...
- KVM 存储虚拟化 - 每天5分钟玩转 OpenStack(7)
KVM 的存储虚拟化是通过存储池(Storage Pool)和卷(Volume)来管理的. Storage Pool 是宿主机上可以看到的一片存储空间,可以是多种类型,后面会详细讨论.Volume 是 ...
- WIN 下的超动态菜单(三)代码
WIN 下的超动态菜单(一)简介 WIN 下的超动态菜单(二)用法 WIN 下的超动态菜单(三)代码 作者:黄山松,发表于博客园:http://www.cnblogs.com/tomview/ 超动态 ...
- nginx 301 永久重定向
nginx301跳转设置很简单,配置如下. (配置文件默认为nginx.conf,如果制定了新的配置文件,在新的文件配置即可.) server{ server_name xxx.com www.xxx ...
- java设计模式之外观模式
外观模式概念 外观模式又称为门面模式,为子系统中的一组接口提供一个一致的界面,此模式定义了一个搞层次接口,使得这一个子系统更加容易使用.这一模式完美的体现了依赖倒转原则和迪米特法则的思想,所以是非常常 ...
- NOIP2015斗地主[DFS 贪心]
题目描述 牛牛最近迷上了一种叫斗地主的扑克游戏.斗地主是一种使用黑桃.红心.梅花.方片的A到K加上大小王的共54张牌来进行的扑克牌游戏.在斗地主中,牌的大小关系根据牌的数码表示如下:3<4< ...
- DFA 最小化
NDFA.εNDFA 确定化的细节这里就不总结了,这里说一说DFA最小化的算法. 关于DFA最小化,
- 第18章 集合框架(2)-Set接口
第18章 集合框架(2)-Set接口 Set是Collection子接口,模拟了数学上的集的概念 Set集合存储特点 1.不允许元素重复 2.不会记录元素的先后添加顺序 Set只包含从Collecti ...