[BZOJ3223]Tyvj 1729 文艺平衡树
[BZOJ3223]Tyvj 1729 文艺平衡树
试题描述
您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:翻转一个区间,例如原有序序列是5 4 3 2 1,翻转区间是[2,4]的话,结果是5 2 3 4 1
输入
第一行为n,m n表示初始序列有n个数,这个序列依次是(1,2……n-1,n) m表示翻转操作次数
接下来m行每行两个数[l,r] 数据保证 1<=l<=r<=n
输出
输入示例
输出示例
数据规模及约定
N,M<=100000
题解
裸 splay,注意最后要多输出一个空格。
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <algorithm>
using namespace std; int read() {
int x = 0, f = 1; char c = getchar();
while(!isdigit(c)){ if(c == '-') f = -1; c = getchar(); }
while(isdigit(c)){ x = x * 10 + c - '0'; c = getchar(); }
return x * f;
} #define maxn 100010
struct Node {
int v, siz; bool rev;
Node() {}
Node(int _): v(_), rev(0) {}
} ns[maxn];
int rt, ToT, fa[maxn], ch[2][maxn];
void maintain(int o) {
ns[o].siz = 1;
for(int i = 0; i < 2; i++) if(ch[i][o])
ns[o].siz += ns[ch[i][o]].siz;
return ;
}
void build(int& o, int l, int r) {
if(l > r) return ;
int mid = l + r >> 1;
ns[o = ++ToT] = Node(mid);
if(l == r) return maintain(o);
build(ch[0][o], l, mid - 1); build(ch[1][o], mid + 1, r);
if(ch[0][o]) fa[ch[0][o]] = o;
if(ch[1][o]) fa[ch[1][o]] = o;
return maintain(o);
}
void pushdown(int o) {
for(int i = 0; i < 2; i++) if(ch[i][o])
ns[ch[i][o]].rev ^= ns[o].rev;
if(ns[o].rev) swap(ch[0][o], ch[1][o]), ns[o].rev = 0;
return ;
}
void rotate(int u) {
int y = fa[u], z = fa[y], l = 0, r = 1;
if(z) ch[ch[1][z]==y][z] = u;
if(ch[1][y] == u) swap(l, r);
fa[u] = z; fa[y] = u; fa[ch[r][u]] = y;
ch[l][y] = ch[r][u]; ch[r][u] = y;
maintain(y); maintain(u);
return ;
}
int S[maxn], top;
void splay(int u) {
int t = u; S[++top] = t;
while(fa[t]) t = fa[t], S[++top] = t;
while(top) pushdown(S[top--]);
while(fa[u]) {
int y = fa[u], z = fa[y];
if(z) {
if(ch[0][y] == u ^ ch[0][z] == y) rotate(u);
else rotate(y);
}
rotate(u);
}
return ;
}
int split(int u) {
if(!u) return 0;
splay(u);
int tmp = ch[1][u];
fa[tmp] = 0; ch[1][u] = 0;
maintain(u);
return tmp;
}
int merge(int a, int b) {
if(!a) return maintain(b), b;
if(!b) return maintain(a), a;
pushdown(a); while(ch[1][a]) a = ch[1][a], pushdown(a);
splay(a);
ch[1][a] = b; fa[b] = a;
return maintain(a), a;
}
int qkth(int o, int k) {
if(!o) return 0;
pushdown(o);
int ls = ch[0][o] ? ns[ch[0][o]].siz : 0;
if(k == ls + 1) return o;
if(k > ls + 1) return qkth(ch[1][o], k - ls - 1);
return qkth(ch[0][o], k);
}
int Find(int k) {
rt = 1; while(fa[rt]) rt = fa[rt];
return qkth(rt, k);
}
void Rev(int ql, int qr) {
int lrt = Find(ql - 1), mrt = Find(qr), rrt;
split(lrt); rrt = split(mrt);
ns[mrt].rev ^= 1;
mrt = merge(lrt, mrt); merge(mrt, rrt);
return ;
} int main() {
int n = read(), q = read(); build(rt, 1, n);
while(q--) {
int l = read(), r = read();
Rev(l, r);
} for(int i = 1; i <= n; i++) {
int k = Find(i);
printf("%d ", ns[k].v);
} return 0;
}
[BZOJ3223]Tyvj 1729 文艺平衡树的更多相关文章
- 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 文艺平衡树
1.题目大意:维护序列,只有区间翻转这个操作 2.分析:splay的经典操作就是实现区间翻转,就是在splay中有一个标记,表示这个区间被翻转了 然后就是记得各种的操作访问某个点时,记得下传,顺便交换 ...
- 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 ...
随机推荐
- [LeetCode] Populating Next Right Pointers in Each Node II 每个节点的右向指针之二
Follow up for problem "Populating Next Right Pointers in Each Node". What if the given tre ...
- Lind.DDD.RedisClient~对StackExchange.Redis调用者的封装及多路复用技术
回到目录 两雄争霸 使用StackExchange.Redis的原因是因为它开源,免费,而对于商业化的ServiceStack.Redis,它将一步步被前者取代,开源将是一种趋势,商业化也值得被我们尊 ...
- JS组件系列——BootstrapTable+KnockoutJS实现增删改查解决方案(三):两个Viewmodel搞定增删改查
前言:之前博主分享过knockoutJS和BootstrapTable的一些基础用法,都是写基础应用,根本谈不上封装,仅仅是避免了html控件的取值和赋值,远远没有将MVVM的精妙展现出来.最近项目打 ...
- Android开发之扫描二维码开发
原贴地址:http://www.cnblogs.com/Fndroid/p/5540688.html 二维码其实有很多种,但是我们常见的微信使用的是一种叫做QRCode的二维码,像下面这样的,可以放心 ...
- Java_jvisualvm使用JMX连接远程机器(实践)
https://my.oschina.net/heroShane/blog/196227 一.启动普通的jar程序 1.执行foo.jar启动命令 java -Dcom.sun.management. ...
- .net的一致性哈希实现
最近在项目的微服务架构推进过程中,一个新的服务需要动态伸缩的弹性部署,所有容器化示例组成一个大的工作集群,以分布式处理的方式来完成一项工作,在集群中所有节点的任务分配过程中,由于集群工作节点需要动态增 ...
- 【hrbust2294】方方正正
题意 哈理工2016级新生程序设计全国邀请赛C题 一个r行c列的01矩阵,告诉你每行的和.每列的和,问是否存在这样的矩阵? 题解 首先,行和和列和之和要相等,否则一定是NO. 然后根据Gale-Rys ...
- C#执行Dos命令公用方法
private static string InvokeCmd(string cmdArgs) { string Tstr = ""; Process p = new Proces ...
- angular服务二
angular服务 $http 实现客户端与服务器端异步请求 get方式 test.html <!DOCTYPE html> <html lang="en"> ...
- JUC学习笔记--Thread多线程基础
实现多线程的两种方法 java 实现多线程通过两种方式1.继承Thread类 ,2.实现Runnable接口 class Newthead extends Thread{ public void ru ...