hust 1007 treap

  1. #include <iostream>
  2. #include <algorithm>
  3. using namespace std;
  4. struct treap
  5. {
  6.     int key, pri, cnt;
  7.     treap *lc, *rc;
  8.     treap(int x, int y);
  9. }NIL=treap(0,0), *nil=&NIL;
  10. treap::treap(int x=0, int y=0)
  11. {
  12.     key=x,pri=y,cnt=1;
  13.     lc=rc=nil;
  14. }
  15. typedef treap*node;
  16. node root;
  17. void right_rotate(node &y)
  18. {
  19.     node x = y->lc;
  20.     y->lc = x->rc;
  21.     x->rc = y;
  22.     y = x;
  23. }
  24. void left_rotate(node &x)
  25. {
  26.     node y = x->rc;
  27.     x->rc = y->lc;
  28.     y->lc = x;
  29.     x = y;
  30. }
  31. void insert(node &t, int x)
  32. {
  33.     if(t == nil)
  34.     {
  35.         t = new treap(x,rand()*rand());
  36.     }
  37.     else if(x == t->key) t->cnt++;
  38.     else if(x < t->key)
  39.     {
  40.         insert(t->lc, x);
  41.         if(t->lc->pri < t->pri) right_rotate(t);
  42.     }
  43.     else if(x > t->key)
  44.     {
  45.         insert(t->rc, x);
  46.         if(t->rc->pri < t->pri) left_rotate(t);
  47.     }
  48. }
  49. void del(node &t, int x)
  50. {
  51.     if(t == nil)return;
  52.     if(x < t->key) del(t->lc, x);
  53.     else if(x > t->key) del(t->rc, x);
  54.     else
  55.     {
  56.         if(t->cnt > 1) {t->cnt--;return;}
  57.         else if(t->lc==nil) t = t->rc;
  58.         else if(t->rc==nil) t = t->lc;
  59.         else
  60.         {
  61.             if(t->lc->pri < t->rc->pri)
  62.             {
  63.                 right_rotate(t);
  64.                 del(t->rc,x);
  65.             }
  66.             else
  67.             {
  68.                 left_rotate(t);
  69.                 del(t->lc,x);
  70.             }
  71.         }
  72.     }
  73. }
  74. int succ(node t, int x)
  75. {
  76.     if(t==nil)return -1;
  77.     if(x > t->key) return succ(t->rc, x);
  78.     else if(x == t->key) return x;
  79.     else
  80.     {
  81.         int tmp=succ(t->lc, x);
  82.         if(tmp==-1)return t->key;
  83.         else return tmp;
  84.     }
  85. }
  86. int main()
  87. {
  88.     int i, j, k, t;
  89.     int n, q, y;
  90.     scanf("%d", &t);
  91.     for(k = 1; k <= t; k++)
  92.     {
  93.         scanf("%d%d%d", &n, &q, &y);
  94.         root = nil;
  95.         for(i = 0; i < n; i++)
  96.         {
  97.             scanf("%d", &j);
  98.             insert(root, j);
  99.         }
  100.         printf("Case %d:/n", k);
  101.         for(i = 0; i < q; i++)
  102.         {
  103.             scanf("%d", &j);
  104.             int tmp = succ(root, j);
  105.             if(tmp == -1 || tmp-j>y) puts("-1");
  106.             else
  107.             {
  108.                 del(root, tmp);
  109.                 printf("%d/n", tmp);
  110.             }
  111.         }
  112.     }
  113. }

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值