ZYB's Premutation

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 637    Accepted Submission(s): 301

Problem Description
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
 
Source
 
题目大意:给你一个长度为n的序列。给出前缀的逆序,问你原始序列是什么。
 
解题思路:首先处理出来每个数的逆序。然后从后处理,如果逆序为2,那么这个数是当前所有数中的第3大的数。那么在线段树中查找第3个为1的位置。   这是个偏想法的比较有意思的题。
 
#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<vector>
using namespace std;
#define mid (L+R)/2
#define lson rt*2,L,mid
#define rson rt*2+1,mid+1,R
const int maxn = 55000;
struct SegTree{
int sum;
}segs[maxn*4];
int a[maxn],ans[maxn];
void PushUp(int rt){
segs[rt].sum = segs[rt*2].sum + segs[rt*2+1].sum;
}
void buildtree(int rt,int L,int R){
if(L == R){
segs[rt].sum = 1;
return ;
}
buildtree(lson);
buildtree(rson);
PushUp(rt);
}
int query(int rt,int L,int R,int k){
if(L == R){
segs[rt].sum = 0;
return L;
}
int ret ;
if(segs[rt*2+1].sum >= k){
ret = query(rson,k);
}else{
ret = query(lson,k-segs[rt*2+1].sum);
}
PushUp(rt);
return ret;
}
int main(){
int T,n;
scanf("%d",&T);
while(T--){
scanf("%d",&n);
buildtree(1,1,n);
for(int i = 1; i <= n; i++){
scanf("%d",&a[i]);
}
for(int i = n; i >= 1; i--){
ans[i] = query(1,1,n,a[i]-a[i-1]+1);
}
printf("%d",ans[1]);
for(int i = 2; i <= n; i++){
printf(" %d",ans[i]);
}puts("");
}
return 0;
}

  

HDU 5592——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. 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​​大的数的个数.我们考虑倒着做,当我们处理 ...

  3. HDU.1556 Color the ball (线段树 区间更新 单点查询)

    HDU.1556 Color the ball (线段树 区间更新 单点查询) 题意分析 注意一下pushdown 和 pushup 模板类的题还真不能自己套啊,手写一遍才行 代码总览 #includ ...

  4. hdu 1166 敌兵布阵 线段树 点更新

    // hdu 1166 敌兵布阵 线段树 点更新 // // 这道题裸的线段树的点更新,直接写就能够了 // // 一直以来想要进线段树的坑,结果一直没有跳进去,今天算是跳进去吧, // 尽管十分简单 ...

  5. 【POJ 2777】 Count Color(线段树区间更新与查询)

    [POJ 2777] Count Color(线段树区间更新与查询) Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4094 ...

  6. HDU 1754 I Hate It(线段树区间查询,单点更新)

    描述 很多学校流行一种比较的习惯.老师们很喜欢询问,从某某到某某当中,分数最高的是多少. 这让很多学生很反感.不管你喜不喜欢,现在需要你做的是,就是按照老师的要求,写一个程序,模拟老师的询问.当然,老 ...

  7. hdu 1116 敌兵布阵 线段树 区间求和 单点更新

    线段树的基本知识可以先google一下,不是很难理解 线段树功能:update:单点增减 query:区间求和 #include <bits/stdc++.h> #define lson ...

  8. HDU 1556 Color the ball(线段树区间更新)

    Color the ball 我真的该认真的复习一下以前没懂的知识了,今天看了一下线段树,以前只会用模板,现在看懂了之后,发现还有这么多巧妙的地方,好厉害啊 所以就应该尽量搞懂 弄明白每个知识点 [题 ...

  9. (简单) HDU 1698 Just a Hook , 线段树+区间更新。

    Description: In the game of DotA, Pudge’s meat hook is actually the most horrible thing for most of ...

  10. HDU 1698 Just a Hook(线段树区间更新查询)

    描述 In the game of DotA, Pudge’s meat hook is actually the most horrible thing for most of the heroes ...

随机推荐

  1. 动态横向(水平)合并GridView数据行DataRow的列

    前一段时间,Insus.NET有写过<动态合并GridView数据行DataRow的列>http://www.cnblogs.com/insus/p/3238348.html, 那是纵向( ...

  2. 工作中用的cobbler命令行

    在使用cobbler服务器,从pxe启动虚机的时候,经常用到的cobbler命令行 1.查看注册信息 cobbler system report --name=test25 2.注册信息 cobble ...

  3. addEntriesFromDictionary用法

    1.addEntriesFromDictionary在字典中的用法: NSMutableDictionary *dic1 = [NSMutableDictionary dictionaryWithOb ...

  4. (原创)E - Straight Shot Gym - 101652R

    解题思路:这道题的题意就是给你n,总距离X,速度v:以及n组数据:人行道的左端点和右端点,以及人行道的速度(竖直方向),如果从(0,0)到(X,0)的时间小于2X/v,则输出其时间,否则输出”Too ...

  5. luogu2486 [SDOI2011]染色

    link 题目大意:给定一个N个点的树,每个点有一个颜色 有M次操作,每次可以修改树某条链所有点变成一个颜色,查询某条链上点的颜色段数 树剖,线段树维护区间合并 我的代码记录的是某个区间左端点颜色.右 ...

  6. [HNOI2009]梦幻布丁 BZOJ1483 set

    题目描述 N个布丁摆成一行,进行M次操作.每次将某个颜色的布丁全部变成另一种颜色的,然后再询问当前一共有多少段颜色.例如颜色分别为1,2,2,1的四个布丁一共有3段颜色. 输入输出格式 输入格式: 第 ...

  7. kuangbin专题七 POJ3264 Balanced Lineup (线段树最大最小)

    For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000) always line up in the same order. One d ...

  8. (16)break和continue

    #(1)这里顺带说下pass # pass 过 的意思,防止代码报错,就是个占位的, if True: # 不允许代码块里面的内容为空,用pass占位 pass #(2)break (只能用在循环当中 ...

  9. springmvc htmlEscape标签的作用

    有些东西自己不知道就想要弄明白 唉   做项目 看人家项目中用到啦 不会 不知道 就百度啦 整理了一下 方便自己记忆 一.SpringMVC 表单元素标签 如下: <form:textarea ...

  10. sql 日期对比

    来自这里