http://poj.org/problem?id=2828
Test instructions: There are n queues, each person has a Val to correspond to, each Yimeimei will be inserted in the current team a position pos. Requires the final state of the team to be output.
Reverse thinking. In this case, the last person is sure to get the position he wants for the current team, and if we move forward, the penultimate person will be able to get the position he wants ...., that is, we can do this, we put the last person in, then we ignore it, and then we insert the penultimate one. That is, we find the current team where he wants to insert the real coordinates of the POS. Then go to update the length of the whole team. So loop until the last person. When the segment tree is updated at a single point, the sense and the binary lookup are very similar and can be implemented with it.
#include <stdio.h> #include <iostream> #include <string> #include <cstring> #include <cmath > #include <cstdlib> #include <algorithm> #include <queue> #include <stack> #include <set > #include <map> #include <vector> #define MID (A, B) (A + (B-A) >>1) #define LL (x) (x<<1) # Define RR (x) (x<<1|1) const int N = 200010;using namespace Std;int n;int pos[n];int val[n];int ans[n];struct node{int Left;int Right;int Sum;int Mid () {return mid (left, right);}}; struct Segtree{node tree[n * 4];void buildtree (int left,int right,int ind) {tree[ind].left = Left;tree[ind].right = Right;t Ree[ind].sum = Right-left + 1;if (left!=right) {int mid = Tree[ind].mid (); Buildtree (LEFT,MID,LL (Ind)); Buildtree (mid+1,r IGHT,RR (Ind));}} int query (int val,int pos) {int left = Tree[pos].left;int right = tree[pos].right;if (left = right) {tree[pos].sum = 0;retu RN left;} Else{int ind;if (Tree[ll (POS)].sum >= val) ind = Query (val, LL (POS)); elseind = queRy (Val-tree[ll (POS)].sum, RR (pos)) Tree[pos].sum = Tree[ll (POS)].sum + TREE[RR (POS)].sum;return ind;}} Seg;int Main () {while (scanf ("%d", &n)!=eof) {seg.buildtree (1,n,1); for (int i = 1; I <= n; i++) {scanf ("%d%d", &po S[i],&val[i]);} for (int i = n; I >=1; i--) {int ps = seg.query (pos[i]+1,1); Ans[ps] = Val[i];} for (int i = 1; I <= n; i++) {printf ("%d", ans[i]); if (i! = N) printf (""); printf ("\ n");} return 0;}
poj-2828 Buy Tickets