ZYB has a premutation P,but he only remeber the reverse log of each prefix of the premutation,now he ask you to
restore the premutation.
Pair (i,j)(i<j) is considered as a reverse log if Ai>Aj is matched.
Input
In 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 are N numbers Ai,describe the number of the reverse logs of each prefix,
The input is correct.
1≤T≤5,1≤N≤50000
Output
For each testcase,print the ans.
Sample Input
1
3
0 1 2
Sample Output
3 1 2
给你一组序列的逆序数前缀和,让你去还原原来的序列。。
这里逆序数统计的是前面大于自己的数字个数。我们可以通过做差的方式a[i]-a[i-1]求出每一个数的逆序数个数。对于第i个数来说,假如逆序数是x,那么他就是前i中第x大(更新之后的第x大)。那么我们从后往前找,每次找区间内的第x大,求出来之后我们必须将那个节点的值更新为0。酱紫就能构造出来了。
代码如下:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
#define ll long long
using namespace std;
const int maxx=5e4+100;
struct node{
int l;
int r;
int v;
}p[maxx<<2];
int a[maxx],b[maxx];
int n;
void pushup(int cur)
{
p[cur].v=p[cur<<1].v+p[cur<<1|1].v;
}
void build(int l,int r,int cur)
{
p[cur].l=l;
p[cur].r=r;
p[cur].v=1;
if(l==r) return ;
int mid=l+r>>1;
build(l,mid,cur<<1);
build(mid+1,r,cur<<1|1);
pushup(cur);
}
void update(int pos,int x,int cur)
{
int L=p[cur].l;
int R=p[cur].r;
if(L==R)
{
p[cur].v=0;
return ;
}
int mid=L+R>>1;
if(pos<=mid) update(pos,x,cur<<1);
else update(pos,x,cur<<1|1);
pushup(cur);
}
int query(int l,int r,int k,int cur)
{
int L=p[cur].l;
int R=p[cur].r;
if(L==R) return L;
int mid=L+R>>1;
if(k<=p[cur<<1].v) return query(l,r,k,cur<<1);
else return query(l,r,k-p[cur<<1].v,cur<<1|1);
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
for(int i=1;i<=n;i++) scanf("%d",&a[i]);
build(1,n,1);
for(int i=n;i>=1;i--)
{
b[i]=query(1,i,i-a[i]+a[i-1],1);//权值线段树求出那个值来
update(b[i],0,1);//在权值线段树中将这个值更新为0,代表已经用过了。
}
for(int i=1;i<=n;i++) printf("%d%s",b[i],i==n?"\n":" ");
}
return 0;
}
努力加油a啊,(o)/~