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. 在Android中使用FlatBuffers(中篇)

    本文来自网易云社区. FlatBuffers.Protobuf及JSON对比测试 FlatBuffers相对于Protobuf的表现又如何呢?这里我们用数据说话,对比一下FlatBuffers格式.J ...

  2. metasploit 读书笔记1

    The msfpayload component of Metasploit allows you to generate shellcode, executables, and much more ...

  3. L - Large Division (大数, 同余)

    Given two integers, a and b, you should check whether a is divisible by b or not. We know that an in ...

  4. 解决“System.Data.OracleClient 需要 Oracle 客户端软件 version 8.1.7 或更高版本”的问题

    以server2008为例: 首先确保使用sqlplus能访问数据库. 1.管理工具->计算机管理->本地用户和组->组->administrators属性,添加,高级,立即查 ...

  5. jzoj4916. 【GDOI2017模拟12.9】完全背包问题 (背包+最短路)

    题面 题解 考场上蠢了--这么简单的东西都想不到-- 首先排序加去重. 先来考虑一下,形如 \[a_1x_1+a_2x_2+...a_nx_n=w,a_1<a_2<...<a_n,x ...

  6. 背包 DP【洛谷P4158】 [SCOI2009]粉刷匠

    P4158 [SCOI2009]粉刷匠 windy有 N 条木板需要被粉刷. 每条木板被分为 M 个格子. 每个格子要被刷成红色或蓝色. windy每次粉刷,只能选择一条木板上一段连续的格子,然后涂上 ...

  7. C++并发低级接口:std::thread和std::promise

    std::thread和std::promise 相比std::async,std::thread就原始多了.thread一定会创建新线程(而不是像async那样创建的时候可能不会,后面才创建新线程( ...

  8. kuangbin专题十六 KMP&&扩展KMP HDU3336 Count the string

    It is well known that AekdyCoin is good at string problems as well as number theory problems. When g ...

  9. JDBC常用套路

    1.连接数据库.使用查询功能 //1.获取数据库连接 Connection con=JDBCTools.getConnection(); String sql="select * from ...

  10. P2906 [USACO08OPEN]牛的街区Cow Neighborhoods

    传送门 曼哈顿距离好像不好直接算,我们可以把牛的坐标转化一下以方便计算距离 (x,y) --> (x+y,x-y) 那么距离就可以表示成 $max(\left |x_1-x_2  \right ...