Treap

各种模板到处飞。。。
lrj版

#include<cstdlib>

struct Node {
  Node *ch[2]; // 左右子树
  int r; // 随机优先级
  int v; // 值
  int s; // 结点总数
  Node(int v = 0):v(v) { ch[0] = ch[1] = NULL; r = rand(); s = 1; }
  int cmp(int x) const {
    if (x == v) return -1;
    return x < v ? 0 : 1;
  }
  void maintain() {
    s = 1;
    if(ch[0] != NULL) s += ch[0]->s;
    if(ch[1] != NULL) 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 == NULL) o = new Node(x);
  else {
    int d = (x < o->v ? 0 : 1); // 不要用cmp函数,因为可能会有相同结点
    insert(o->ch[d], x);
    if(o->ch[d]->r > o->r) rotate(o, d^1);
  }
  o->maintain();
}

Node* find(Node* o, int x) {
  if(o == NULL) return NULL;
  if(x == o->v) return o;
  return x < o->v ? find(o->ch[0], x) : find(o->ch[1], x);
}

// 要确保结点存在
void remove(Node* &o, int x) {
  int d = o->cmp(x);
  if(d == -1) {
    Node* u = o;
    if(o->ch[0] != NULL && o->ch[1] != NULL) {
      int d2 = (o->ch[0]->r > o->ch[1]->r ? 1 : 0);
      rotate(o, d2); remove(o->ch[d2], x);
    } else {
      if(o->ch[0] == NULL) o = o->ch[1]; else o = o->ch[0];
      delete u;
    }
  } else
    remove(o->ch[d], x);
  if(o != NULL) o->maintain();
}

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

// 在以o为根的子树中,值比x小的结点总数加1
int rank(Node* o, int x) {
  if(o == NULL) return 1;
  if(x <= o->v) return rank(o->ch[0], x);
  return rank(o->ch[1], x) + (o->ch[0] == NULL ? 0 : o->ch[0]->s) + 1;
}

#include<cstdio>
const int INF = 1000000000;

int main() {
  int m, c, v;
  Node* root = new Node(INF);
  freopen("bzoj3224.in", "r", stdin);
  freopen("bzoj3224.out", "w", stdout);
  scanf("%d", &m);
    while(m--) {
      scanf("%d%d", &c, &v);
      if(c == 1) insert(root, v);
      else if(c == 2) {
        Node* o = find(root, v);
        //printf("%d\n", o == NULL ? 0 : 1);
        if(o != NULL) remove(root, v);
      }
      else if(c == 3) printf("%d\n", rank(root, v));
      else if(c == 4) printf("%d\n", kth(root, v));
      else if(c == 5) {
        int t = rank(root, v);
        printf("%d\n", kth(root, t-1));
      }
      else if(c == 6) {
        int t = rank(root, v), k = kth(root, t);
        while(k == v) k = kth(root, ++t);
        printf("%d\n", k);
      }
    }
  fclose(stdin);
  fclose(stdout);
  return 0;
}

指针版

#include <cstdio>
#include <cstdlib>

inline int read()
{
    int x = 0, f = 1, t = getchar();
    while(t < '0' || t > '9') t == '-' ? f = -1 : 0, t = getchar();
    while(t >= '0' && t <= '9') x = (x<<1)+(x<<3)+t-'0', t = getchar();
    return x * f;
}

struct Node
{
    Node *ch[2];
    int r, v, sz;
    Node(int v = 0): v(v) { ch[0] = ch[1] = NULL, r = rand(), sz = 1; }
    int cmp(int x)
    {
        if(x == v) return -1;
        return x < v ? 0 : 1;
    }
    void maintain()
    {
        sz = 1;
        if(ch[0]) sz += ch[0]->sz;
        if(ch[1]) sz += ch[1]->sz;
    }
}*root = NULL;

inline 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 == NULL) 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();
}
Node* find(Node *o, int x)
{
    if(o == NULL) return NULL;
    int d = o->cmp(x);
    return d == -1 ? o : find(o->ch[d], x);
}
//void remove(Node *&o, int x)
//{
//  int d = o->cmp(x);
//  if(d == -1)
//  {
//      Node *k = o;
//      if(o->ch[0] && o->ch[1]) d = o->ch[0]->r > o->ch[1]->r ? 1 : 0, rotate(o, d), remove(o->ch[d], x);
//      else o = o->ch[0] ? o->ch[0] : o->ch[1], free(k), k = NULL;
//  }
//  else remove(o->ch[d], x);
//  if(o) o->maintain();
//}
//void remove(Node *&o)
//{
//  if(o->ch[0]) remove(o->ch[0]);
//  if(o->ch[1]) remove(o->ch[1]);
//  free(o), o = NULL;
//}
int del(Node *&o, int x) //delete all nodes whose value is lower than x
{
    if(o == NULL) return 0;
    int cnt = 0;
    if(o->v >= x) cnt = del(o->ch[0], x);
    else
    {
        cnt = (o->ch[0] ? o->ch[0]->sz : 0) + 1;
        //if(o->ch[0]) remove(o->ch[0]);
        o = o->ch[1];
        cnt += del(o, x);
    }
    if(o) o->maintain();
    return cnt;
}
int kth(Node *o, int k) //the minimum kth number
{
    if(o == NULL || k <= 0 || k > o->sz) return 0;
    int s = o->ch[0] ? o->ch[0]->sz : 0;
    if(k == s+1) return o->v;
    else if(k <= s) return kth(o->ch[0], k);
    else return kth(o->ch[1], k-s-1);
}

int main()
{
#ifndef ONLINE_JUDGE
    freopen("bzoj1503.in", "r", stdin);
    freopen("bzoj1503.out", "w", stdout);
#endif

    register int T = read(), M = read();
    register int op, k, add = 0, ans = 0;
    while(T--)
    {
        op = getchar(), k = read();
        switch(op)
        {
            case 'I': if(k >= M) insert(root, k-add); break;
            case 'A': add += k; break;
            case 'S': add -= k, ans += del(root, M-add); break;
            case 'F': if(root == NULL || k > root->sz) puts("-1"); else printf("%d\n", kth(root, root->sz-k+1)+add); break;
        }
    }
    printf("%d\n", ans);

#ifndef ONLINE_JUDGE
    fclose(stdin), fclose(stdout);
#endif
    return 0;
}

数组版

#include <cstdio>
#include <cstdlib>

const int maxn = 100005;

int ch[maxn][2], size[maxn], r[maxn], v[maxn], sz, root;

int cmp(int o, int x)
{
    if(v[o] == x) return -1;
    return v[o] < x ? 0 : 1;
}
void maintain(int o)
{
    size[o] = size[ch[o][0]] + size[ch[o][1]] + 1;
}
void rotate(int &o, int d)
{
    int k = ch[o][d^1];
    ch[o][d^1] = ch[k][d], ch[k][d] = o;
    maintain(o), maintain(k), o = k;
}
void insert(int &o, int x)
{
    if(!o) {
        o= ++sz;
        v[o] = x, r[o] = rand();
    }
    else {
        int d = x < v[o] ? 0 : 1;
        insert(ch[o][d], x);
        if(r[ch[o][d]] > r[o]) rotate(o, d^1);
    }
    maintain(o);
}
int del(int &o, int x)
{
    if(!o) return 0;
    int res = 0;
    if(v[o] >= x) res = del(ch[o][0], x);
    else {
        res = size[ch[o][0]] + 1;
        o = ch[o][1];
        res += del(o, x);
    }
    if(o) maintain(o);
    return res;
}
int kth(int o, int k)
{
    if(!o || k <= 0 || size[o] < k) return 0;
    int s = size[ch[o][0]];
    if(k == s+1) return v[o];
    else if(k <= s) return kth(ch[o][0], k);
    else return kth(ch[o][1], k-s-1);
}

int main()
{
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif

    char o[5];
    int n, min, k, add = 0, ans = 0;
    scanf("%d%d", &n, &min);
    for(int i = 1; i <= n; ++i)
    {
        scanf("%s%d", o, &k);
        switch(o[0])
        {
            case 'I': if(k >= min) insert(root, k-add); break;
            case 'A': add += k; break;
            case 'S': add -= k, ans += del(root, min-add); break;
            case 'F': if(!root || k > size[root]) puts("-1");
                        else printf("%d\n", kth(root, size[root] - k + 1) + add); break;
        }
    }
    printf("%d\n", ans);

#ifndef ONLINE_JUDGE
    fclose(stdin), fclose(stdout);
#endif
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值