POJ 2828 Buy Tickets (queue problem, Segment tree application)
ACM
Title Address: POJ 2828 Buy Tickets
Test Instructions :
Queue in line to buy tickets.
Give some number pairs that represent a person's desired insertion position pos_i and his val_i to find the final queue in Val order.
Analysis :
is also a very ingenious topic.
It was just beginning to be naïve to think sort of a bit. WA got a pitch and found out I was wrong ...
The original can be very ingenious with the line tree. Because someone wants to insert the posi position, he is in the posi position after insertion, but maybe the other person will be in front of him, his position will become [behind him and inserted in his position and the number of people]+posi.
Suppose this is the beginning of the request, of course, the line tree can be done, with the same number of reverse order.
But we can think of the opposite, only to start from behind, if the people are already standing in the right position, then to that person standing, now the position is already behind those people, just to count posi a space, that person standing position can be determined. You can ask for the next one, so this premise and conclusion are set up.
So we just have to stand up from behind, Count posi a space to stand up.
The line tree is the same as the sum line tree, initialized when all initialized to 1, and then find the time to "two" find, cleverly find the required location, detailed see code, although the code is very frustrated.
Code with the input and output plug to speed up, no addition can also be, please ignore.
Code :
/** Author:illuz <iilluzen[at]gmail.com>* blog:http://blog.csdn.net/hcbbt* file:2828.cpp* Create date:2014-08-05 20:16:28* Descripton: * * #include <iostream> #include <cstdio> #include <CSTRING&G t; #include <algorithm>using namespace std; #define REPF (I,A,B) for (int i= (a); i<= (b); i++) #define Lson (x) ((x) << 1) #define Rson (x) ((x) << 1 | 1) typedef LONG LONG ll;const int N = 200000;const int ROOT = 1;//below is SE ment 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) {//Erase and query if (l = = r) {NODE[POS].W = 0;return l;} int m = (L + R) >> 1;int res;if (x < Node[lson (POS)].W)//This binary finds 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 * + 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;}
POJ 2828 Buy Tickets (queue problem, Segment tree application)