poj2828 Buy Tickets
阿新 • • 發佈:2018-03-03
spa ket -- esp stream std urn else cst
倒序處理,線段樹保存有多少空位
#include <iostream>
#include <cstdio>
using namespace std;
int n, ans[200005], uu[200005], vv[200005];
struct SGT{
int sum[800005];
void build(int o, int l, int r){
if(l==r) sum[o] = 1;
else{
int mid=(l+r)>>1;
int lson=o<<1;
int rson=lson|1;
if(l<=mid) build(lson, l, mid);
if(mid<r) build(rson, mid+1, r);
sum[o] = sum[lson] + sum[rson];
}
}
void update(int o, int l, int r, int x, int y){
if(l==r){
ans[l] = y;
sum[o] = 0;
}
else {
int mid=(l+r)>>1;
int lson=o<<1;
int rson=lson|1;
if(sum[lson]>x) update(lson, l, mid, x, y);
else update(rson, mid+1, r, x-sum[lson], y);
sum[o] = sum[lson] + sum[rson];
}
}
}sgt;
int main(){
while (scanf("%d", &n)!=EOF){
sgt.build(1, 1, n);
for(int i=1; i<=n; i++)
scanf("%d %d", &uu[i], &vv[i]);
for(int i=n; i; i--)
sgt.update(1, 1, n, uu[i], vv[i]);
for(int i=1; i<=n; i++)
printf("%d ", ans[i]);
printf("\n");
}
return 0;
}
poj2828 Buy Tickets