1. 程式人生 > >2182 二分 + 樹狀陣列

2182 二分 + 樹狀陣列

Code:

#include<cstdio>
#include<stack>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn = 100000 + 233;
int n, C[maxn], height[maxn];
stack<int>S;
inline int lowbit(int t) { return t & (-t);}
inline void update(int x, int delta)
{
    while(x <=
n) C[x] += delta, x += lowbit(x); } inline int query(int x) { int sum = 0; while(x > 0) sum += C[x], x -=lowbit(x); return sum; } int main() { scanf("%d",&n); height[1] = 0; for(int i = 2;i <= n; ++i) scanf("%d",&height[i]); for(int i = 1;i <= n; ++i) update
(i, 1); for(int i = n;i >= 1; --i) { int l = 1, r = n, ans = 0; while(l <= r) { int mid = (l + r) >> 1; if(query(mid) >= height[i] + 1) ans = mid, r = mid - 1; else l = mid + 1; } update(ans, -1); S.
push(ans); } while(!S.empty()) {printf("%d\n",S.top()); S.pop();} return 0; }