-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy path1442BlackBox.cpp
More file actions
90 lines (86 loc) · 2.05 KB
/
1442BlackBox.cpp
File metadata and controls
90 lines (86 loc) · 2.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include<stdlib.h>
#include<queue>
#include<iostream>
#include<stdio.h>
#include<assert.h>
using namespace std;
struct Treap{
struct Node{
int key, priority;
Node *left, *right;
int size;
Node(int k,int p):key(k),priority(p),left(NULL),right(NULL),size(1){}
}* root;
Treap():root(NULL){}
void left_rotate(Node *&x){
Node* y = x->right;
x->right = y->left;
y->left = x;
update(x);
x = y;
}
int size(Node *root){
if(!root) return 0;
else return root->size;
}
void update(Node *root){
root->size = 1 + size(root->left) + size(root->right);
}
void right_rotate(Node *&y){
Node *x = y->left;
y->left = x->right;
x->right = y;
update(y);
y = x;
}
void insert(Node *&root, int key){
if(!root) {
root = new Node(key,rand());
return;
}
if(key<root->key){
insert(root->left,key);
if(root->priority>root->left->priority)
right_rotate(root);
}else{
insert(root->right,key);
if(root->priority>root->right->priority)
left_rotate(root);
}
update(root);
}
void insert(int key){
insert(root,key);
}
int select(Node *root,int k){
if(size(root->left)+1==k) return root->key;
else if(size(root->left)+1>k) return select(root->left,k);
else return select(root->right,k-size(root->left)-1);
}
int select(int k){
assert(size(root)>=k);
return select(root,k);
}
};
const int M = 30001;
const int N = 30001;
int A[M];
int cnt[M];
int n,m,u;
int main()
{
Treap treap;
int k = 0;
scanf("%d%d",&m,&n);
for(int i=1;i<=m;i++)
scanf("%d",A+i);
memset(cnt,0,sizeof(cnt));
for(int i=1;i<=n;i++){
scanf("%d",&u);
cnt[u] ++;
}
for(int i=1;i<=m;i++){
treap.insert(A[i]);
while(cnt[i]--) printf("%d\n",treap.select(++k));
}
}