【splay】文艺平衡树 BZOJ 3223
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
思路
一看就知道是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的更多相关文章
- 3223: Tyvj 1729 文艺平衡树 - BZOJ
Description 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:翻转一个区间,例如原有序序列是5 4 3 2 1,翻转区间是[2,4]的话,结果是5 2 3 ...
- splay 文艺平衡树 (数据结构)
题目大意:略 splay维护区间翻转裸题,为了减少不必要的麻烦,多插入两个点,分别是0和n+1 每次找区间的第K个值,就在splay上二分即可 顺便学了一下splay的完美建树,而且splay有一些小 ...
- bzoj 3223 文艺平衡树 - Splay
3223: Tyvj 1729 文艺平衡树 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 3884 Solved: 2235[Submit][Sta ...
- 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 ...
- fhq_treap || BZOJ 3223: Tyvj 1729 文艺平衡树 || Luogu P3391 【模板】文艺平衡树(Splay)
题面: [模板]文艺平衡树(Splay) 题解:无 代码: #include<cstdio> #include<cstring> #include<iostream> ...
- [题解]bzoj 3223 文艺平衡树
3223: Tyvj 1729 文艺平衡树 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 3884 Solved: 2235[Submit][Sta ...
- BZOJ 3223: Tyvj 1729 文艺平衡树
3223: Tyvj 1729 文艺平衡树 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 3628 Solved: 2052[Submit][Sta ...
随机推荐
- [LeetCode] Add Binary 二进制数相加
Given two binary strings, return their sum (also a binary string). For example,a = "11"b = ...
- [LeetCode] First Missing Positive 首个缺失的正数
Given an unsorted integer array, find the first missing positive integer. For example,Given [1,2,0] ...
- FineUI(专业版)公测版发布(这速度,真TM快!)
经过近一年的筹备.编码和测试,FineUI(专业版)公测版终于和大家见面了!现在就来体验一下专业版飞一般的速度吧:http://fineui.com/demo_pro/FineUI(专业版)首页:ht ...
- 将HTML5封装成android应用APK文件的几种方法
越来越多的开发者热衷于使用html5+JavaScript开发移动Web App.不过,HTML5 Web APP的出现能否在未来取代移动应用,就目前来说,还是个未知数.一方面,用户在使用习惯上,不喜 ...
- jQuery 邮箱下拉列表自动补全
综述 我想大家一定见到过,在某个网站填写邮箱的时候,还没有填写完,就会出现一系列下拉列表,帮你自动补全邮箱的功能.现在我们就用jQuery来实现一下. 博主原创代码,如有代码写的不完善的地方还望大家多 ...
- maven
maven常见问题问答 1.前言 Maven,发音是[`meivin],"专家"的意思.它是一个很好的项目管理工具,很早就进入了我的必备工具行列,但是这次为了把project1项目 ...
- asp.net mvc 权限过滤和单点登录(禁止重复登录)
1.权限控制使用controller和 action来实现,权限方式有很多种,最近开发项目使用控制控制器方式实现代码如下 /// <summary> /// 用户权限控制 /// < ...
- centos tar压缩与解压缩命令大全
tar命令详解 -c: 建立压缩档案 -x:解压 -t:查看内容 -r:向压缩归档文件末尾追加文件 -u:更新原压缩包中的文件 这五个是独立的命令,压缩解压都要用到其中一个,可以和别的命令连用但只能用 ...
- ThinkPhp 3.2 自动验证
自动验证是ThinkPHP模型层提供的一种数据验证方法,可以在使用create创建数据对象的时候自动进行数据验证. 数据验证有两种方式: 静态方式:在模型类里面通过$_validate属性定义验证规则 ...
- BOM操作
BOM操作 //浏览器对象模型 opener=window.open(页面的url,打开方式) opener.document.body.style.background="red" ...