hdu-1890-Robotic Sort splay区间翻转
题意:
依次找第i大的数下标pos[i],然后将区间[i,pos[i]]翻转
分析:
splay树区间翻转
- // File Name: ACM/HDU/1890.cpp
- // Author: Zlbing
- // Created Time: 2013年08月10日 星期六 20时26分39秒
- #include<iostream>
- #include<string>
- #include<algorithm>
- #include<cstdlib>
- #include<cstdio>
- #include<set>
- #include<map>
- #include<vector>
- #include<cstring>
- #include<stack>
- #include<cmath>
- #include<queue>
- using namespace std;
- #define CL(x,v); memset(x,v,sizeof(x));
- #define INF 0x3f3f3f3f
- #define LL long long
- #define REP(i,r,n) for(int i=r;i<=n;i++)
- #define RREP(i,n,r) for(int i=n;i>=r;i--)
- #define L ch[x][0]
- #define R ch[x][1]
- #define KT (ch[ ch[rt][1] ][0])
- const int MAXN = ;
- int cmp(pair<int,int> a,pair<int,int> b){
- if(a.first!=b.first) return a.first<b.first;
- return a.second < b.second;
- }
- int mp[MAXN];
- int id[MAXN];
- pair<int,int> num[MAXN];
- struct SplayTree {
- int sz[MAXN];
- int ch[MAXN][];
- int pre[MAXN];
- int rt,top;
- inline void down(int x){
- if(flip[x]) {
- flip[ L ] ^= ;
- flip[ R ] ^= ;
- swap(L,R);
- flip[x]=;
- }
- }
- inline void up(int x){
- sz[x]=+sz[ L ] + sz[ R ];
- }
- inline void Rotate(int x,int f){
- int y=pre[x];
- down(y);
- down(x);
- ch[y][!f] = ch[x][f];
- pre[ ch[x][f] ] = y;
- pre[x] = pre[y];
- if(pre[x]) ch[ pre[y] ][ ch[pre[y]][] == y ] =x;
- ch[x][f] = y;
- pre[y] = x;
- up(y);
- }
- inline void Splay(int x,int goal){//将x旋转到goal的下面
- down(x);//防止pre[x]就是目标点,下面的循环就进不去了,x的信息就传不下去了
- while(pre[x] != goal){
- down(pre[pre[x]]); down(pre[x]);down(x);//在旋转之前需要先下传标记,因为节点的位置可能会发生改变
- if(pre[pre[x]] == goal) Rotate(x , ch[pre[x]][] == x);
- else {
- int y=pre[x],z=pre[y];
- int f = (ch[z][]==y);
- if(ch[y][f] == x) Rotate(x,!f),Rotate(x,f);
- else Rotate(y,f),Rotate(x,f);
- }
- }
- up(x);
- if(goal==) rt=x;
- }
- inline void RTO(int k,int goal){//将第k位数旋转到goal的下面
- int x=rt;
- down(x);
- while(sz[ L ]+ != k) {
- if(k < sz[ L ] + ) x=L;
- else {
- k-=(sz[ L ]+);
- x = R;
- }
- down(x);
- }
- Splay(x,goal);
- }
- void vist(int x){
- if(x){
- printf("结点%2d : 左儿子 %2d 右儿子 %2d %2d flip:%d\n",x,L,R,val[x],flip[x]);
- vist(L);
- vist(R);
- }
- }
- void Newnode(int &x,int c,int f){
- x=++top;flip[x]=;
- L = R = ; pre[x] = f;
- sz[x]=; val[x]=c;
- }
- inline void build(int &x,int l,int r,int f){
- if(l>r) return ;
- int m=(l+r)>>;
- Newnode(x,num[id[m]].first,f);
- mp[id[m]]=x;
- build(L , l , m- , x);
- build(R , m+ , r , x);
- pre[x]=f;
- up(x);
- }
- inline void init(){
- ch[][]=ch[][]=pre[]=sz[]=;
- rt=top=; flip[]=; val[]=;
- }
- void Del(){
- int t=rt;
- if(ch[rt][]) {
- rt=ch[rt][];
- RTO(,);
- ch[rt][]=ch[t][];
- if(ch[rt][]) pre[ch[rt][]]=rt;
- }
- else rt=ch[rt][];
- pre[rt]=;
- up(rt);
- }
- int flip[MAXN];
- int val[MAXN];
- }spt;
- int main()
- {
- int n;
- while(~scanf("%d",&n))
- {
- if(!n)break;
- spt.init();
- REP(i,,n)
- {
- scanf("%d",&num[i].first);
- num[i].second=i;
- }
- sort(num+,num+n+,cmp);
- REP(i,,n)
- id[num[i].second]=i;
- spt.build(spt.rt,,n,);
- REP(i,,n)
- {
- spt.Splay(mp[i],);
- int ans=i+spt.sz[spt.ch[spt.rt][]];
- spt.flip[spt.ch[spt.rt][]]^=;
- spt.Del();
- if(i==)printf("%d",ans);
- else printf(" %d",ans);
- }
- printf("\n");
- }
- return ;
- }
hdu-1890-Robotic Sort splay区间翻转的更多相关文章
- hdu 1890 Robotic Sort(splay 区间反转+删点)
题目链接:hdu 1890 Robotic Sort 题意: 给你n个数,每次找到第i小的数的位置,然后输出这个位置,然后将这个位置前面的数翻转一下,然后删除这个数,这样执行n次. 题解: 典型的sp ...
- HDU 1890 - Robotic Sort - [splay][区间反转+删除根节点]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1890 Time Limit: 6000/2000 MS (Java/Others) Memory Li ...
- HDU 1890 Robotic Sort | Splay
Robotic Sort Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) [Pr ...
- hdu1890 Robotic Sort (splay+区间翻转单点更新)
Problem Description Somewhere deep in the Czech Technical University buildings, there are laboratori ...
- 数据结构(Splay平衡树):HDU 1890 Robotic Sort
Robotic Sort Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tota ...
- HDU 1890 Robotic Sort (splay tree)
Robotic Sort Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tota ...
- HDU 1890 Robotic Sort(splay)
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=1890 [题意] 给定一个序列,每次将i..P[i]反转,然后输出P[i],P[i]定义为当前数字i ...
- hdu 1890 Robotic Sort
原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1890 如下: #include<cstdio> #include<cstdlib&g ...
- hdu 1890 Robotic SortI(splay区间旋转操作)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1890 题解:splay又一高级的功能,区间旋转这个是用线段树这些实现不了的,这题可以学习splay的旋 ...
随机推荐
- Apache MINA 框架之默认session管理类实现
DefaultSocketSessionConfig 类 extends AbstractSocketSessionConfig extends AbstractIoSessionConfig imp ...
- MVC ViewEngine视图引擎解读及autofac的IOC运用实践
MVC 三大特色 Model.View.Control ,这次咱们讲视图引擎ViewEngine 1.首先看看IViewEngine接口的定义 namespace System.Web.Mvc { ...
- 一次利用MSSQL的SA账户提权获取服务器权限
遇到小人,把服务器整走了 自己手里只有sql server的sa账号密码 模糊记起之前用这个账户提权读取文件的事 百度之,发现相关信息一堆堆 各种工具也用了不少 发现不是语法错误就是权限不够 无奈之下 ...
- java异常类的使用
1.异常的概念 什么是异常?程序出错分为两部分,编译时出粗和运行时出错.编译时出错是编译器在编译源码时发生的错误: 运行时出错是在编译通过,在运行时出现的错误.这种情况叫异常. 例如:数组越界,除数为 ...
- 移动触摸事件(touchstart、touchmove和touchend)
touchstart事件:当手指触摸屏幕时候触发,即使已经有一个手指放在屏幕上也会触发. touchmove事件:当手指在屏幕上滑动的时候连续地触发.在这个事件发生期间,调用preventDefaul ...
- 浅谈angular框架
最近新接触了一个js框架angular,这个框架有着诸多特性,最为核心的是:MVVM.模块化.自动化双向数据绑定.语义化标签.依赖注入,以上这些全部都是属于angular特性,虽然说它的功能十分的强大 ...
- (一)Nodejs - 框架类库 - Nodejs异步流程控制Async
简介 Async是一个流程控制工具包,提供了直接而强大的异步功能 应用场景 业务流程逻辑复杂,适应异步编程,减少回调的嵌套 安装 npm insatll async 函数介绍 Collections ...
- 根据CreateDirectory递归创建多级目录
分为MFC下的和非MFC下的两种,MFC路径是CString类型的,非MFC的路径是wstring类型的. 下面是MFC下的创建目录: void __fastcall RecursiveDirecto ...
- DX笔记之一---Direct3D基础
一.预备知识 1.表面 表面就是Direct3D用于储存2D图像数据的一个像素矩阵.width和height以像素为单位,pitch以字节单位,用接口IDirect3DSurface来描述表面 Loc ...
- 每天一条linux命令——login
login命令用于给出登录界面,可用于重新登录或者切换用户身份,也可通过它的功能随时更换登入身份.当/etc/nologin文件存在时,系统只root帐号登入系统,其他用户一律不准登入. 语法: lo ...