POJ 2827 Buy Tickets (queuing problem, line segment tree Application)
POJ 2827 Buy Tickets (queuing problem, line segment tree Application)
ACM
Address: POJ 2827 Buy Tickets
Question:
Line up when buying tickets.
A number pair is given to represent the position Pos_ I and Val_ I of a person respectively, and the val sequence of the final queue is obtained.
Analysis:
It is also a clever question.
At first, I thought it would be nice to use sort. After wa's run, I found that I was wrong...
Originally, we could use a line segment tree. Because a person wants to insert a posi position, he will insert it in front of the posi position, but other people may insert it in front of him, his position will become [behind him and inserted in his position and the previous number of people] + posi.
If this is the case, you can use the line segment tree. This is the same as the reverse order number pair.
However, we can think about it. As long as we start to stand behind and assume that all the people behind us are already in the correct position, when we get there, now there are all the people behind it. As long as there are spaces in posi, the position of the person's station can be determined. After confirmation, you can find the next one. Therefore, both the premise and conclusion are true.
So we only need to stand up from the people behind and count posi spaces.
The line segment tree is the same as the summation line segment tree. It is initialized to 1 during initialization, and can be searched in binary mode to find the desired position. For details, see the code, although the Code is frustrated.
The Code uses an input/output plug-in to increase the speed. You can skip this step without adding a plug-in.
Code:
/** Author: illuz
* Blog: http://blog.csdn.net/hcbbt* File: 2828. cpp * Create Date: 20:16:28 * Descripton: */# include
# Include
# Include
# Include using namespace std; # define repf (I, a, B) for (int I = (a); I <= (B); I ++) # define lson (x) <1) # define rson (x) <1 | 1) typedef long ll; const int N = 200000; const int ROOT = 1; // below is sement point updated versionstruct seg {ll w ;}; struct segment_tree {seg node [N <2]; void update (int pos) {node [pos]. w = node [lson (pos)]. w + node [rson (pos)]. w;} void build (int l, int r, int pos) {if (L = r) {node [pos]. w = 1; return;} int m = (l + r)> 1; build (l, m, lson (pos); build (m + 1, r, rson (pos); update (pos);} int remove (int l, int r, int pos, ll x) {// Delete and query if (l = r) {node [pos]. w = 0; return l;} int m = (l + r)> 1; int res; if (x <node [lson (pos)]. w) // search for res = remove (l, m, lson (pos), x); elseres = remove (m + 1, r, rson (pos ), x-node [lson (pos)]. w); update (pos); return res ;}} sgm; int Scan () {int res = 0, ch, flag = 0; if (ch = getchar () = '-') flag = 1; else if (ch> = '0' & ch <= '9') res = ch-'0'; while (ch = getchar ()> = '0' & ch <= '9') res = res * 10 + ch-'0'; return flag? -Res: res;} void Out (int a) {if (a> 9) Out (a/10); putchar (a % 10 + '0 ');} int a [2] [N], n; int ans [N]; int main () {while (~ Scanf ("% d", & n) {repf (I, 0, n-1) {a [0] [I] = Scan (); a [1] [I] = Scan ();} sgm. build (1, n, ROOT); for (int I = n-1; I> = 0; I --) {ans [sgm. remove (1, n, ROOT, a [0] [I])] = a [1] [I];} repf (I, 1, n) {if (I! = 1) printf (""); Out (ans [I]);} printf ("\ n");} return 0 ;}