【POJ1442】【Treap】Black Box

Description

Our Black Box represents a primitive database. It can save an integer array and has a special i variable. At the initial moment Black Box is empty and i equals 0. This Black Box processes a sequence of commands (transactions). There are two types of transactions: 

ADD (x): put element x into Black Box; 
GET: increase i by 1 and give an i-minimum out of all integers containing in the Black Box. Keep in mind that i-minimum is a number located at i-th place after Black Box elements sorting by non- descending. 

Let us examine a possible sequence of 11 transactions: 

Example 1 

N Transaction i Black Box contents after transaction Answer
(elements are arranged by non-descending)
1 ADD(3) 0 3
2 GET 1 3 3
3 ADD(1) 1 1, 3
4 GET 2 1, 3 3
5 ADD(-4) 2 -4, 1, 3
6 ADD(2) 2 -4, 1, 2, 3
7 ADD(8) 2 -4, 1, 2, 3, 8
8 ADD(-1000) 2 -1000, -4, 1, 2, 3, 8
9 GET 3 -1000, -4, 1, 2, 3, 8 1
10 GET 4 -1000, -4, 1, 2, 3, 8 2
11 ADD(2) 4 -1000, -4, 1, 2, 2, 3, 8

It is required to work out an efficient algorithm which treats a given sequence of transactions. The maximum number of ADD and GET transactions: 30000 of each type. 


Let us describe the sequence of transactions by two integer arrays: 


1. A(1), A(2), ..., A(M): a sequence of elements which are being included into Black Box. A values are integers not exceeding 2 000 000 000 by their absolute value, M <= 30000. For the Example we have A=(3, 1, -4, 2, 8, -1000, 2). 

2. u(1), u(2), ..., u(N): a sequence setting a number of elements which are being included into Black Box at the moment of first, second, ... and N-transaction GET. For the Example we have u=(1, 2, 6, 6). 

The Black Box algorithm supposes that natural number sequence u(1), u(2), ..., u(N) is sorted in non-descending order, N <= M and for each p (1 <= p <= N) an inequality p <= u(p) <= M is valid. It follows from the fact that for the p-element of our u sequence we perform a GET transaction giving p-minimum number from our A(1), A(2), ..., A(u(p)) sequence. 


Input

Input contains (in given order): M, N, A(1), A(2), ..., A(M), u(1), u(2), ..., u(N). All numbers are divided by spaces and (or) carriage return characters.

Output

Write to the output Black Box answers sequence for a given sequence of transactions, one number each line.

Sample Input

7 4
3 1 -4 2 8 -1000 2
1 2 6 6

Sample Output

3
3
1
2

Source

【分析】

练下手而已,没什么。

  1 #include <iostream>
  2 #include <cstdio>
  3 #include <algorithm>
  4 #include <cstring>
  5 #include <vector>
  6 #include <utility>
  7 #include <iomanip>
  8 #include <string>
  9 #include <cmath>
 10 #include <queue>
 11 #include <assert.h>
 12 #include <map>
 13 
 14 const int N = 30000 + 10;
 15 const int SIZE = 250;//块状链表的大小 
 16 const int M = 50000 + 5;
 17 using namespace std;
 18 struct TREAP{
 19        struct Node{
 20               int fix, size;
 21               int val;
 22               Node *ch[2];
 23        }mem[30000 + 10], *root;
 24        int tot;
 25        //大随机 
 26        int BIG_RAND(){return (rand() * RAND_MAX + rand());}
 27        Node *NEW(){
 28             Node *p = &mem[tot++];
 29             p->fix = BIG_RAND();
 30             p->val = 0;
 31             p->size = 1;
 32             p->ch[0] = p->ch[1] = NULL;
 33             return p;
 34        }
 35        //将t的d节点换到t 
 36        void rotate(Node *&t, int d){
 37             Node *p = t->ch[d];
 38             t->ch[d] = p->ch[d ^ 1];
 39             p->ch[d ^ 1] = t;
 40             t->size = 1;
 41             if (t->ch[0] != NULL) t->size += t->ch[0]->size;
 42             if (t->ch[1] != NULL) t->size += t->ch[1]->size; 
 43             t = p;
 44             t->size = 1;
 45             if (t->ch[0] != NULL) t->size += t->ch[0]->size;
 46             if (t->ch[1] != NULL) t->size += t->ch[1]->size; 
 47             return; 
 48        }
 49        void insert(Node *&t, int val){
 50             //插入 
 51             if (t == NULL){
 52                t = NEW();
 53                t->val = val;
 54                return; 
 55             }
 56             //大的在右边,小的在左边 
 57             int dir = (val >= t->val);
 58             insert(t->ch[dir], val);
 59             //维护最大堆的性质 
 60             if (t->ch[dir]->fix > t->fix) rotate(t, dir);
 61             t->size = 1;
 62             if (t->ch[0] != NULL) t->size += t->ch[0]->size;
 63             if (t->ch[1] != NULL) t->size += t->ch[1]->size; 
 64        }
 65        //在t的子树中找到第k小的值 
 66        int find(Node *t, int k){
 67            if (t->size == 1) return t->val;
 68            int l = 0;//t的左子树中有多少值 
 69            if (t->ch[0] != NULL) l += t->ch[0]->size;
 70            if (k == (l + 1)) return t->val;
 71            if (k <= l) return find(t->ch[0], k);
 72            else return find(t->ch[1], k - (l + 1));
 73        }
 74 }treap; 
 75 typedef long long ll;
 76 int have[N];//have为1则在这个地方GET 
 77 int data[N], m, n;
 78 
 79 void init(){
 80      treap.root = NULL;
 81      treap.tot = 0; 
 82      memset(have, 0, sizeof(have));
 83      scanf("%d%d", &m, &n);
 84      for (int i = 1; i <= m; i++) scanf("%d", &data[i]);
 85      for (int i = 1; i <= n; i++){
 86          int x;
 87          scanf("%d", &x);
 88          have[x]++;
 89      }
 90 }
 91 void work(){
 92      int pos = 0;//代表要获得的位置 
 93      for (int i = 1; i <= m; i++){
 94          treap.insert(treap.root, data[i]); 
 95          //printf("%d", treap.root->size);
 96          while (have[i]){
 97             pos++;
 98             printf("%d\n", treap.find(treap.root, pos));
 99             have[i]--;
100          }
101      }
102     
103 }
104 
105 int main(){
106     int T;
107     #ifdef LOCAL
108     freopen("data.txt", "r", stdin);
109     freopen("out.txt", "w", stdout);
110     #endif
111     init();
112     work();
113     return 0;
114 }
View Code

 

posted @ 2015-03-10 09:20  TCtower  阅读(197)  评论(0编辑  收藏  举报