Question Analysis:
There are n queues for you. Each person has a location where they want to stand. You need to sort them from the past to the next and output the final result. Note that the people behind will overwrite the previous ones. That is, the person who was originally at this position moves a position.
Algorithm analysis:
We can regard the total number of people as the size of the range, and then the result is to put every position in the range on the top, that is, the answer.
From the questions, we can know that the people behind the questions are not influenced by the people above. So we can simulate the process.
How to simulate it? We can think of line tree (who thinks of it? I didn't think of it anyway). Based on the characteristics of the Line Segment tree, we can assign values to the values of each interval to the available positions of the interval. In this way, quick search can be achieved.
In fact, the key to this question is whether to assign the value of the range to the available number of positions in the range when the line segment tree is used. If you think of it, the line segment tree is very basic.
#include <iostream>#include <cstdio>#include <cstring>using namespace std;#define L(x) (x << 1)#define R(x) (x << 1|1)#define MOD(a,b) (a+((b-a) >> 1))#define lson lft,md,rt << 1#define rson md+1,rht,rt << 1|1const int MAXN = 200000 + 10;struct Node{ int lft,rht,val; int mid(){return MOD(lft,rht);}};Node tree[4*MAXN];int n,ran[MAXN],num[MAXN],res[MAXN];class SegTree{public: void Build(int lft,int rht,int rt){ tree[rt].lft = lft; tree[rt].rht = rht; tree[rt].val = rht - lft + 1; if(lft != rht){ int md = tree[rt].mid(); Build(lson); Build(rson); } } int Query(int val,int rt){ int lft = tree[rt].lft,rht = tree[rt].rht; if(lft == rht){ tree[rt].val = 0; return lft; } else{ int pos; if(val <= tree[L(rt)].val)pos = Query(val,L(rt)); else pos = Query(val-tree[L(rt)].val,R(rt)); tree[rt].val = tree[L(rt)].val + tree[R(rt)].val; return pos; } }};int main(){ while(~scanf("%d",&n)){ SegTree seg; seg.Build(0,n-1,1); for(int i = 0;i < n;++i){ scanf("%d%d",&ran[i],&num[i]); } int pos; for(int i = n-1;i >= 0;--i){ pos = seg.Query(ran[i]+1,1); res[pos] = num[i]; } for(int i = 0;i < n-1;++i) printf("%d ",res[i]); printf("%d\n",res[n-1]); } return 0;}