bzoj 3223/tyvj 1729 文艺平衡树 splay tree
原题链接:http://www.tyvj.cn/p/1729
这道题以前用c语言写的splay tree水过了。。
现在接触了c++重写一遍。。。
只涉及区间翻转,由于没有删除操作故不带垃圾回收,具体如下:
#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<algorithm>
const int MAX_N = ;
struct Node{
int v, s, rev;
Node *pre, *ch[];
inline void set(int _v = , int _s = , Node *p = NULL){
v = _v, s = _s, rev = ;
pre = ch[] = ch[] = p;
}
inline void push_up(){
s = ch[]->s + ch[]->s + ;
}
inline void update(){
Node *t = ch[];
rev ^= ;
t = ch[];
ch[] = ch[];
ch[] = t;
}
inline void push_down(){
if (rev != ){
rev ^= ;
ch[]->update();
ch[]->update();
}
}
};
struct SplayTree{
Node stack[MAX_N];
Node *tail, *root, *null;
inline Node *newNode(int v){
Node *p = tail++;
p->set(v, , null);
return p;
}
void initialize(int l, int r){
tail = &stack[];
null = tail++;
null->set(-);
root = newNode(-);
root->ch[] = newNode(-);
root->ch[]->pre = root;
Node *x = built(l, r);
root->ch[]->ch[] = x;
x->pre = root->ch[];
root->ch[]->push_up();
root->push_up();
}
inline void rotate(Node *x, int c){
Node *y = x->pre;
y->push_down(), x->push_down();
y->ch[!c] = x->ch[c];
x->pre = y->pre;
if (x->ch[c] != null) x->ch[c]->pre = y;
if (y->pre != null) y->pre->ch[y->pre->ch[] != y] = x;
x->ch[c] = y;
y->pre = x;
y->push_up();
if (y == root) root = x;
}
void splay(Node *x, Node *f){
if (x == root) return;
for (; x->pre != f; x->push_down()){
if (x->pre->pre == f){
rotate(x, x->pre->ch[] == x);
} else {
Node *y = x->pre, *z = y->pre;
if (z->ch[] == y){
if (y->ch[] == x)
rotate(y, ), rotate(x, );
else rotate(x, ), rotate(x, );
} else {
if (y->ch[] == x)
rotate(y, ), rotate(x, );
else rotate(x, ), rotate(x, );
}
}
}
x->push_up();
}
Node *built(int l, int r){
Node *p = null;
if (l > r) return null;
int mid = (l + r) >> ;
p = newNode(mid);
p->ch[] = built(l, mid - );
if (p->ch[] != null) p->ch[]->pre = p;
p->ch[] = built(mid + , r);
if (p->ch[] != null) p->ch[]->pre = p;
p->push_up();
return p;
}
Node *select(Node *x, int k){
int t = ;
Node *ret = x;
for (;;){
ret->push_down();
t = ret->ch[]->s;
if (t == k) break;
if (k < t) ret = ret->ch[];
else k -= t + , ret = ret->ch[];
}
return ret;
}
void travel(Node *x){
if (x != null){
x->push_down();
travel(x->ch[]);
printf("%d ", x->v);
travel(x->ch[]);
}
}
Node *get_range(int l, int r){
splay(select(root, l - ), null);
splay(select(root, r + ), root);
return root->ch[]->ch[];
}
void reverse(int l, int r){
Node *ret = get_range(l, r);
ret->update();
}
void print(int n){
Node *ret = get_range(, n);
travel(ret);
}
}Splay;
int main(){
#ifdef LOCAL
freopen("in.txt", "r", stdin);
freopen("out.txt", "w+", stdout);
#endif
int n, m, a, b;
while (~scanf("%d %d", &n, &m)){
Splay.initialize(, n);
while (m--){
scanf("%d %d", &a, &b);
Splay.reverse(a, b);
}
Splay.print(n);
}
return ;
}
bzoj 3223/tyvj 1729 文艺平衡树 splay tree的更多相关文章
- 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/无旋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输出的时候下放标记,把左儿子和右儿子换一下 记得建树的时候建 ...
- BZOJ 3223: Tyvj 1729 文艺平衡树
3223: Tyvj 1729 文艺平衡树 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 3628 Solved: 2052[Submit][Sta ...
- fhq_treap || BZOJ 3223: Tyvj 1729 文艺平衡树 || Luogu P3391 【模板】文艺平衡树(Splay)
题面: [模板]文艺平衡树(Splay) 题解:无 代码: #include<cstdio> #include<cstring> #include<iostream> ...
- [BZOJ 3223 & Tyvj 1729]文艺平衡树 & [CodeVS 3243]区间翻转
题目不说了,就是区间翻转 传送门:BZOJ 3223 和 CodeVS 3243 第一道题中是1~n的区间翻转,而第二道题对于每个1~n还有一个附加值 实际上两道题的思路是一样的,第二题把值对应到位置 ...
- BZOJ 3223 Tyvj 1729 文艺平衡树(Splay)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=3223 [题目大意] 给出一数列,问m次区间翻转后的结果. [题解] Splay 区间翻 ...
随机推荐
- MBProgressHUD 扩展加载动画
效果图: 设计给了一个组的图片,但是由于是透明的背景,会产生卡顿,其实只要两张图片就可以了 创建一个 MBProgressHUD 分类 增加方法 + (MB_INSTANCETYPE)myShowHU ...
- pb中打开窗口并传递参数
try long ll_result; ll_result=1;openwithparm(w_sb_order,UserCode);catch(RuntimeError er) errorMsg=e ...
- IOS异步和多线程操作&&在sqlite3中的应用
1,数据库I/O操作(异步) 数据库本身是存储在磁盘上.访问和修改数据库,即对磁盘进行读写,即I/O操作. 磁盘属于计算机硬件,具有DMA能力,不需要CPU干预,可以实现异步操作. I/O操作一般是消 ...
- CI系统
- sql case when 多条件
when 'ChangeProductName'= case --联名借姓名 --when a.ChangeProductName is not null then (substr ...
- 检查字符串长度 检查字符串是否为空 用正则表达式验证出版物的ISBN号 用正则表达式验证邮证编码 验证字符串中是否含有汉字
<?php /** * 常用的正则表达式来验证信息.如:网址 邮箱 手机号等 */ class check { /** * 正则表达式验证email格式 * * @param string $s ...
- c++ string 转GUID及反转
#include "stdafx.h" #include <string> #include <windows.h> using namespace std ...
- FTP操作
using System; using System.Collections.Generic; using System.Text; using System.IO; using System.Net ...
- CSS3 图片悬浮缩放效果
<html> <head> <meta http-equiv="Content-Type" content="text/html; char ...
- Python之路【第六篇】:socket
Python之路[第六篇]:socket Socket socket通常也称作"套接字",用于描述IP地址和端口,是一个通信链的句柄,应用程序通常通过"套接字&quo ...