Topic links
POJ2828 Topic
The train station has n (n≤\le200000) individuals to queue up, they arrive in order, they have to jump in line after arrival, now tell you that each person in line in the queue at the end of the current ranks of the number of people behind and their weights, the output of the final team of the weight sequence. Analysis
According to test instructions, everyone in the queue when the position is relative to that time of the team state, and therefore is dynamic change, but after analysis we can find that the last person's position is certain, so we can reverse consider everyone's queue, and then determine the position of each person. Give a chestnut: for example, now I consider the n-1 person's cut-in line, which will be inserted into the team when he arrives at the end of the first person, and when the nth person's position has been determined, equivalent to that position has been occupied, we only need to find in the remaining position x+1 to the n-1 person can, and so on.
So we can use the segment tree to maintain the number of remaining positions in the interval sum[], in the queue when the equivalent of a single point of update, the location of the point is equivalent to finding the K-Large element : When the remaining elements in the left dial hand interval is greater than or equal to K, the left sub-range is found in the K position, Otherwise, find the K-sum[rs] position in the right sub-range and update the weights after locating the position. Finally, traverse the line tree, then output the weight of the leaf node. Code
#include <iostream> #include <cstdio> #define LS (rt<<1) #define RS (rt<<1|1) using namespace std;
const int maxn=200010; struct Node {int p,v;}
node[maxn];//record everyone's jump queue information int n,sum[maxn<<2],val[maxn<<2],cnt;
Sum[] The number of remaining positions in the interval is maintained with the segment tree, val[] is the node weight void pushup (int rt) {Sum[rt]=sum[ls]+sum[rs];} void Build (int l,int r,int RT) {
if (l==r) {sum[rt]=1;//initially has an empty return;
} int mid= (L+R) >>1;
Build (L,MID,LS);
Build (MID+1,R,RS);
Pushup (RT);
} void Update (int p,int v,int l,int r,int RT)//Find the remainder of p in the interval [l,r] and update its weight to V {if (l==r) {if (sum[rt]==1)
{sum[rt]=0; Val[rt]=v;
Placeholder and update the weight} return;
} int mid= (L+R) >>1;
if (P<=sum[ls])//Find the idea of the K-large element Update (P,V,L,MID,LS);
else Update (P-SUM[LS],V,MID+1,R,RS);
Pushup (RT);
} void Print (int l,int r,int RT)//Traversal segment tree node, output leaf node weight {if (l==r) {cnt++; Printf ("%d", Val[rt]);
if (cnt<n) printf ("");
else printf ("\ n");
Return
} int mid= (L+R) >>1;
Print (L,MID,LS);
Print (MID+1,R,RS);
} int main () {int i; while (scanf ("%d", &n)!=eof) {for (i=1;i<=n;i++) {scanf ("%d%d", &node[i].p,&
; node[i].v);
node[i].p++;//is the first person to occupy the p+1 position} Build (1,n,1) after p.
for (i=n;i>=1;i--)//in descending queue {int p=node[i].p;
int v=node[i].v;
Update (p,v,1,n,1);
} cnt=0;
Print (1,n,1);
} return 0;
}
Note: When the input output is large, avoid using the input and output stream, easy tle.