Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- const int maxn = 1e5 + 10;
- const int INF = 1e8;
- int a[maxn];
- int n;
- int segment_tree[3 * maxn];
- void build(int L, int R, int pos) {
- if(L == R) {
- segment_tree[pos] = 1;
- }
- else {
- int middle = (L + R) / 2;
- build(L, middle, 2 * pos);
- build(middle + 1, R, 2 * pos + 1);
- segment_tree[pos] = segment_tree[2 * pos] + segment_tree[2 * pos + 1];
- }
- }
- void update(int L, int R, int pos, int idx) {
- if(L == R) {
- segment_tree[pos] = 0;
- return;
- }
- int middle = (L + R) / 2;
- if(idx <= middle) {
- update(L, middle, 2 * pos, idx);
- }
- else {
- update(middle + 1, R, 2 * pos + 1, idx);
- }
- segment_tree[pos] = segment_tree[2 * pos] + segment_tree[2 * pos + 1];
- }
- int query(int L, int R, int pos, int x) {
- if(L == R) {
- return L;
- }
- int middle = (L + R) / 2;
- if(segment_tree[2 * pos] >= x) {
- return query(L, middle, 2 * pos, x);
- }
- else {
- return query(middle + 1, R, 2 * pos + 1, x - segment_tree[2 * pos]);
- }
- }
- int main()
- {
- ios_base::sync_with_stdio(false);
- cin >> n;
- for(int i = 0; i < n; i++) {
- cin >> a[i];
- }
- sort(a, a + n);
- build(0, n - 1, 1);
- vector<int> q(n);
- for(int i = 0; i < n; i++) {
- cin >> q[i];
- q[i]++;
- }
- vector<int> res(n);
- for(int i = n - 1; i >= 0; i--) {
- int idx = query(0, n - 1, 1, q[i]);
- res[i] = a[idx];
- update(0, n - 1, 1, idx);
- }
- for(int i = 0; i < n; i++) {
- cout << res[i] << " ";
- }
- return 0;
- }
- // 134 167
- // 0 1 0 0 3 2 6 7 4 6 9 4
- // 134 167 120 119 156 120 167 182 155 163 172 145
- // 1 1 1 1 0 1 1 1 1 1 0 1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement