The last few days did not do a line tree, busy digestion of new algorithms and competition topics.
The game came out of the question, so I did the problem by the way.
The general meaning of the topic:
Now there are n people, and then give each person's location pos[i] and Val[i], and finally let you output where they are.
For example, a sample:
40 771 511) 332 69
0 represents a person with a current value of 77 standing behind a 0th person (the No. 0 person is the ticket sales office).
In the same vein, 1 represents a person with a current value of 51 standing behind a 1th person.
But the third group of samples will be noticed, the update here, that is, 33 of the value of people will be standing behind the 1th people, so the second person will be squeezed out.
The same is true of the fourth person.
So the final output is.
The reason for using the line segment tree here is:
We want to quickly find the location of everyone, so it is convenient to use line tree to locate. Moreover, there is a trick here is to look forward from the back, so that the words will ensure that the current coverage must be the latest.
The process of querying is very clever, please refer to the code for details:
#include <stdio.h> #include <string.h> #include <iostream> #include <algorithm>using namespace STD; #define MAXN 222222int ans[maxn],pos[maxn],val[maxn];struct node{int l,r;int sum;} tree[maxn*4];void pushup (int v) {int temp=v<<1;tree[v].sum=tree[temp].sum+tree[temp+1].sum;} void build (int l,int R,int v) {tree[v].l=l;tree[v].r=r;if (l==r) {Tree[v].sum=1;return;} int mid= (L+R) >>1;int temp=v<<1;build (l,mid,temp); build (mid+1,r,temp+1);p ushup (v);} void query (int t,int v,int cnt) {if (TREE[V].L==TREE[V].R) {//This is the beginning of the wrong, written tree[v].l==t, actually here is the most front of the child node is good ans[tree[v] l]=cnt;tree[v].sum--;//Note that each time a child node is found, the current point's sum is reduced by 1. int temp=v<<1;if (t<=tree[temp].sum) query (t,temp,cnt); else query (t-tree[temp].sum,temp+1,cnt);//Query here (t-tree[temp].sum,temp+1,cnt), do not forget to lose the sum value of the left node pushup (v);} int main () {int n;while (~scanf ("%d", &n)) {memset (ans,0,sizeof (ans)); Memset (Pos,0,sizeof (POS)); memset (val,0, sizeof (Val)); for (int i=1;i<=n;i++) {scanf ("%d%d", &pOs[i],&val[i]);} Build (1,n,1); for (int i=n;i>=1;i--) {query (Pos[i]+1,1,val[i]);} for (int i=1;i<=n;i++) {if (i!=n) printf ("%d", ans[i]), Else printf ("%d\n", Ans[i]);}}}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
POJ (2828)--buy tickets (segment tree maintenance point & Point Update)