ZYB ' s premutation
Time limit:2000/1000 MS (java/others) Memory limit:131072/131072 K (java/others) total submission (s): 758 Accepted Submission (s): 359
Problem DescriptionZYB Has a premutationP, but he is remeber the reverse log of each prefix of the premutation,now he ask youRestore the premutation.
Pair ( Span id= "mathjax-span-12" class= "Mi" >i j) (i< j) is considered as a reverse log if ai>aJ is matched .
Inputin The first line there is the number of testcases T.
For each teatcase:
In the first line there is one number N.
In the next line there isNNumbersAi , describe the number of the reverse logs of each prefix,
The input is correct.
1≤T≤5,1≤N≤50000
Outputfor each testcase,print the ans.
Sample Input130 1 2
Sample OUTPUT3 1 2
Sourcebestcoder Round #65
Test instructions
Known "1,i" in reverse order number, let you restore this interval;
Idea: Use an array to record the number of numbers before the number of a[i] larger than the first.
The number of vacancies in the current interval is recorded by the line-segment tree; similar to buying a ticket in the queue, and then starting from the last number, into the line segment tree, A[i] The larger the number is smaller, the left tree inserted, inserted in the corresponding position of the position was used, that is 0;
Why do you want to plug in from the back, because if a group of numbers 1,2,3,4 from the trip, then the result will be 4,3,2,1; that's wrong; inverted into is 1,2,3,4;
Code:
#include <iostream> #include <cstring> #include <cstdio> #include <cmath> #include < algorithm> #include <queue> #include <vector>using namespace std;const int inf=0x3f3f3f3f; #define SI (x) scanf ("%d", &x) #define PI (x) printf ("%d", x) #define P_ printf ("") #define T_T while (t--) #define MEM (x, y) memset (x, Y, sizeof (x)) #define LL Root<<1#define RR root<<1|1#define Lson root<<1,l,mid#define Rson root<<1 |1,mid+1,r#define V (x) tree[x]const int maxn=50010;int tree[maxn<<2],a[maxn],ans[maxn];int nowans;void pushup ( int root) {V (root) =v (ll) +v (RR);} void made (int root,int l,int r) {V (root) =r-l+1;int mid= (l+r) >>1;if (l==r) Return;made (Lson); made (Rson);} void query (int root,int l,int R,int v) {if (l==r) {nowans=l; V (root) =0;return;} int mid= (L+R) >>1;if (V>=v (RR)) query (LSON,V-V (RR)), else query (rson,v);p ushup (root);} int main () {int t,n;si (T); T_t{si (N); made (1,1,n); int cur,last=0;for (int i=0;i<n;i++) {scanf ("%d", &cur); a[i]=cur-last;last=cur;} for (int i=n-1;i>=0;i--) {query (1,1,n,a[i]); Ans[i]=nowans;} for (int i=0;i<n;i++) {if (i) p_;printf ("%d", Ans[i]);} Puts ("");} return 0;}
ZYB ' s premutation (with reverse number output original sequence, line segment tree)