Poj-2828 Buy Tickets
Question: There are n queues, each of which has a val, and each of the Later users inserts a position pos in the current team. Output the final status of the team.
Reverse Thinking. In this way, the last person will surely get the position he wants from the current team. If we move forward, the last person will surely get the position he wants ......, That is to say, we can do this. We insert the last person, ignore it, and insert the last person. That is, we can find the real coordinates of the position pos that the current team wants to insert. Then update the length of the entire team. This loop continues until the last person. When a line segment tree is updated at a single point, it feels very similar to binary search and can be used for implementation.
#include
#include
#include
#include
#include
#include
#include #include
#include
#include
#include
#include
#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;tree[ind].sum = right - left + 1;if (left !=right){int mid = tree[ind].mid();buildtree(left,mid,ll(ind));buildtree(mid+1,right,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;return 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,&pos[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();}return 0;}