加的注释,都流着泪。。。

胡了一种做法,样例都没过,翻题解发现一神仙Remove操作,妙啊!

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#define R(a,b,c) for(register int a = (b); a <= (c); ++ a)
#define nR(a,b,c) for(register int a = (b); a >= (c); -- a)
#define Max(a,b) ((a) > (b) ? (a) : (b))
#define Min(a,b) ((a) < (b) ? (a) : (b))
#define Fill(a,b) memset(a, b, sizeof(a))
#define Abs(a) ((a) < 0 ? -(a) : (a))
#define Swap(a,b) a^=b^=a^=b
#define ll long long #define ON_DEBUG #ifdef ON_DEBUG #define D_e_Line printf("\n\n----------\n\n")
#define D_e(x) cout << #x << " = " << x << endl
#define Pause() system("pause")
#define FileOpen() freopen("in.txt","r",stdin); #else #define D_e_Line ;
#define D_e(x) ;
#define Pause() ;
#define FileOpen() ; #endif struct ios{
template<typename ATP>ios& operator >> (ATP &x){
x = 0; int f = 1; char c;
for(c = getchar(); c < '0' || c > '9'; c = getchar()) if(c == '-') f = -1;
while(c >= '0' && c <= '9') x = x * 10 + (c ^ '0'), c = getchar();
x*= f;
return *this;
}
}io;
using namespace std; const int N = 100007; int minLimit, totLeave;
struct Treap{
int ch[2], fa, val, tot, siz;
}t[N];
int root, treeIndex;
inline void Pushup(int rt){
t[rt].siz = t[t[rt].ch[0]].siz + t[t[rt].ch[1]].siz + t[rt].tot;
}
inline int Ident(int x){
return t[t[x].fa].ch[1] == x;
}
inline void Rotate(int x){
int y = t[x].fa, z = t[y].fa, k = Ident(x);
t[z].ch[Ident(y)] = x, t[x].fa = z;
t[y].ch[k] = t[x].ch[k ^ 1], t[t[x].ch[k ^ 1]].fa = y;
t[x].ch[k ^ 1] = y, t[y].fa = x;
Pushup(y), Pushup(x);
}
inline void Splay(int x, int pos){
while(t[x].fa != pos){
int y = t[x].fa, z = t[y].fa;
if(z != pos){
Ident(x) == Ident(y) ? Rotate(y) : Rotate(x);
}
Rotate(x);
}
if(!pos) root = x;
}
inline void Insert(int x){
if(x < minLimit) return;
int u = root, fa = 0;
while(u && t[u].val != x){ // !
fa = u;
u = t[u].ch[x > t[u].val];
}
if(u){
++t[u].tot;
}
else{
u = ++treeIndex;
t[u].ch[0] = t[u].ch[1] = 0;
t[u].siz = t[u].tot = 1;
t[u].fa = fa;
t[u].val = x;
if(fa) t[fa].ch[x > t[fa].val] = u;
}
Splay(u, 0);
}
inline void Find(int x){
int u = root;
if(!u) return;
while(t[u].val != x && t[u].ch[x > t[u].val]) u = t[u].ch[x > t[u].val]; // !
Splay(u, 0); // !
return;
}
inline void Add(int x){
R(i,1,treeIndex) t[i].val += x;
}
inline void Sub(int x){
R(i,1,treeIndex) t[i].val -= x;
} inline int Next(int x, int type){ // the minimu number >= x
Find(x);
int u = root;
if((t[u].val >= x && type) || (t[u].val < x && !type)) return u; // >= ?
u = t[u].ch[type];
while(t[u].ch[type ^ 1]) u = t[u].ch[type ^ 1];
return u;
} inline void Remove(int x){ // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
int rt = Next(x + minLimit, 1);
Splay(rt, 0);
totLeave += t[t[rt].ch[0]].siz;
t[rt].ch[0] = 0;
Pushup(rt);
Sub(x);
} inline int Kth(int x){
int u = root;
if(t[u].siz <= x) return -1;
++x;
while(1){
int v = t[u].ch[1];
if(t[v].siz + t[u].tot < x){
x -= t[v].siz + t[u].tot;
u = t[u].ch[0];
}
else if(x <= t[v].siz){ // !
u = v;
}
else
return t[u].val;
}
} int main(){
//FileOpen();
//freopen("my.txt","w",stdout);
int m;
io >> m >> minLimit;
Insert(2147483647);
// Insert(-2147483647);
while(m --){
char opt = getchar();
while(opt != 'I' && opt != 'S' && opt != 'A' && opt != 'F') opt = getchar();
int x;
io >> x;
if(opt == 'I'){
Insert(x);
}
else if(opt == 'A'){
Add(x);
}
else if(opt == 'S'){
Remove(x);
}
else{
printf("%d\n",Kth(x));
}
} printf("%d", totLeave);
return 0;
}

最新文章

  1. DNS-2
  2. mysql 5.5多实例部署【图解】
  3. android获取状态栏高度
  4. [SVN(ubuntu)] svn 文件状态标记含义
  5. NodeJS 各websocket框架性能分析
  6. HOG特征(Histogram of Gradient)总结(转载)
  7. ### Theano
  8. Java单实例的最佳写法
  9. jni cocos2d-x移植到android:helloworld
  10. delphi 功能函数大全-备份用
  11. LeetCode(2) || Add Two Numbers &amp;&amp; Longest Substring Without Repeating Characters
  12. C++学习笔记33 转换操作符
  13. 安卓手机微信页面position: fixed位置错误
  14. virtualbox, vt-s, rmmod kvm-intel
  15. angr进阶(5)内存操作
  16. 使用Intellij中的Spring Initializr来快速构建Spring Boot/Cloud工程(十五)
  17. ansible经常使用模块使用方法
  18. [加密]展讯secureboot方案
  19. JS 中的布尔运算符 &amp;&amp; 和 ||
  20. 九九乘法表的python复习

热门文章

  1. Python &lt;算法思想集结&gt;之初窥基础算法
  2. 2.26NK周赛
  3. 验证cuda和cudnn是否安装成功(转载)
  4. Python列表推导式,字典推导式,元组推导式
  5. 第1章 C++绪论
  6. C语言- 基础数据结构和算法 - 动态数组
  7. Camunda定时器事件示例Demo(Timer Events)
  8. 『忘了再学』Shell流程控制 — 35、多分支case条件语句
  9. sql-自动增长的列
  10. 用console画条龙?