传送门->

这题的正确做法是splay维护这摞书。

但是并不对劲的人选择了暴力(皮这一下很开心)。

#include<algorithm>
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iomanip>
#include<iostream>
#include<map>
#include<queue>
#include<stack>
#include<vector>
#define rep(i,x,y) for(register int i=(x);i<=(y);++i)
#define dwn(i,x,y) for(register int i=(x);i>=(y);--i)
#define re register
#define maxn 300010
using namespace std;
inline int read()
{
int x=0,f=1;
char ch=getchar();
while(isdigit(ch)==0 && ch!='-')ch=getchar();
if(ch=='-')f=-1,ch=getchar();
while(isdigit(ch))x=(x<<3)+(x<<1)+ch-'0',ch=getchar();
return x*f;
}
inline void write(int x)
{
int f=0;char ch[20];
if(!x){puts("0");return;}
if(x<0){putchar('-');x=-x;}
while(x)ch[++f]=x%10+'0',x/=10;
while(f)putchar(ch[f--]);
putchar('\n');
}
int a[maxn],tr[maxn],pos[maxn],n,m,qx,qy,hd,tl;
char s[20];
int lt(int x){return x&(-x);}
void add(int x,int k){for(;x<=n+m*2;x+=lt(x))tr[x]+=k;}
int ask(int x){int k=0;for(;x;x-=lt(x))k+=tr[x];return k;}
int kth(int k)
{
int l=hd,r=tl,ans=tl+233;
while(l<=r)
{
int mi=(r+l)>>1;
int rank=ask(mi);
if(rank==k)ans=min(ans,mi);
if(rank>=k)r=mi-1;
else l=mi+1;
}
return ans;
}
int main()
{
n=read(),m=read();hd=1+m,tl=n+m;
rep(i,1,n)a[i+m]=read(),pos[a[i+m]]=i+m,add(i+m,1);
rep(k,1,m)
{
scanf("%s",s);qx=read();
if(s[0]=='T')add(pos[qx],-1),hd--,add(hd,1),swap(a[hd],a[pos[qx]]),pos[qx]=hd;
if(s[0]=='B')add(pos[qx],-1),tl++,add(tl,1),swap(a[tl],a[pos[qx]]),pos[qx]=tl;
if(s[0]=='I')
{
qy=read();if(!qy)continue;
int tmp=ask(pos[qx])+qy;
// cout<<"rank:"<<tmp<<endl;
int ans=kth(tmp);
// cout<<"ans&pos:"<<qx<<" "<<a[ans]<<pos[qx]<<" ";
int px=pos[qx];
swap(pos[qx],pos[a[ans]]);swap(a[px],a[ans]);// cout<<pos[qx]<<endl;
}
if(s[0]=='A')write(ask(pos[qx])-1);
if(s[0]=='Q')
{
int ans=kth(qx);
write(a[ans]);
}
//cout<<"a:"<<endl;
//rep(i,1,n+m*2)cout<<a[i]<<" ";cout<<endl;
//cout<<"pos:"<<endl;
//rep(i,1,n)cout<<pos[i]<<" ";cout<<endl;
}
return 0;
}
/*
10 10
1 3 2 7 5 8 10 4 9 6
Query 3
Top 5
Ask 6
Bottom 3
Ask 3
Top 6
Insert 4 -1
Query 5
Query 2
Ask 2
*/

  

据说照顾了splay的大肠腧的数据为暴力提供了漏洞。

最新文章

  1. Hive Tutorial(上)(Hive 入门指导)
  2. Python~~~关键字~~~
  3. WebConfig配置
  4. [BI项目记]-搭建代码管理环境之客户端
  5. centos安装配置amoeba以及测试
  6. js运动:多div变宽、二级菜单
  7. Scrapy and Selenium
  8. java疯狂演义----简单java IDE工具
  9. Javascript 运动应用 02
  10. Eclipse / Intellij Idea配置Git+Maven+Jetty开发环境
  11. java入门了解02
  12. LINQ基础(三)
  13. 求从n个数组任意选取一个元素的所有组合
  14. margin、padding单位百分比
  15. HDU 1250
  16. Oracle的AES加密与解密用法
  17. k-means算法MATLAB和opencv代码
  18. bzoj1047 理想的正方形
  19. MyEclipse10配置PyDev进行Python开发
  20. spring boot(4)-html和templates

热门文章

  1. 关于srand()rand()的用法
  2. 潘多拉的盒子(bzoj 1194)
  3. 【POJ1185】炮兵阵地(状压DP)
  4. BZOJ1740: [Usaco2005 mar]Yogurt factory 奶酪工厂
  5. 每日一个linux命令(1)
  6. 转: 关于Linux常用的二进制文件分析方法
  7. Java时间戳转化为今天、昨天、明天(字符串格式)
  8. Unable to connect to database server to retrieve database list; Arcgis 连接不上postsql库;
  9. webpack-Module Resolution(模块解析)
  10. 【甘道夫】并行化频繁模式挖掘算法FP Growth及其在Mahout下的命令使用