A total of N people come to the queue one by one. Each person has a requirement to be placed behind the first few people (of course, it must be the requirements of the last person ), each person has a corresponding Val, which outputs the Val of n persons in order.
We use the line segment tree to maintain the number of remaining positions in the interval. Of course, we must update the line segment tree from the last person, after each update, set the remaining quantity of the location to 0 (because the requirements of people later must be met first)
The Code is as follows:
#include <set>#include <map>#include <cmath>#include <ctime>#include <queue>#include <stack>#include <cctype>#include <cstdio>#include <string>#include <vector>#include <cstdlib>#include <cstring>#include <iostream>#include <algorithm>using namespace std;typedef unsigned long long ull;typedef long long ll;const int inf = 0x3f3f3f3f;const double eps = 1e-8;const int maxn = 2e5+10;int seg[maxn<<2],ans[maxn],Pos[maxn],val[maxn],n;void build(int l,int r,int pos){ if (l == r) { seg[pos] = 1; return; } int mid = (l + r) >> 1; build(l,mid,pos<<1); build(mid+1,r,pos<<1|1); seg[pos] = seg[pos<<1] + seg[pos<<1|1];}int update(int l,int r,int pos,int x){ if (l == r) { seg[pos] = 0; return l; } int mid = (l + r) >> 1; int ans; if (seg[pos<<1] >= x) ans = update(l,mid,pos<<1,x); else ans = update(mid+1,r,pos<<1|1,x-seg[pos<<1]); seg[pos] = seg[pos<<1|1] + seg[pos<<1]; return ans;}int main(void){ #ifndef ONLINE_JUDGE freopen("in.txt","r",stdin); #endif while (~scanf ("%d",&n)){ for (int i = 0; i < n; i++) { scanf ("%d%d",Pos+i,val+i); } build(1,n,1); for (int i = n - 1; i >= 0; i--) { int tmp = update(1,n,1,Pos[i]+1); ans[tmp-1] = val[i]; } printf("%d",ans[0]); for (int i = 1; i < n; i++) { printf(" %d",ans[i]); } printf("\n");}return 0;}
Poj2828 -- buy tickets