BZOJ 1506 郁闷的出纳员 (treap)

Posted dwtfukgv

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了BZOJ 1506 郁闷的出纳员 (treap)相关的知识,希望对你有一定的参考价值。

题意:中文题。

析:用treap 维护一个序列,用一个变量来记录全体人员加的工资和扣除的工资,对于每次扣除时,把一小于的最低工资的删除,很容易维护。

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#include <list>
#include <assert.h>
#include <bitset>
#define debug() puts("++++");
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define fi first
#define se second
#define pb push_back
#define sqr(x) ((x)*(x))
#define ms(a,b) memset(a, b, sizeof a)
//#define sz size()
#define pu push_up
#define pd push_down
#define cl clear()
#define all 1,n,1
#define FOR(i,x,n)  for(int i = (x); i < (n); ++i)
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std;

typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 1e20;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 1e5 + 1000;
const int maxm = 100 + 10;
const ULL mod = 10007;
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, -1, 0, 1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c) {
  return r >= 0 && r < n && c >= 0 && c < m;
}

int val, ans;
struct Node{
  Node *ch[2];
  int r, v, s;
  Node(int vv) : v(vv) { ch[0] = ch[1] = 0; r = rand(); s = 1; }
  bool operator < (const Node &rhs) const{
    return r < rhs.r;
  }
  int cmp(int x) const{
    if(x == v)  return -1;
    return x < v ? 0 : 1;
  }
  void maintain(){
    s = 1;
    if(ch[0])  s += ch[0]->s;
    if(ch[1])  s += ch[1]->s;
  }
};

void rotate(Node *&o, int d){
  Node *k = o->ch[d^1];
  o->ch[d^1] = k->ch[d];
  k->ch[d] = o;
  o->maintain();
  k->maintain();  o = k;
}

void insert(Node *&o, int x){
  if(o == 0)  o = new Node(x);
  else{
    int d = (x < o->v ? 0 : 1);
    insert(o->ch[d], x);
    if(o->ch[d]->r > o->r)  rotate(o, d^1);
  }
  o->maintain();
}

void del(Node *&o){
  if(o == 0)  return ;
  ++ans;
  del(o->ch[0]);
  del(o->ch[1]);
  delete o;
  o = 0;
}

void remove(Node *&o, int x){
  if(o == 0)  return ;
  while(o && x > o->v){
    del(o->ch[0]);
    if(o->ch[1])  rotate(o, 0);
    else del(o);
  }
  if(o == 0)  return ;
  remove(o->ch[0], x);
  o->maintain();
}

int get_kth(Node *o, int k){
  if(o == 0 || k <= 0 || k > o->s)  return -1;
  int s = o->ch[1] == 0 ? 0 : o->ch[1]->s;
  if(k == s + 1)  return o->v + val;
  else if(k <= s)  return get_kth(o->ch[1], k);
  return get_kth(o->ch[0], k-s-1);
}

Node *root;

int main(){
  srand(233333);
  scanf("%d %d", &n, &m);
  int x;  root = 0;
  char op[5];

  while(n--){
    scanf("%s %d", op, &x);
    if(op[0] == ‘I‘){
      if(x < m)  continue;
      insert(root, x - val);
    }
    else if(op[0] == ‘A‘)  val += x;
    else if(op[0] == ‘S‘){
      val -= x;  x = m - val;
      remove(root, x);
    }
    else  printf("%d\n", get_kth(root, x));
  }
  printf("%d\n", ans);
  return 0;
}

  

以上是关于BZOJ 1506 郁闷的出纳员 (treap)的主要内容,如果未能解决你的问题,请参考以下文章

[BZOJ1503] [NOI2004] 郁闷的出纳员 (treap)

bzoj 1503 郁闷的出纳员

bzoj 1503 郁闷的出纳员

Bzoj1503--Noi2004郁闷的出纳员

洛谷 1486 郁闷的出纳员Treap

Noi2004郁闷的出纳员-treap树