Question:
Someone is waiting in the queue, and every time it is inserted behind the I-th person.
The final output order.
Train of Thought Analysis:
You will find that if reverse processing is performed, you will know the number of this person.
The problem is simplified at once.
It is just the number of null positions in the online segment tree.
#include <cstdio>#include <iostream>#include <cstring>#include <algorithm>#include <vector>#define lson num<<1,s,mid#define rson num<<1|1,mid+1,e#define maxn 200005using namespace std;struct foo{ int pos,val;}Q[maxn];int cnt[maxn<<2];int v[maxn];void update(int num,int s,int e,int k,int val){ if(s==e){ cnt[num]++; v[s]=val; return; } int mid=(s+e)>>1; int t=mid-s+1-cnt[num<<1]; if(t>k)update(lson,k,val); else update(rson,k-t,val); cnt[num]=cnt[num<<1]+cnt[num<<1|1];}int main(){ int n; while(scanf("%d",&n)!=EOF) { memset(cnt,0,sizeof cnt); for(int i=1;i<=n;i++){ scanf("%d%d",&Q[i].pos,&Q[i].val); } for(int i=n;i>=1;i--){ update(1,1,n,Q[i].pos,Q[i].val); } for(int i=1;i<=n;i++){ printf("%d%c",v[i],i==n?'\n':' '); } } return 0;}