ZYB's Premutation HDU - 5592(权值线段树)

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)/~

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

starlet_kiss

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值