Title Link: http://poj.org/problem?id=2828
Similar titles: http://www.cnblogs.com/lovychen/p/3674048.html
Test data:
In
4
0 77
1 51
1 33
2 69
4
0 20523
1 19243
1 3890
0 31492
Out
77 33 69 51
31492 20523 3890 19243
Problem analysis: Insert directly backwards and then follow the previous search for slots to insert:
"Puzzle":
In the segment tree node, save the number of slots in this section, and then reverse-insert the POS:
Example: 0 77
1 51
1 33
2 69
First fetch: 2 —— —— — 69---(requires 3 seats before inserting)
Then take: 1 —— — 33--69---(requires 2 seats before inserting)
Then take: 1 —— — 33--69--51-(requires 2 seats before inserting) there are only 1 seats in front of it, so insert the trailing space
Then take: 0 77-77--33--69--51-(requires 1 seats before inserting)
AC Code:
1#include <iostream>2#include <algorithm>3#include <cstdio>4#include <cstring>5#include <queue>6#include <string>7#include <cmath>8 using namespacestd;9 Const intN =200010;Ten intT,tot; One intPre[n],vis[n]; A structTT - { - intx, y; the }a[n]; - intLowbit (intx) - { - returnx& (-x); + } - voidInit () + { A for(intI=1; i<=t;i++) atVis[i] =lowbit (i); - } - intSumintx) - { - intAns =0; - while(x>0) in { -Ans = ans+Vis[x]; to× = Xlowbit (x); + } - returnans; the } * intUpdateintx) $ {Panax Notoginseng while(x<=T) - { theVIS[X] = vis[x]-1; +x = x +lowbit (x); A } the } + voidAddintXinty) - { $ intL=1, r=T,mid; $ while(l<R) - { - //printf ("L = =%d%d%d\n", l,r,mid); theMid = (l+r)/2; - if(Sum (mid) >=x) R =mid;Wuyi ElseL = mid+1; the } - //printf ("L =%d\n", l); Wu update (l); -PRE[L] =y; About } $ intMain () - { - intAA,BB; - while(~SCANF ("%d",&T)) A { + init (); the for(intI=1; i<=t;i++) - { $scanf"%d%d",&aa,&BB); thea[i].x = aa+1; theA[I].Y =BB; the } the for(intI=t;i>0; i--) - Add (a[i].x,a[i].y); in for(intI=1; i<=t;i++) the { the if(i==1) printf ("%d", Pre[i]); About Elseprintf"%d", Pre[i]); the } theprintf"\ n"); the } + return 0; -}
Poj Buy Tickets