平衡树
这题感觉用splay会更好写,我们用平衡树维护相对工资。
用一个变量记录工资增减的变化,也就是说存进树的值是x-w,每次工资减少后,假设记录工资变化的变量为m,直接把m-w的前驱splay到根,删除左子树即可
#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
#define full(a, b) memset(a, b, sizeof a)
using namespace std;
typedef long long ll;
inline int lowbit(int x){ return x & (-x); }
inline int read(){
int X = 0, w = 0; char ch = 0;
while(!isdigit(ch)) { w |= ch == '-'; ch = getchar(); }
while(isdigit(ch)) X = (X << 3) + (X << 1) + (ch ^ 48), ch = getchar();
return w ? -X : X;
}
inline int gcd(int a, int b){ return a % b ? gcd(b, a % b) : b; }
inline int lcm(int a, int b){ return a / gcd(a, b) * b; }
template<typename T>
inline T max(T x, T y, T z){ return max(max(x, y), z); }
template<typename T>
inline T min(T x, T y, T z){ return min(min(x, y), z); }
template<typename A, typename B, typename C>
inline A fpow(A x, B p, C lyd){
A ans = 1;
for(; p; p >>= 1, x = 1LL * x * x % lyd)if(p & 1)ans = 1LL * x * ans % lyd;
return ans;
}
const int N = 1000005;
int n, m, t;
struct Node{
int val, size, num;
Node *son[2], *fa;
}*root, *null, *NI, a[N];
Node *newNode(Node *fa, int val){
Node *node = &a[t++];
node->val = val, node->size = 1, node->num = 1, node->fa = fa;
node->son[0] = node->son[1] = null;
return node;
}
void push_up(Node *node){
if(node == NI) return;
node->size = node->son[0]->size + node->son[1]->size + node->num;
}
void rotate(Node *x, int p){
Node *y = x->fa;
y->son[p^1] = x->son[p];
if(x->son[p] != null) x->son[p]->fa = y;
y->fa->son[y->fa->son[1] == y] = x;
x->fa = y->fa;
x->son[p] = y;
y->fa = x;
push_up(y), push_up(x);
}
void splay(Node *node, Node *goal){
if(node == goal) return;
while(node->fa != goal){
Node *fa = node->fa, *gfa = fa->fa;
if(gfa == goal){
rotate(node, fa->son[0] == node);
break;
}
int p = gfa->son[1] == fa;
if((fa->son[1] == node) == p){
rotate(fa, p^1);
rotate(node, p^1);
}
else{
rotate(node, p);
rotate(node, p^1);
}
}
push_up(node);
if(goal == NI) root = node;
}
void insert(int x){
if(root == null || root == NULL){
root = newNode(NI, x);
return;
}
Node *node = root;
while(node->son[x > node->val] != null){
if(node->val == x) break;
node = node->son[x > node->val];
}
if(node->val == x){
node->size ++, node->num ++;
splay(node, NI);
return;
}
node->son[x > node->val] = newNode(node, x);
splay(node->son[x > node->val], NI);
}
void find(int x){
Node *node = root;
while(x != node->val && node->son[x > node->val] != null)
node = node->son[x > node->val];
splay(node, NI);
}
Node *successor(int x){
find(x);
if(root->val >= x) return root;
Node *node = root->son[1];
while(node->son[0] != null) node = node->son[0];
return node;
}
int del(int x){
int ret = 0;
Node *node = successor(x);
splay(node, NI);
ret += root->son[0]->size;
root->son[0] = null;
push_up(root);
return ret;
}
int select(int k){
Node *node = root;
int m = node->son[0]->size;
while(!(k > m && k <= m + node->num)){
if(k > m){
k -= m + node->num;
node = node->son[1];
}
else node = node->son[0];
m = node->son[0]->size;
}
splay(node, NI);
return node->val;
}
// x + w >= m
// x >= m - w
int main(){
n = read(), m = read();
int w = 0, lf = 0, cnt = 0;
null = &a[t++], NI = &a[t++];
insert(INF), root->size = root->num = 0;
while(n --){
char opt[2]; scanf("%s", opt);
if(opt[0] == 'I'){
int x = read();
if(x >= m) insert(x - w), cnt ++;
}
else if(opt[0] == 'A') w += read();
else if(opt[0] == 'S'){
w -= read();
int tmp = del(m - w);
cnt -= tmp, lf += tmp;
}
else{
int k = read();
if(k > cnt) printf("-1
");
else printf("%d
", select(cnt - k + 1) + w);
}
}
printf("%d
", lf);
return 0;
}