3223: Tyvj 1729 文艺平衡树

Time Limit: 10 Sec  Memory Limit: 128 MB
Submit: 3884  Solved: 2235
[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

5 3

1 3

1 3

1 4

Sample Output

4 3 2 1 5

HINT

N,M<=100000

Source

[Submit][Status][Discuss]


    不是特别难,打个lazy标记就行了,详见[Splay]

 /**
* bzoj
* Problem#3223
* Accepted
* Time:2012ms
* Memory:4336k
*/
#include<iostream>
#include<fstream>
#include<sstream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<ctime>
#include<cctype>
#include<cmath>
#include<algorithm>
#include<stack>
#include<queue>
#include<set>
#include<map>
#include<vector>
using namespace std;
typedef bool boolean;
#define smin(a, b) (a) = min((a), (b))
#define smax(a, b) (a) = max((a), (b))
template<typename T>
inline void readInteger(T& u){
char x;
int aFlag = ;
while(!isdigit((x = getchar())) && x != '-' && x != -);
if(x == -) return;
if(x == '-'){
x = getchar();
aFlag = -;
}
for(u = x - ''; isdigit((x = getchar())); u = (u << ) + (u << ) + x - '');
ungetc(x, stdin);
u *= aFlag;
} template<typename T>
class SplayNode {
public:
T data;
int s;
boolean lazy;
SplayNode* next[];
SplayNode* father;
SplayNode():s(), lazy(){
memset(next, , sizeof(next));
}
SplayNode(T data, SplayNode* father):data(data), father(father), s(), lazy(){
memset(next, , sizeof(next));
}
int cmp(T a){
if(a == data) return -;
return (a > data) ? () : ();
}
int getWhich(SplayNode* p){
return (next[] == p) ? () : ();
}
void maintain(){
s = ;
for(int i = ; i < ; i++)
if(next[i] != NULL)
s += next[i]->s;
}
void pushDown(){
swap(next[], next[]);
for(int i = ; i < ; i++)
if(next[i] != NULL)
next[i]->lazy ^= ;
lazy = false;
}
}; template<typename T>
class Splay {
protected:
inline static void rotate(SplayNode<T>*& node, int d){
SplayNode<T> *father = node->father;
SplayNode<T> *newRoot = node->next[d ^ ];
if(newRoot->lazy) newRoot->pushDown();
node->next[d ^ ] = newRoot->next[d];
node->father = newRoot;
newRoot->next[d] = node;
newRoot->father = father;
if(node->next[d ^ ] != NULL) node->next[d ^ ]->father = node;
if(father != NULL) father->next[father->getWhich(node)] = newRoot;
node->maintain();
node->father->maintain();
} static SplayNode<T>* insert(SplayNode<T>*& node, SplayNode<T>* father, T data){
if(node == NULL){
node = new SplayNode<T>(data, father);
return node;
}
int d = node->cmp(data);
if(d == -) return NULL;
SplayNode<T>* res = insert(node->next[d], node, data);
if(res != NULL) node->maintain();
return res;
} static SplayNode<T>* findKth(SplayNode<T>*& node, int k){
if(node->lazy) node->pushDown();
int ls = (node->next[] != NULL) ? (node->next[]->s) : ();
if(k >= ls + && k <= ls + ) return node;
if(k <= ls) return findKth(node->next[], k);
return findKth(node->next[], k - ls - );
} public:
SplayNode<T> *root; Splay(){ } inline void splay(SplayNode<T>* node, SplayNode<T>* father){
if(node == father) return;
while(node->father != father){
SplayNode<T>* f = node->father;
int fd = f->getWhich(node);
SplayNode<T>* ff = f->father;
if(ff == father){
rotate(f, fd ^ );
break;
}
int ffd = ff->getWhich(f);;
if(ffd == fd){
rotate(ff, ffd ^ );
rotate(f, fd ^ );
}else{
rotate(f, fd ^ );
rotate(ff, ffd ^ );
}
}
if(father == NULL)
root = node;
} inline SplayNode<T>* insert(T data){
SplayNode<T>* res = insert(root, NULL, data);
if(res != NULL) splay(res, NULL);
return res;
} inline SplayNode<T>* findKth(int k, SplayNode<T>* father){
if(k <= || k > root->s) return NULL;
SplayNode<T>* p = findKth(root, k);
splay(p, father);
return p;
} SplayNode<T>* split(int from, int end){
if(from > end) return NULL;
if(from == && end == root->s){
findKth(, NULL);
return this->root;
}
if(from == ){
findKth(end + , NULL);
findKth(from, root);
return root->next[];
}
if(end == root->s){
findKth(from - , NULL);
findKth(end, root);
return root->next[];
}
findKth(end + , NULL);
findKth(from - , root);
return root->next[]->next[];
} void out(SplayNode<T>* node){
if(node == NULL) return;
if(node->lazy) node->pushDown();
out(node->next[]);
printf("%d ", node->data);
out(node->next[]);
} void debugOut(SplayNode<T>* node){ //调试使用函数,打印Splay
if(node == NULL) return;
cout << node->data << "(" << node->s << "," << ((node->father == NULL) ? (-) : (node->father->data)) << "," << node->lazy << "){";
debugOut(node->next[]);
cout << ",";
debugOut(node->next[]);
cout << "}";
}
}; int n, m;
Splay<int> s; int main(){
readInteger(n);
readInteger(m);
for(int i = ; i <= n; i++){
s.insert(i);
}
for(int i = , a, b; i<= m; i++){
readInteger(a);
readInteger(b);
if(a == b) continue;
SplayNode<int>* p = s.split(a, b);
p->lazy ^= ;
}
s.out(s.root);
return ;
}

bzoj 3223 文艺平衡树 - Splay的更多相关文章

  1. bzoj 3223 文艺平衡树 splay 区间翻转

    Tyvj 1728 普通平衡树 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 17715  Solved: 7769[Submit][Status][ ...

  2. bzoj 3223 文艺平衡树 Splay 打标志

    是NOI2003Editor的一个子任务 #include <cstdio> #include <vector> #define maxn 100010 using names ...

  3. [题解]bzoj 3223 文艺平衡树

    3223: Tyvj 1729 文艺平衡树 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 3884  Solved: 2235[Submit][Sta ...

  4. BZOJ 3223 文艺平衡树 [codevs3303翻转区间]

    AC通道:http://www.lydsy.com/JudgeOnline/problem.php?id=3223 通道2:http://codevs.cn/problem/3303/ 题目分析: 我 ...

  5. BZOJ 3223 文艺平衡树

    Description   您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:翻转一个区间,例如原有序序列是5 4 3 2 1,翻转区间是[2,4]的话,结果是5 2  ...

  6. 【阶梯报告】洛谷P3391【模板】文艺平衡树 splay

    [阶梯报告]洛谷P3391[模板]文艺平衡树 splay 题目链接在这里[链接](https://www.luogu.org/problemnew/show/P3391)最近在学习splay,终于做对 ...

  7. luoguP3391[模板]文艺平衡树(Splay) 题解

    链接一下题目:luoguP3391[模板]文艺平衡树(Splay) 平衡树解析 这里的Splay维护的显然不再是权值排序 现在按照的是序列中的编号排序(不过在这道题目里面就是权值诶...) 那么,继续 ...

  8. BZOJ 3223: Tyvj 1729 文艺平衡树(splay)

    速度居然进前十了...第八... splay, 区间翻转,用一个类似线段树的lazy标记表示是否翻转 ------------------------------------------------- ...

  9. bzoj 3223: Tyvj 1729 文艺平衡树 (splay)

    链接: https://www.lydsy.com/JudgeOnline/problem.php?id=3223 题面: 3223: Tyvj 1729 文艺平衡树 Time Limit: 10 S ...

随机推荐

  1. ResourceManager High Availability

    Introduction This guide provides an overview of High Availability of YARN’s ResourceManager, and det ...

  2. python3学习笔记(1)_string

    #python学习笔记 17/07/07 # !/usr/bin/evn python3 # -*- coding:utf-8 -*- #r"" 引号当中的字符串不转义 #练习 # ...

  3. bzip2 zip 压缩后体积比 0.8:1

    1. 对.bz2 后缀文件 跳过不处理 2.逐行同字段的json文件,压缩后大小为原文件的12.81% 测试文件近似认为为逐行json文本数据,没有进行多文件重复测试,没有统计时间: {"u ...

  4. CentOS工作内容(四)主机禁ping

    CentOS工作内容(四)主机禁ping 用到的快捷键 tab 自动补齐(有不知道的吗) ctrl+a 移动到当前行的开头(a ahead) ctrl+u 删除(剪切)此处至开始所有内容 vim 末行 ...

  5. 15 jmeter分布式性能测试

    背景由于jmeter本身的瓶颈,当需要模拟数以千计的并发用户时,使用单台机器模拟所有的并发用户就有些力不从心,甚至还会引起Java内存溢出的错误.要解决这个问题,可以使用分布式测试,运行多台机器运用所 ...

  6. clientHeight , scrollHeight , offsetHeight之间的区别

    clientHeight:元素客户区的大小,指的是元素内容及其边框所占据的空间大小(经过实践取出来的大多是视口大小) scrollHeight: 滚动大小,指的是包含滚动内容的元素大小(元素内容的总高 ...

  7. [py]django表单不清空实现的2种方法

    参考 参考: django实现内容不清空2种方法 django form的作用 1.生成html标签 2.验证输入内容 form生成表单 zhuji/forms.py - 实例化表单 - 定制form ...

  8. [py]你真的了解多核处理器吗? 了解多线程

    越来越多的人搞爬虫,设计到多线程爬取, 还有一些机器学习的一些模块也需要这玩意, 感觉自己不会逼格不高. 抽时间赶紧玩一玩这东西, 希望提高对软件的认知和归属感,不要太傻. cpu内部架构参考 你知道 ...

  9. 机器学习理论基础学习15---条件随机场(CRF)

    一.CRF的由来HMM->MEMM->CRF 二.HMM到MEMM MEMM打破了HMM的观测条件独立假设 三.MEMM到CRF CRF克服了MEMM的label bias problem ...

  10. sql server 中的分区函数用法(partition by 字段)

    partition  by关键字是分析性函数的一部分,它和聚合函数不同的地方在于它能返回一个分组中的多条记录,而聚合函数一般只有一条反映统计值的记录,partition  by用于给结果集分组,如果没 ...