首先将树状数组中的数都置为1,代表还没有数被选
然后从后往前遍历,去找处在a[i] + 1位置上且没被选的牛是哪一个,过程用二分维护即可
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> PII;
const int N = 100010;
int tr[N];
int n;
int a[N], ans[N], idx;
int lowbit(int x)
{
return x & -x;
}
void add(int x, int k)
{
for (int i = x; i <= n; i += lowbit(i))
tr[i] += k;
}
int sum(int x)
{
LL res = 0;
for (int i = x; i; i -= lowbit(i))
res += tr[i];
return res;
}
int main()
{
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
cin >> n;
for (int i = 2; i <= n; i ++) cin >> a[i];
for (int i = 1; i <= n; i ++) add(i, 1);
for (int i = n; i >= 1; i --)
{
int y = a[i] + 1;
int l = 1, r = n;
while (l < r)
{
int mid = l + r >> 1;
if (sum(mid) >= y) r = mid;
else l = mid + 1;
}
add(l, -1);
ans[++ idx] = l;
}
for (int i = n; i >= 1; i --) cout << ans[i] << endl;
return 0;
}