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

思路

一看就知道是Splay。。。。如此经典。也是我写过的第二道splay。第一道是文本编辑器,但是本地测AC,提交的话就WA到死。。

只有区间翻转操作,如果我们要翻转[L,R]的话,就将L-1转到根,再将第R+1大的转到根的右子树。把根的右子树的左子树打上翻转标记就行了。

[1,L-1]和[R+1,n]都被转到别的地方去了,所以不会被旋转。

然后别的都是splay的基本操作啦~只要每次记得push_down就行啦~

 #include <iostream>
#include <cstring>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <list>
#include <vector>
#include <ctime>
#include <functional>
#define pritnf printf
#define scafn scanf
#define sacnf scanf
#define For(i,j,k) for(int i=(j);i<=(k);(i)++)
#define Clear(a) memset(a,0,sizeof(a))
using namespace std;
typedef unsigned int Uint;
const int INF=0x3fffffff;
///==============struct declaration==============
struct Node{
int Val;bool Rev;
int siz;
Node *lc,*rc;
Node(){lc=rc=NULL;Rev=false;siz=;}
};
///==============var declaration=================
const int MAXN=;
int n,q;
int num[MAXN];
Node *root=NULL;
///==============function declaration============
void BuildTree(int l,int r,Node *&o);
int find_kth(int rank,Node *&o);
void Splay(int Rank,Node *&o);
void Rrotate(Node *&x);void Lrotate(Node *&x);
void push_down(Node *&x);void Output(Node *&x);
void update(Node *&x);void TestOutput(Node *&x);
///==============main code=======================
int main()
{
#define FILE__
#ifdef FILE__
freopen("input","r",stdin);
freopen("output","w",stdout);
#endif
scanf("%d%d",&n,&q);
BuildTree(,n+,root);//TestOutput(root);printf("\n");
while (q--){
int L,R;scanf("%d%d",&L,&R);
if (L==R) continue;
Splay(L,root);
if (root->lc==NULL)
Splay(R+,root->rc);
else
Splay(R+-root->lc->siz,root->rc);
if (root->rc!=NULL&&root->rc->lc!=NULL)
root->rc->lc->Rev^=;
//TestOutput(root);printf("\n");
}
Output(root);
return ;
}
///================fuction code====================
void BuildTree(int l,int r,Node *&o){
int m=(l+r)>>;
o=new(Node);o->Val=m;
if (l==r) return;
if (m>l) BuildTree(l,m-,o->lc);
if (m<r) BuildTree(m+,r,o->rc);
if (o->lc!=NULL) o->siz+=o->lc->siz;
if (o->rc!=NULL) o->siz+=o->rc->siz;
}
void Lrotate(Node *&x){
push_down(x);push_down(x->lc);
Node *y=x->lc;
x->lc=y->rc;
y->rc=x;x=y;
update(x->rc);update(x);
}
void Rrotate(Node *&x){
push_down(x);push_down(x->rc);
Node *y=x->rc;
x->rc=y->lc;
y->lc=x;x=y;
update(x->lc);update(x);
}
void push_down(Node *&x){
if (x->Rev){
swap(x->lc,x->rc);
x->Rev=false;
if (x->lc!=NULL) x->lc->Rev^=;
if (x->rc!=NULL) x->rc->Rev^=;
}
}
void update(Node *&x){
x->siz=;
if (x->lc!=NULL) x->siz+=x->lc->siz;
if (x->rc!=NULL) x->siz+=x->rc->siz;
}
void Splay(int Rank,Node *&o){
int ls=;push_down(o);
if (o->lc!=NULL) ls=o->lc->siz;
if (Rank==ls+) return;
if (Rank>ls+){
Splay(Rank-ls-,o->rc);
Rrotate(o);
}
else{
Splay(Rank,o->lc);
Lrotate(o);
}
}
void Output(Node *&x){
if (x==NULL) return;
push_down(x);
Output(x->lc);
if (x->Val!=&&x->Val!=n+)
printf("%d ",x->Val);
Output(x->rc);
}
void TestOutput(Node *&x){
if (x==NULL) return;
if (!x->Rev){
printf("%d(",x->Val);
TestOutput(x->lc);
printf(",");
TestOutput(x->rc);
printf(")");
}
else{
printf("%d(",x->Val);
TestOutput(x->rc);
printf(",");
TestOutput(x->lc);
printf(")");
}
}

BZOJ 3223

那个TestOutput是我用来在不改变标记的情况下看数的结构的。

【splay】文艺平衡树 BZOJ 3223的更多相关文章

  1. 3223: Tyvj 1729 文艺平衡树 - BZOJ

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

  2. splay 文艺平衡树 (数据结构)

    题目大意:略 splay维护区间翻转裸题,为了减少不必要的麻烦,多插入两个点,分别是0和n+1 每次找区间的第K个值,就在splay上二分即可 顺便学了一下splay的完美建树,而且splay有一些小 ...

  3. bzoj 3223 文艺平衡树 - Splay

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

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

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

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

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

  6. BZOJ 3223: Tyvj 1729 文艺平衡树-Splay树(区间翻转)模板题

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

  7. fhq_treap || BZOJ 3223: Tyvj 1729 文艺平衡树 || Luogu P3391 【模板】文艺平衡树(Splay)

    题面: [模板]文艺平衡树(Splay) 题解:无 代码: #include<cstdio> #include<cstring> #include<iostream> ...

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

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

  9. BZOJ 3223: Tyvj 1729 文艺平衡树

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

随机推荐

  1. [LeetCode] Implement Trie (Prefix Tree) 实现字典树(前缀树)

    Implement a trie with insert, search, and startsWith methods. Note:You may assume that all inputs ar ...

  2. JQuery Ajax调用asp.net后台方法

    利用JQuery的$.ajax()可以很方便的调用asp.net的后台方法. 先来个简单的实例热热身吧. 1.无参数的方法调用 asp.net code: using System.Web.Scrip ...

  3. 字节、字、bit、byte的关系

    字 word 字节 byte 位 bit 字长是指字的长度 1字=2字节(1 word = 2 byte) 1字节=8位(1 byte = 8bit)  一个字的字长为16 一个字节的字长是8 bps ...

  4. python 补漏计划

    从今天开始把python的细枝末节都梳理下 争取 每星期 两篇博文

  5. 【USACO 3.2】Sweet Butter(最短路)

    题意 一个联通图里给定若干个点,求他们到某点距离之和的最小值. 题解 枚举到的某点,然后优先队列优化的dijkstra求最短路,把给定的点到其的最短路加起来,更新最小值.复杂度是\(O(NElogE) ...

  6. QPS 与 TPS 简介

    QPS:Queries Per Second意思是"每秒查询率",是一台服务器每秒能够相应的查询次数,是对一个特定的查询服务器在规定时间内所处理流量多少的衡量标准. TPS是Tra ...

  7. angularjs $emit $on $broadcast 父子 兄弟之间传值

    父子之间 <div ng-controller="ParentCtrl"> <div ng-controller="ChildCtrl"> ...

  8. CodeForces - 148D Bag of mice

    http://codeforces.com/problemset/problem/148/D 题目大意: 原来袋子里有w只白鼠和b只黑鼠 龙和王妃轮流从袋子里抓老鼠.谁先抓到白色老鼠谁就赢. 王妃每次 ...

  9. ReactNative 根据scrollView/listview滑动距离动态修改NavBar颜色

    我们常见某些APP上滑的时候,NavBar颜色会从透明渐变为某种颜色 原理非常简单,根据scrollView的回调动态修改NavBar的透明度即可. 在RN中,尤其是ListView中这个回调不是很好 ...

  10. 【CoreAnimation】1 到 5

    学习资源来自:图层树 . Quartz 2D Core Animation 复合引擎,职责为尽可能快地组合屏幕上不同的可视内容.这些内容被分解成多个独立的图层,存储在 图层树 的体系中.于是这个树形成 ...