逆向思维。从最后一位开始考虑,用线段树查询空位置。
#include <iostream> #include <cstring> #include <cstdlib> #include <cstdio> #include <algorithm> #include <vector> #include <queue> #include <stack> #include <set> #include <map> #include <cmath> #define LL long long #define lson l , m , rt<<1 #define rson m+1 , r , rt<<1|1 using namespace std; const int maxn = 222222; int sum[maxn<<2]; void pushup(int rt) { sum[rt] = sum[rt<<1] + sum[rt<<1|1]; } void build(int l,int r,int rt) { sum[rt] = r - l + 1; if(l == r) return ; int m = (l + r) >> 1; build(lson); build(rson); } int query(int p,int l,int r,int rt) { if(l == r) { sum[rt] = 0; return l; } int m = (l + r) >> 1; int pos; if(sum[rt<<1] >= p) pos = query(p,lson); else pos = query(p - sum[rt<<1] ,rson); pushup(rt); return pos; } int ans[maxn]; int x[maxn],y[maxn]; int main() { int N; while(~scanf("%d",&N)) { build(1,N,1); for(int i=1;i<=N;i++) scanf("%d%d",&x[i],&y[i]); for(int i=N;i>=1;i--) { int pos = query(x[i]+1,1,N,1); ans[pos] = y[i]; } for(int i=1;i<N;i++) cout<<ans[i]<<' '; cout<<ans[N]<<endl; } return 0; }
POJ 2828 Buy Tickets (线段树,区间修改),布布扣,bubuko.com
POJ 2828 Buy Tickets (线段树,区间修改)
原文地址:http://blog.csdn.net/moguxiaozhe/article/details/38293493