BZOJ3223——Tyvj 1729 文艺平衡树
1、题目大意:维护序列,只有区间翻转这个操作
2、分析:splay的经典操作就是实现区间翻转,就是在splay中有一个标记,表示这个区间被翻转了
然后就是记得各种的操作访问某个点时,记得下传,顺便交换一下左右子树的左右子树(我语文不好
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
using namespace std;
struct Node{
Node *ch[2];
int f, v, s;
int cmp(int x){
if(x < v) return 0;
else if(x == v) return -1;
else return 1;
}
int cmp1(int x){
int k = 1;
if(ch[0]) k += ch[0] -> s;
if(x < k) return 0;
else if(x == k) return -1;
else if(x > k) return 1;
}
void maintain(){
s = 1;
if(ch[0]) s += ch[0] -> s;
if(ch[1]) s += ch[1] -> s;
}
void pushdown(){
if(f == 1){
if(ch[0] != NULL){
ch[0] -> f ^= 1;
swap(ch[0] -> ch[0], ch[0] -> ch[1]);
}
if(ch[1] != NULL){
ch[1] -> f ^= 1;
swap(ch[1] -> ch[0], ch[1] -> ch[1]);
}
f = 0;
}
}
};
struct splay_tree{
Node ft[2000000];
Node *root;
int a[200000], tot;
void rotate(Node* &o, int d){
Node *k = o -> ch[d ^ 1];
k -> pushdown();
o -> ch[d ^ 1] = k -> ch[d];
k -> ch[d] = o;
o -> maintain();
k -> maintain();
o = k;
}
void splay(Node* &o, int k){
if(!o) return;
o -> pushdown();
if(o -> ch[0]) o -> ch[0] -> pushdown();
if(o -> ch[1]) o -> ch[1] -> pushdown();
int d = o -> cmp1(k);
if(d == 1 && o -> ch[0]) k = k - o -> ch[0] -> s - 1;
else if(d == 1) k --;
if(d != -1){
Node* w = o -> ch[d];
int d2 = w -> cmp1(k);
int k2 = k;
if(d2 == 1){
k2 --;
if(w -> ch[0]) k2 -= w -> ch[0] -> s;
}
if(d2 != -1){
splay(w -> ch[d2], k2);
if(d == d2) rotate(o, d ^ 1);
else rotate(o -> ch[d], d);
}
rotate(o, d ^ 1);
}
return;
}
Node* merge(Node *left, Node *right){
if(left == NULL) return right;
if(right == NULL) return left;
splay(left, left -> s);
left -> ch[1] = right;
left -> maintain();
return left;
}
void split(Node* &o, int k, Node* &left, Node* &right){
if(k == 0){
left = NULL;
right = o;
return;
}
splay(o, k);
left = o;
right = o -> ch[1];
left -> ch[1] = NULL;
left -> maintain();
return;
}
void flip(int l, int r){
Node *left, *mid, *right;
split(root, l - 1, left, mid);
split(mid, r - l + 1, mid, right);
swap(mid -> ch[0], mid -> ch[1]);
mid -> f ^= 1;
root = merge(left, merge(mid, right));
}
int value(int l){
Node *left, *mid, *right;
split(root, l - 1, left, mid);
split(mid, 1, mid, right);
int ret = mid -> v;
root = merge(left, merge(mid, right));
return ret;
}
void insert(Node* &o, int l, int r){
if(r < l) return;
int mid = (l + r) / 2;
o = &ft[tot]; tot ++;
o -> ch[0] = o -> ch[1] = NULL;
o -> v = mid;
o -> f = 0;
if(l != r){
insert(o -> ch[0], l, mid - 1);
insert(o -> ch[1], mid + 1, r);
}
o -> maintain();
return;
}
} wt;
int main(){
int n, m;
scanf("%d%d", &n, &m);
for(int i = 1; i <= n; i ++) wt.a[i] = i;
wt.insert(wt.root, 1, n);
for(int i = 1; i <= m; i ++){
int l, r;
scanf("%d%d", &l, &r);
wt.flip(l, r);
}
for(int i = 1; i <= n; i ++){
printf("%d ", wt.value(i));
}
return 0;
}
BZOJ3223——Tyvj 1729 文艺平衡树的更多相关文章
- [BZOJ3223]Tyvj 1729 文艺平衡树
[BZOJ3223]Tyvj 1729 文艺平衡树 试题描述 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:翻转一个区间,例如原有序序列是5 4 3 2 1,翻转区 ...
- BZOJ3223: Tyvj 1729 文艺平衡树 [splay]
3223: Tyvj 1729 文艺平衡树 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 3595 Solved: 2029[Submit][Sta ...
- bzoj3223 Tyvj 1729 文艺平衡树(Splay Tree+区间翻转)
3223: Tyvj 1729 文艺平衡树 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 2202 Solved: 1226[Submit][Sta ...
- 2018.08.05 bzoj3223: Tyvj 1729 文艺平衡树(非旋treap)
传送门 经典的平衡树问题,之前已经用splay写过一次了,今天我突发奇想,写了一发非旋treap的版本,发现挺好写的(虽然跑不过splay). 代码: #include<bits/stdc++. ...
- bzoj3223: Tyvj 1729 文艺平衡树 splay裸题
splay区间翻转即可 /************************************************************** Problem: 3223 User: walf ...
- 【Splay】bzoj3223 Tyvj 1729 文艺平衡树
#include<cstdio> #include<iostream> #include<cstring> #include<algorithm> #i ...
- BZOJ3223: Tyvj 1729 文艺平衡树 无旋Treap
一开始光知道pushdown却忘了pushup......... #include<cstdio> #include<iostream> #include<cstring ...
- 【Splay】【块状链表】bzoj3223 Tyvj 1729 文艺平衡树
让蒟蒻见识到了常数大+滥用STL的危害. <法一>很久之前的Splay #include<cstdio> #include<algorithm> using nam ...
- BZOJ 3223: Tyvj 1729 文艺平衡树
3223: Tyvj 1729 文艺平衡树 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 3628 Solved: 2052[Submit][Sta ...
随机推荐
- zabbix 3.0快速安装简介(centos 7)
zabbix快速安装 系统版本:centos 7 通过yum方法安装Zabbix3.0,安装源为阿里云 yum源配置 rpm -ivh http://mirrors.aliyun.com/zabbix ...
- (总结)Nginx配置文件nginx.conf中文详解
#定义Nginx运行的用户和用户组 user www www; #nginx进程数,建议设置为等于CPU总核心数. worker_processes 8; #全局错误日志定义类型,[ debug | ...
- JS010-DOM
JS010-DOM 本章内容: 1.理解包含不同层次节点的DOM 2.使用不同的节点类型 3.客服浏览器兼容性问题及各种陷阱 DOM(文档对象模型)是针对 HTML和xml文旦过得一个API(应用程序 ...
- css015 定位网页上的元素
css015 定位网页上的元素 一. 定位属性的功能 1. 四中类型的定位 Position: absolute relative fixed static a. 绝对定位 绝对定 ...
- 自然语言20_The corpora with NLTK
QQ:231469242 欢迎喜欢nltk朋友交流 https://www.pythonprogramming.net/nltk-corpus-corpora-tutorial/?completed= ...
- Numerical Optimization: Understanding L-BFGS
http://aria42.com/blog/2014/12/understanding-lbfgs/ Numerical optimization is at the core of much of ...
- 2012 Theory for Forward Rendering
http://miss-cache.blogspot.com/2012/08/lighting-transparent-surfaces-with_26.html http://aras-p.info ...
- Django笔记-MySQL初次使用设置
以下为个人学习时的笔记,正在完善中........... [1]启动服务 [root@bogon /]# service mysqld start正在启动 mysqld: [确定] [root@bog ...
- netstat--查看服务器[有效]连接数--统计端口并发数--access.log分析
简介 Netstat 命令用于显示各种网络相关信息,如网络连接,路由表,接口状态 (Interface Statistics),masquerade 连接,多播成员 (Multicast Member ...
- Hibernate.lock()方法中各种锁的区别
悲观锁 它指的是对数据被外界修改持保守态度.假定任何时刻存取数据时,都可能有另一个客户也正在存取同一笔数据,为了保持数据被操作的一致性,于是对数据采取了数据库层次的锁定状态,依靠数据库提供的锁机制来实 ...