-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathQUE2-nlogn.cpp
57 lines (56 loc) · 1.26 KB
/
QUE2-nlogn.cpp
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
/**
Rasul Kerimov (CoderINusE)
QUE2 - Queue (Pro)
O(n*logn)
*/
#include <bits/stdc++.h>
#define MAXN 10005
using namespace std;
typedef pair<int,int> p;
pair <int, int> a[MAXN + 2];
struct node {
int val, num;
node *left, *right;
node() {}
node(int val, int num) {
this->val = val;
this->num = num;
this->left = NULL;
this->right = NULL;
}
};
inline void insert(node *& n, p a) {
if(n == NULL) {
n = new node(1, a.first);
return;
}
if(a.second < n->val) {
n->val += 1;
insert(n->left, a);
}
else {
int tmp = max(0, a.second - n->val);
insert(n->right, {a.first, tmp});
}
}
void traverse(node* n) {
if(n == NULL) return;
traverse(n->left);
printf("%d ", n->num);
traverse(n->right);
}
int main() {
int tx, n; cin >> tx;
for(int i = 0; i < tx; ++i) {
scanf("%d", &n);
for(int i = 0; i < n; ++i) scanf("%d", &a[i].first);
for(int i = 0; i < n; ++i) scanf("%d", &a[i].second);
sort(a, a + n, ::greater<pair<int,int>>());
node* root = NULL;
//cout << root << endl;
for(int i = 0; i < n; ++i)
insert(root, a[i]);
traverse(root);
printf("\n");
}
}