-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathSPOJ16016.cc
67 lines (61 loc) · 1.39 KB
/
SPOJ16016.cc
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
// SPOJ 16016. Queue (Pro)
// http://www.spoj.com/problems/QUE2/
//
// Solution: sqrt array
#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
#define fst first
#define snd second
#define all(c) ((c).begin()), ((c).end())
template <class T>
struct sqrt_array {
int n, m;
vector<vector<T>> x;
sqrt_array(int n) : n(n), m(sqrt(n)) {
x.assign(1+m, vector<T>(n/m));
}
pair<int,int> id(int k) {
for (int i = 0; k >= 0; k -= x[i++].size())
if (k < x[i].size()) return {i, k};
}
void erase(int k) {
--n;
auto p = id(k);
x[p.fst].erase(x[p.fst].begin() + p.snd);
if (x[p.fst].size() == 0) x.erase(x.begin() + p.fst);
}
T &operator[](int k) {
auto p = id(k);
return x[p.fst][p.snd];
}
const int size() const { return n; }
};
void doit() {
int n; scanf("%d", &n);
vector<pair<int, int>> H(n);
for (int i = 0; i < n; ++i)
scanf("%d", &H[i].fst);
for (int i = 0; i < n; ++i)
scanf("%d", &H[i].snd);
sort(all(H));
vector<int> ans(n);
sqrt_array<int> L(n);
for (int i = 0; i < n; ++i) L[i] = i;
for (auto h: H) {
int k = L[h.snd];
ans[k] = h.fst;
L.erase(h.snd);
}
for (int i = 0; i < n; ++i) {
if (i > 0) printf(" ");
printf("%d", ans[i]);
}
printf("\n");
}
int main() {
int ncase; scanf("%d", &ncase);
for (int icase = 0; icase < ncase ; ++icase) doit();
}