【题意概述】

  要求维护一个序列支持以下操作:

  1,插入元素x;

  2,把序列的所有元素加上x;

  3,把序列的所有元素减去x,同时低于一个给定的下限的元素马上被删除;

  4,询问序列中第k大的元素。

【题解】

  Treap

  用一个delta记录元素的增减情况,即实际上的元素的值应该是qval(root,x)+delta,那么插入元素时插入的应该是Val-delta.

  每次加减操作的时候改delta和下限Minwage,加操作的时候Minwage-=val(即下限相对于元素值减小了Val),减操作的时候Minwage+=val(即下限相对于元素值增加了Val)

  每次减操作时分裂出序列里小于Minwage的元素,把它们扔掉即可。

 #include<cstdio>
#include<algorithm>
#define ls (a[u].l)
#define rs (a[u].r)
using namespace std;
const int maxn=;
int n,k,x,y,z,v,tot,root,Minwage,Minnow,delta;
char c,c2;
struct treap{int l,r,v,rnd,size;}a[maxn];
inline int read(){
int k=,f=; char c=getchar();
while(c<''||c>'')c=='-'&&(f=-),c=getchar();
while(''<=c&&c<='')k=k*+c-'',c=getchar();
return k*f;
}
void newnode(int val){a[++tot]=(treap){,,val,rand()+,};}
void update(int u){a[u].size=a[ls].size+a[rs].size+;}
void split(int u,int k,int &x,int &y){
if(!k){x=; y=u; return;}
if(a[u].size==k){x=u; y=; return;}
if(a[ls].size>=k) split(ls,k,x,ls),y=u;
else split(rs,k-a[ls].size-,rs,y),x=u;
update(u);
}
int merge(int x,int y){
if(!x||!y) return x+y;
if(a[x].rnd<a[y].rnd){a[x].r=merge(a[x].r,y); update(x); return x;}
else{a[y].l=merge(x,a[y].l); update(y); return y;}
}
int qrank(int u,int val){
if(!u) return ;
return a[u].v>=val?qrank(ls,val):qrank(rs,val)+a[ls].size+;
}
int qval(int u,int k){
if(a[ls].size+==k) return a[u].v;
return a[ls].size>=k?qval(ls,k):qval(rs,k-a[ls].size-);
}
int main(){
srand();
n=read(); Minwage=Minnow=read();
while(n--){
c=getchar();
while(c!='I'&&c!='A'&&c!='S'&&c!='F') c=getchar();
c2=getchar(); v=read();
if(c=='I'&&v>=Minwage){
split(root,qrank(root,v-delta),x,y);
newnode(v-delta); root=merge(merge(x,tot),y);
}
if(c=='A') Minnow-=v,delta+=v;
if(c=='S'){
Minnow+=v; delta-=v;
split(root,qrank(root,Minnow),x,y);
root=y;
}
if(c=='F') printf("%d\n",a[root].size>=v?qval(root,a[root].size-v+)+delta:-);
}
return printf("%d",tot-a[root].size),;
}

最新文章

  1. webpack进阶构建项目(一)
  2. Elasticsearch——Search的基本介绍
  3. 零售业数据分析的媒介——BI工具
  4. 161028、Nginx负载均衡实现tomcat集群方案简要小结
  5. 集合类 Contains 方法 深入详解 与接口的实例
  6. Codeforces Educational Codeforces Round 3 C. Load Balancing 贪心
  7. Hadoop的伪分布式搭建
  8. 2016.08.06计算几何总结测试day1
  9. VM Depot 登陆中国!
  10. python高级编程之选择好名称:完
  11. 博客搬到了http://xianglong.me
  12. python中的二维数组90度旋转
  13. ip forward
  14. ecos新命令
  15. ReentrantLock 以及 AQS 实现原理
  16. eml企业通讯录管理系统v5.0 存在sql注入
  17. linux_快照和克隆
  18. Flyway数据表迁移框架的使用
  19. Ibatis中的&lt;trim&gt;标签应用
  20. js+jquery手写弹出提示框

热门文章

  1. Linux 下查找文件或文件夹
  2. 使用SimpleAdapter 适配器时显示网络上图片方法
  3. 2017iOS开发最新的打包测试步骤(亲测)
  4. bzoj4668
  5. 单纯形&amp;&amp;线性规划
  6. 用vue-cli快速构建项目
  7. query.setFirstResult(0),query.setMaxResults(4)
  8. Spring Cloud (4) 服务消费者-Feign
  9. 关于java.util.properties的随笔
  10. DeadObjectException