[题解]bzoj 3223 文艺平衡树
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
1 3
1 3
1 4
Sample Output
HINT
N,M<=100000
Source
不是特别难,打个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 文艺平衡树的更多相关文章
- bzoj 3223 文艺平衡树 - Splay
3223: Tyvj 1729 文艺平衡树 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 3884 Solved: 2235[Submit][Sta ...
- bzoj 3223 文艺平衡树 splay 区间翻转
Tyvj 1728 普通平衡树 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 17715 Solved: 7769[Submit][Status][ ...
- BZOJ 3223 文艺平衡树 [codevs3303翻转区间]
AC通道:http://www.lydsy.com/JudgeOnline/problem.php?id=3223 通道2:http://codevs.cn/problem/3303/ 题目分析: 我 ...
- BZOJ 3223 文艺平衡树
Description 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:翻转一个区间,例如原有序序列是5 4 3 2 1,翻转区间是[2,4]的话,结果是5 2 ...
- bzoj 3223 文艺平衡树 Splay 打标志
是NOI2003Editor的一个子任务 #include <cstdio> #include <vector> #define maxn 100010 using names ...
- [bzoj3224]普通平衡树/3223文艺平衡树
这是一道很普通的题.. 最近花了很多时间来想要去干什么,感觉自己还是太拿衣服 做这道题是因为偶尔看到了lavender的blog和她的bzoj早期AC记录,就被题目深深地吸引到了,原因有二: 自己sp ...
- 3223. 文艺平衡树【平衡树-splay】
Description 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:翻转一个区间,例如原有序序列是5 4 3 2 1,翻转区间是[2,4]的话,结果是5 2 3 ...
- 【题解】P3391 文艺平衡树
用pb_ds库中的rope水过去的,忽然发现这玩意能水好多模拟题. 详见这个博客:背景的小姐姐真的好看 声明 #include <ext/rope> using namespace __g ...
- BZOJ 3223: Tyvj 1729 文艺平衡树
3223: Tyvj 1729 文艺平衡树 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 3628 Solved: 2052[Submit][Sta ...
随机推荐
- Hibernate锁机制
业务逻辑的实现过程中,往往需要保证数据访问的排他性.因此,我们就需要通过一些机制来保证这些数据在某个操作过程中不会被外界修改,这样的机制,在这里,也就是所谓的“锁”,即给我们选定的目标数据上锁,使其无 ...
- PAT1006
At the beginning of every day, the first person who signs in the computer room will unlock the door, ...
- MFC乱七八糟笔记
1.CBitmap------------------------------------------------------------------------------------- 1.类层次 ...
- Bootstrap 模态对话框只加载一次 remote 数据的解决办法 转载
http://my.oschina.net/qczhang/blog/190215 摘要 前端框架 Bootstrap 的模态对话框,可以使用 remote 选项指定一个 URL,这样对话框在第一次弹 ...
- 【转载】区间DP
http://www.cnblogs.com/zsboy/archive/2013/03/08/2950261.html 博客园 首页 新随笔 联系 订阅 管理 定义区间DP 区间动态规划问题一般 ...
- FusionCharts生成报表应用
1.需要组装要展示的数据,至于如何怎样去设计数据模型,看你要展示的图形和需要的数据就行了.来个简单的. 实体类,只有两个属性,也可以使用Bean里面的实体类,无所谓了. package com.gol ...
- 2016大连网络赛 Football Games
Football Games Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) P ...
- 深入理解最强桌面地图控件GMAP.NET ---离线地图
enjoyeclipse 深入理解最强桌面地图控件GMAP.NET ---离线地图 这章会介绍GMAP.NET的核心功能之一:离线地图.这个功能可以满足很多政府项目.保密项目.或者由于种种原因不能上网 ...
- hrbustoj 2130 一笔画(状态压缩)
基础状态压缩 转移方程为 dp[j][i] = min(dp[j][i],dp[k][i^(1<<j)] + dis[k][j]); #include<iostream> #i ...
- STM32开发指南-按键输入实验
I/O口做为输入的例子.通过配置寄存器设置为输入口,检测对应寄存器的值,判读输入状态,按键是否被按下.