[LSGDOJ1822]书架 Splay
题目描述
输入
输出
样例输入
样例输出
提示
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<cstdlib>
#include<ctime>
using namespace std;
const int N=,INF=;
struct node
{
int x,size,book;
node *child[],*fa;
}a[N*];
node *pos=a,*root,*id[N];
void newnode(node *&r,int key,node *ff,int number)
{
r=pos++;
r->size=,id[number]=r;
r->x=key;r->fa=ff;r->book=number;
r->child[]=r->child[]=NULL;
}
int gi()
{
int str=;char ch=getchar();
while(ch>'' || ch<'')ch=getchar();
while(ch>=''&& ch<='')str=str*+ch-'',ch=getchar();
return str;
}
int n,m;
void updata(node *&r)
{
if(r){
r->size=(r->child[]?r->child[]->size:)+(r->child[]?r->child[]->size:)+;
return ;
}
}
void rotate(node *&r,bool t)
{
node *y=r->fa;
y->child[!t]=r->child[t];
if(r->child[t])r->child[t]->fa=y;
r->fa=y->fa;
if(y->fa)y->fa->child[y->fa->child[]==y]=r;
r->child[t]=y;
y->fa=r;
updata(y);
updata(r);
updata(r->fa);
}
void splay(node *r,node *g)
{
while(r->fa!=g)
{
if(r->fa->fa==g)rotate(r,r->fa->child[]==r);
else{
node *y=r->fa;
bool t=y->fa->child[]==y;
if(y->child[t]==r)rotate(r,!t);
else rotate(y,t);
rotate(r,t);
}
}
if(g==NULL)root=r;
}
void insert(node *&r,int key,node *fa,int number)
{
if(r==NULL){
newnode(r,key,fa,number);
splay(r,NULL);
return;
}
else insert(r->child[key>r->x],key,r,number);
}
node *pre,*nxt;
char s[];
void getpre(node *r,int key)
{
if(!r)return ;
if(r->x>=key)getpre(r->child[],key);
else pre=r,getpre(r->child[],key);
}
void getnext(node *r,int key)
{
if(!r)return ;
if(r->x<=key)getnext(r->child[],key);
else nxt=r,getnext(r->child[],key);
}
void Ask(int x)
{
splay(id[x],NULL);
printf("%d\n",root->child[]?root->child[]->size:);
return ;
}
int getrank(node *r,int rk)
{
while(r){
int d=r->child[]?r->child[]->size:;
if(rk==d+)return r->book;
if(rk<d+)r=r->child[];
else rk-=d+,r=r->child[];
}
return -;
}
node *findmax(node *r)
{
if(r->child[])return findmax(r->child[]);
else return r;
}
int maxn=,minn=;
void Delet(int x)
{
node *y;
splay(id[x],NULL);
if(root->child[]){
y=findmax(root->child[]);
splay(y,root);
y->child[]=root->child[];
y->fa=NULL;
if(root->child[])
root->child[]->fa=y;
root=y;
updata(y);
}
else{
root=root->child[];
root->fa=NULL;
}
}
void Totop(int x,bool t)
{
Delet(x);
if(!t)
insert(root,--minn,NULL,x);
else
insert(root,++maxn,NULL,x);
}
void change(int x,int y)
{
node *kl;
if(y==)getnext(root,id[x]->x),kl=nxt;
else getpre(root,id[x]->x),kl=pre;
y=kl->book;
swap(id[x],id[y]);
swap(id[x]->book,id[y]->book);
}
int main()
{
int x,y;
n=gi();m=gi();maxn=n;
for(int i=;i<=n;i++){
x=gi();
insert(root,i,NULL,x);
}
int cc=;
while(m--){
scanf("%s%d",s,&x);
if(s[]=='A')Ask(x);
else if(s[]=='Q')printf("%d\n",getrank(root,x));
else if(s[]=='I'){scanf("%d",&y);if(y)change(x,y);}
else if(s[]=='T')Totop(x,);
else if(s[]=='B')Totop(x,);
}
return ;
}
[LSGDOJ1822]书架 Splay的更多相关文章
- BZOJ 1861: [Zjoi2006]Book 书架 splay
1861: [Zjoi2006]Book 书架 Description 小T有一个很大的书柜.这个书柜的构造有些独特,即书柜里的书是从上至下堆放成一列.她用1到n的正整数给每本书都编了号. 小T在看书 ...
- BZOJ-1861 Book 书架 Splay
1861: [Zjoi2006]Book 书架 Time Limit: 4 Sec Memory Limit: 64 MB Submit: 1010 Solved: 588 [Submit][Stat ...
- [题解]bzoj 1861 Book 书架 - Splay
1861: [Zjoi2006]Book 书架 Time Limit: 4 Sec Memory Limit: 64 MBSubmit: 1396 Solved: 803[Submit][Stat ...
- P2596 [ZJOI2006]书架 && Splay 区间操作(三)
P2596 [ZJOI2006]书架 题目描述 小T有一个很大的书柜.这个书柜的构造有些独特,即书柜里的书是从上至下堆放成一列.她用1到n的正整数给每本书都编了号. 小T在看书的时候,每次取出一本书, ...
- 洛谷.2596.[ZJOI2006]书架(Splay)
题目链接 /* 五个操作: 1.将某元素置顶.删掉这个数,插入最左 2.将某元素置底.同样 3.旋到根后,直接将这个数与前驱/后继交换所有信息 不是左右子节点! 4.5.裸平衡树 ps:1.用pos[ ...
- BZOJ1861:[ZJOI2006]书架(Splay)
Description 小T有一个很大的书柜.这个书柜的构造有些独特,即书柜里的书是从上至下堆放成一列.她用1到n的正整数给每本书都编了号. 小T在看书的时候,每次取出一本书,看完后放回书柜然后再拿下 ...
- 洛谷 P2596 [ZJOI2006]书架 (splay)
题目描述 小T有一个很大的书柜.这个书柜的构造有些独特,即书柜里的书是从上至下堆放成一列.她用1到n的正整数给每本书都编了号. 小T在看书的时候,每次取出一本书,看完后放回书柜然后再拿下一本.由于这些 ...
- bzoj1861 书架 splay版
单点插入删除以及求前缀 #include<cstdio> #include<cstring> #include<algorithm> using namespace ...
- BZOJ 1861: [Zjoi2006]Book 书架 | SPlay 板题
#include<cstdio> #include<algorithm> #include<cstring> #define N 80010 #define whi ...
随机推荐
- Python打包分发工具setuptools
作为Python标准的打包及分发工具,setuptools可以说相当地简单易用.它会随着Python一起安装在你的机器上.你只需写一个简短的setup.py安装文件,就可以将你的Python应用打包 ...
- day-4 python多进程编程知识点汇总
1. python多进程简介 由于Python设计的限制(我说的是咱们常用的CPython).最多只能用满1个CPU核心.Python提供了非常好用的多进程包multiprocessing,他提供了一 ...
- 关于移动web教程免费发布
各位老铁大家好,最近经历了太多太多,精力一直不能集中做自己愿意做的事情. 移动Web课程一开始设置收费10块,其实本意是让大家感觉有支出,就会相对珍惜好好学习,但是发现收费把大部分人挡在门外,现在恢复 ...
- linux 下 /bin /sbin 的区别
/bin,/sbin,/usr/bin,/usr/sbin区别 / : this is root directory root 用户根目录 /bin : command ...
- AutoCAD中的扩展字典及扩展记录(C#)
在学习CAD扩展记录的过程中,遇到了一些问题,也积累了一些经验,现在给大家分享一些我的学习心得.在学习扩展字典之前需要读者了解cad的组码,也就是DxfCode.感兴趣的也可以了解一下扩展数据的相关内 ...
- xftp上传文件失败,执行程序发现磁盘满了:No space left on device
参考链接 No space left on device 解决Linux系统磁盘空间满的办法http://www.cnblogs.com/aspirant/p/3604801.html如何解决linu ...
- SpringCloud的注解:汇总篇
使用注解之前要开启自动扫描功能,如下配置中base-package为需要扫描的包(含子包): 1 <context:component-scan base-package="cn.te ...
- ASP.NET CORE系列【二】使用Entity Framework Core进行增删改查
介绍 EntityFrameworkCore EF core 是一个轻量级的,可扩展的EF的跨平台版本.对于EF而言 EF core 包含许多提升和新特性,同时 EF core 是一个全新的代码库,并 ...
- Python入门之函数的嵌套/名称空间/作用域/函数对象/闭包函数
本篇目录: 一.函数嵌套 二.函数名称空间与作用域 三.函数对象 四.闭包函数 ============================================================ ...
- python开发:初识python
python简介 Python可以应用于众多领域,如:数据分析.组件集成.网络服务.图像处理.数值计算和科学计算等众多领域.目前业内几乎所有大中型互联网企业都在使用Python,如:Youtube.D ...