luogu P1486 [NOI2004]郁闷的出纳员

传送门

真是比出纳员还郁闷...

splay有点无法理解 可能是被机房dalao影响心态了吧

嘤嘤嘤还是Treap好写

这题主要难度在于批量删除并统计答案

发现其实删除子树还是很方便的

统计答案我就删一个ans++

其实可以统计插入了几个和最后剩几个这样应该能方便点

Code:

  1 #include<cstdio>
  2 #include<iostream>
  3 #include<cstdlib>
  4 #include<ctime>
  5 #include<cstring>
  6 #include<vector>
  7 #include<cmath>
  8 #include<algorithm>
  9 #define rep(i,a,n) for(int i = a;i <= n;i++)
 10 #define per(i,n,a) for(int i = n;i >= a;i--)
 11 using namespace std;
 12 typedef long long ll;
 13 int read() {
 14     int as = 0,fu = 1;
 15     char c = getchar();
 16     while(c < '0' || c > '9') {
 17         if(c == '-') fu = -1;
 18         c = getchar();
 19     }
 20     while(c >= '0' && c <= '9') {
 21         as = as * 10 + c - '0';
 22         c = getchar();
 23     }
 24     return as * fu;
 25 }
 26 const int N = 300005;
 27 //head
 28 int root,ans,tot;
 29 int val[N],key[N],sze[N],sn[2][N];
 30 #define ls sn[0][x]
 31 #define rs sn[1][x]
 32 inline void pup(int x) {if(x) sze[x] = sze[ls] + sze[rs] + 1;}
 33 
 34 inline void Rot(int& x,int d) {
 35     int tmp = sn[d^1][x];
 36     sn[d^1][x] = sn[d][tmp];
 37     sn[d][tmp] = x;
 38     pup(x),pup(tmp);
 39     x = tmp;
 40 }
 41 
 42 void Ins(int &x,int v) {
 43     if(!x) {
 44         x = ++tot;
 45         sze[x] = 1,val[x] = v,key[x] = rand();
 46     } else {
 47         bool d = v > val[x];
 48         Ins(sn[d][x],v);
 49         if(key[sn[d][x]] > key[x]) Rot(x,d^1);
 50     }
 51     pup(x);
 52 }
 53 
 54 void Del(int &x,int v) {
 55     if(val[x] == v) {
 56         ans++;
 57         if(!ls || !rs) return void(x = ls|rs);
 58         if(val[ls] > val[rs]) Rot(x,0),Del(ls,v);
 59         else Rot(x,1),Del(rs,v);
 60     } else Del(sn[v > val[x]][x],v);
 61     pup(x);
 62 }
 63 
 64 void Delt(int &x) {
 65     if(!x) return;
 66     Delt(ls),Delt(rs);
 67     pup(x),x = 0,ans++;
 68 }
 69 
 70 void Dlowerbound(int &x,int v) {
 71     if(!x) return;
 72     if(v > val[x]) {
 73         Delt(ls),Dlowerbound(rs,v);
 74         Del(x,val[x]);
 75     } else Dlowerbound(ls,v);
 76     pup(x);
 77 }
 78 
 79 int rnkq(int x,int rnk) {
 80     if(rnk <= sze[ls]) return rnkq(ls,rnk);
 81     if(rnk <= sze[ls] + 1) return val[x];
 82     return rnkq(rs,rnk - sze[ls] - 1);
 83 }
 84 int n,x,minn,delta;
 85 char cmd[10];
 86 int main() {
 87     n = read(),minn = read();
 88     rep(i,1,n) {
 89         scanf("%s%d",cmd,&x);
 90         if(cmd[0] == 'I') if(x >= minn) Ins(root,x - delta);
 91         if(cmd[0] == 'A') delta += x;
 92         if(cmd[0] == 'S') {
 93             delta -= x;
 94             int tmp = minn - delta;
 95             Dlowerbound(root,tmp);
 96         }
 97         if(cmd[0] == 'F') {
 98             if(sze[root] < x) puts("-1");
 99             else printf("%d
",rnkq(root,sze[root] - x + 1) + delta);
100         }
101     }
102     printf("%d
",ans);
103     return 0;
104 }
原文地址:https://www.cnblogs.com/yuyanjiaB/p/10101546.html