-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
executable file
·60 lines (47 loc) · 1.13 KB
/
main.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
58
59
60
#include <bits/stdc++.h>
#define MAX 100001
using namespace std;
int N, t;
int arr[MAX], arrIndex[MAX], tree[4 * MAX];
int init(int start, int end, int n) {
if (start == end) {
arrIndex[start] = n;
return tree[n] = 1;
}
int mid = (start + end) / 2;
return tree[n] = init(start, mid, 2 * n) + init(mid + 1, end, 2 * n + 1);
}
void update(int index, int value) {
int cur = arrIndex[index];
tree[cur] = value;
while (cur != 1) {
tree[cur / 2] = tree[cur] + tree[(cur % 2 == 0) ? (cur + 1) : (cur - 1)];
cur /= 2;
}
}
int query(int start, int end, int n, int sum) {
if (start == end)
return start;
int mid = (start + end) / 2;
int ret = 0;
if (sum <= tree[2 * n])
ret = query(start, mid, 2 * n, sum);
else
ret = query(mid + 1, end, 2 * n + 1, sum - tree[2 * n]);
return ret;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> N;
fill(tree, tree + 4 * N, 1);
init(0, N - 1, 1);
for (int i = 0; i < N; i++) {
cin >> t;
int tmp = query(0, N - 1, 1, t + 1);
update(tmp, 0);
arr[tmp] = i;
}
for (int i = 0; i < N; i++)
cout << arr[i] + 1 << "\n";
}