记录编号 484059 评测结果 RRRRRRRRRR
题目名称 [CQOI2011]动态逆序对 最终得分 0
用户昵称 Gravatar泪寒之雪 是否通过 未通过
代码语言 C++ 运行时间 0.000 s
提交时间 2018-01-21 16:27:33 内存使用 0.00 MiB
显示代码纯文本
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. int delta, n, m, leave;
  4. struct Treap{
  5. struct Node{
  6. int v, r, s;
  7. Node* ch[2];
  8. int cmp(int x) const{
  9. if(x == v) return -1;
  10. else return x < v ? 0 : 1;
  11. }
  12. void maintain(){
  13. s = ch[0]->s + ch[1]->s +1;
  14. }
  15. };
  16. Node *root, *null;
  17. Treap(){
  18. null = new Node();
  19. root = null;
  20. }
  21. void rotate(Node* &o, int d){
  22. Node* k = o->ch[d^1];
  23. o->ch[d^1] = k->ch[d];
  24. k->ch[d] = o;
  25. o->maintain();
  26. k->maintain();
  27. o = k;
  28. }
  29. void insert(Node* &o, int x){
  30. if(o == null){
  31. o = new Node();
  32. o->ch[0] = o->ch[1] = null;
  33. o->v = x;
  34. o->r = rand();
  35. o->s = 1;
  36. }
  37. else{
  38. int d = (x < o->v ? 0 : 1);
  39. insert(o->ch[d], x);
  40. if(o->ch[d]->r > o->r) rotate(o, d^1);
  41. }
  42. o->maintain();
  43. }
  44. int del(Node* &o, int x){
  45. if(o == null) return 0;
  46. if(o->v < x){
  47. int t = o->ch[0]->s + 1;
  48. o = o->ch[1];
  49. return t + del(o, x);
  50. }
  51. else{
  52. int t = del(o->ch[0], x);
  53. o->s -= t;
  54. return t;
  55. }
  56. }
  57. int find(Node* o, int k){
  58. if(o == null || k <= 0 || k > o->s) return 0;
  59. int s = (o->ch[1] == null ? 0 : o->ch[1]->s);
  60. if(s + 1 == k) return o->v;
  61. if(s >= k) return find(o->ch[1], k);
  62. return find(o->ch[0], k-s-1);
  63. }
  64. } tp;
  65. int main(){
  66. freopen("cashier.in", "r", stdin);
  67. freopen("cashier.out","w",stdout);
  68. scanf("%d %d\n", &n, &m);
  69. char p[10];
  70. int x;
  71. tp = Treap();
  72. while(n--){
  73. scanf("%s%d\n", p, &x);
  74. if(p[0] == 'I') if(x >= m) tp.insert(tp.root, x-delta);
  75. if(p[0] == 'A') delta += x;
  76. if(p[0] == 'S'){ delta -= x; leave += tp.del(tp.root, m-delta); }
  77. if(p[0] == 'F'){
  78. if(x > tp.root->s) printf("-1\n");
  79. else printf("%d\n", tp.find(tp.root, x)+delta);
  80. }
  81. }
  82. printf("%d\n", leave);
  83. return 0;
  84. }