Files
python/TangDou/AcWing/BIT/244_ErFen_2.cpp
HuangHai 1f397eca87 'commit'
2025-08-30 18:35:01 +08:00

57 lines
1.4 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <bits/stdc++.h>
using namespace std;
const int N = 100010;
int a[N];
int ans[N];
int n;
// 树状数组模板
typedef long long LL;
#define lowbit(x) (x & -x)
int c[N];
void add(int x, int d) {
for (int i = x; i < N; i += lowbit(i)) c[i] += d;
}
LL sum(int x) {
LL res = 0;
for (int i = x; i; i -= lowbit(i)) res += c[i];
return res;
}
/*
参考答案:
2
4
5
3
1
*/
int main() {
#ifndef ONLINE_JUDGE
freopen("244.in", "r", stdin);
#endif
scanf("%d", &n);
for (int i = 2; i <= n; i++) scanf("%d", &a[i]); // 读入每头奶牛前面有几头奶牛比自己矮
// 这里是区别!
// 利用树状数组,把1~n之间每个位置设置为1,方便后面用来求前缀和
// for (int i = 1; i <= n; i++) add(i, 1);
for (int i = n; i; i--) {
int l = 1, r = n;
while (l < r) { // 用二分找出前缀和恰好为a[i] + 1的数
// 这里是区别!
int mid = (l + r) >> 1; // mid枚举的是下标
if (mid - sum(mid) >= a[i] + 1)
r = mid; // 再小点试试
else
l = mid + 1; // 再大点~
}
ans[i] = l; // 记录答案
add(l, 1); // 找到后,这个数-1,标识为0,方便下次求前缀和
}
// 输出结果
for (int i = 1; i <= n; i++) printf("%d\n", ans[i]);
return 0;
}