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 <= 51,1 <= N <= 50000.


Output
For each testcase,print the ans.


Sample Input
1
3
0 1 2


Sample Output
3 1 2


----------------------------------------我是分割线^_^-------------------------------------------

又是一道线段树的题目,普通方法也可以做,只不过肯定超时了,因为用线段树进行优化之后都差点超时= =,
又是逆序数,题目意思是说给出逆序数对数的前缀和,然后让你求出原序列,查看题解后你会发现一个规律:
题目给出的Ai,可以通过与前一项相减获得第i个数在1到n的第几大蛇王位置,第i个数为A(i) - A(i-1) + 1
大,括号中的数为下标,然后通过线段树找得到这个数在剩下的为筛选出去的数中的第几大的位置。
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<string>
#include<queue>
#include<vector>
#include<cctype>
#include<set>
#include<map>
#include<sstream>
using namespace std; #define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define INF 0x3f3f3f3f
#define Int __int64
#define pii pair<int,int> const int MAXN = 55555;
int sum[MAXN<<2];
int num[MAXN];
int ans[MAXN]; void PushUp(int rt) {
sum[rt] = sum[rt<<1] + sum[rt<<1|1];
}
void Build(int l, int r, int rt) {
if (l == r) {
sum[rt] = 1;
return ;
}
int m = (l + r)>>1;
Build(lson);
Build(rson);
PushUp(rt);
}
void UpDate(int pos, int l, int r, int rt) {
if (l == r) {
sum[rt] = 0;
return ;
}
int m = (l + r)>>1;
if (pos <= m) UpDate(pos, lson);
else UpDate(pos, rson);
PushUp(rt);
}
int Query(int value, int l, int r, int rt) {
if (l == r) {
return r;
}
int ret;
int m = (l + r)>>1;
if (value - sum[rt<<1|1] > 0) ret = Query(value - sum[rt<<1|1], lson);/*注意理解这里,
  如果第几大的位置超过右边的范围了,就去左边找*/
else ret = Query(value, rson);
return ret;
}
int main() {
//freopen("input.txt", "r", stdin);
int T;
while (cin>>T) {
while (T--) {
int n;
cin>>n;
memset(sum, 0, sizeof(sum));
Build(1, n, 1);
for (int i = 0; i < n; i++) {
cin>>num[i];
}
for (int i = n - 1; i >= 1; i--) {
int t = num[i] - num[i - 1] + 1;
ans[i] = Query(t, 1, n, 1);
UpDate(ans[i], 1, n, 1);
}
ans[0] = Query(1, 1, n, 1);
for (int i = 0; i < n; i++) {
cout<<ans[i];
if (i != n - 1) cout<<" ";
else cout<<endl;
}
}
}
return 0;
}

线段树 - ZYB's Premutation的更多相关文章

  1. Bestcoder round #65 && hdu 5592 ZYB's Premutation 线段树

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submissio ...

  2. ZYB's Premutation(有逆序数输出原序列,线段树)

    ZYB's Premutation Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Othe ...

  3. hdu 5592 ZYB's Premutation (权值线段树)

    最近在线段树的世界里遨游,什么都能用线段树做,这不又一道权值线段树了么. ZYB's Premutation Time Limit: 2000/1000 MS (Java/Others)    Mem ...

  4. HDU 5592——ZYB's Premutation——————【线段树单点更新、单点查询】

    ZYB's Premutation Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Othe ...

  5. BC 65 ZYB's Premutation (线段树+二分搜索)

    题目简述:有一个全排列,一直每个前缀区间的逆序对数,还原这个排列. fi记录逆序对数,pi记录该位置数值,则k=fi-f(i-1)表示前i-1个数比pi大的数的个数,那么只要在剩余元素求出按大小顺序第 ...

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

    题意:给出序列前k项中的逆序对数,构造出这个序列. 分析:使用权值线段树来确定序列元素. 逆序对的数量肯定是递增的,从最后一个元素开始逆向统计,则\(a[i] - a[i-1]\)即位置i之前比位置i ...

  7. hdu 5592 ZYB's Premutation(线段树优化)

    设f_if​i​​是第ii个前缀的逆序对数,p_ip​i​​是第ii个位置上的数,则f_i-f_{i-1}f​i​​−f​i−1​​是ii前面比p_ip​i​​大的数的个数.我们考虑倒着做,当我们处理 ...

  8. [HDU5592] ZYB's Premutation

    [HDU5592] ZYB's Premutation 题目大意:一个由\([1,n]\)组成的数列,但不知道具体排列,但给出每个前缀的逆序对数目,让你还原排列 Solution 创造一颗\([1,n ...

  9. BestCoder Round #65 (ZYB's Premutation)

    ZYB's Premutation Accepts: 220 Submissions: 983 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: ...

随机推荐

  1. PHP 验证码生成类(可定制长度和内容)

    ===================VerifyTool====================== <?php class VerifyTool { private $fontPath; / ...

  2. 3. Longest Substring Without Repeating Characters(c++) 15ms

    Given a string, find the length of the longest substring without repeating characters. Examples: Giv ...

  3. CSS标签

    CSS类选择器被大量使用了 class = cssname

  4. 《Caffe下跑AlxNet之数据处理过程》

    环境:Windows 最近用Caffe跑了一下AlxNet网络,现在总结一下数据处理部分:(处理过的数据打包链接:http://pan.baidu.com/s/1sl8M5ad   密码:ph1y) ...

  5. opengl es中不同的绘制方式

    opengl es中不同的绘制方式 转载请保留出处:http://xiaxveliang.blog.163.com/blog/static/297080342013467344263/ 1. GL_P ...

  6. Centos7 wifi

    centos7如果在安装系统选择安装软件的选项是gnome套件(要注意退出选择界面回到安装界面时软件选项显示的是gnome,仅仅选择了gnome的软件也不行),安装完成后就会有wifi的图标,下面的方 ...

  7. 【原创】node+express+socket搭建一个实时推送应用

    技术背景 Web领域的实时推送技术,也被称作Realtime技术.这种技术要达到的目的是让用户不需要刷新浏览器就可以获得实时更新. 应用场景: 监控系统:后台硬件热插拔.LED.温度.电压发生变化 即 ...

  8. 安装Java的IDE Eclipse时出现java.net.SocketException,出现错误Installer failed,show.log

    ERROR: org.eclipse.equinox.p2.transport.ecf code=1002 Unable to read repository at http://download.e ...

  9. 模拟搭建Web项目的真实运行环境(四)

    本篇介绍如何部署mongodb环境,主要分为三个部分: 第一部分 介绍如何在ubuntu下安装mongodb, 第二部分 介绍如何在windows下安装使用MongoChef客户端, 第三部分 介绍在 ...

  10. java回调机制

    http://www.cnblogs.com/heshuchao/p/5376298.html