Description

You are working for Macrohard company in data structures department. After failing your previous task about key insertion you were asked to write a new data structure that would be able to return quickly k-th order statistics in the array segment. 
That is, given an array a[1...n] of different integer numbers, your program must answer a series of questions Q(i, j, k) in the form: "What would be the k-th number in a[i...j] segment, if this segment was sorted?" 
For example, consider the array a = (1, 5, 2, 6, 3, 7, 4). Let the question be Q(2, 5, 3). The segment a[2...5] is (5, 2, 6, 3). If we sort this segment, we get (2, 3, 5, 6), the third number is 5, and therefore the answer to the question is 5.

Input

The first line of the input file contains n --- the size of the array, and m --- the number of questions to answer (1 <= n <= 100 000, 1 <= m <= 5 000). 
The second line contains n different integer numbers not exceeding 109 by their absolute values --- the array for which the answers should be given. 
The following m lines contain question descriptions, each description consists of three numbers: i, j, and k (1 <= i <= j <= n, 1 <= k <= j - i + 1) and represents the question Q(i, j, k).

Output

For each question output the answer to it --- the k-th number in sorted a[i...j] segment.

Sample Input

7 3
1 5 2 6 3 7 4
2 5 3
4 4 1
1 7 3

Sample Output

5
6
3

趁热赶紧来道主席树裸题,感觉有些明白了
主席树,可持久化线段树,核心思想就是维护线段树的历史状态
在这道题中,我们以数字大小为位置,位置上存的是出没出现过(1/0),统计加和
我们依次将数字加入线段树,这样就会形成n个历史版本
再加上我们维护的数据具有可减性,所以对于(l,r),每个节点位置上只要用r这颗线段树减去l线段树对应的值,就是相当于只维护了(l,r)这个区间内的元素的线段树,自然就可以在上面愉快的跳跃了~
这次的主席树大部分是自己写的了2333,看看吧~
 #include<cstdio>
#include<algorithm>
#define N 100005
using namespace std;
int n,m,siz,tot;
int a[N],b[N],rt[N];
struct node{
int l,r,sum;
}t[*N];
int insert(int k,int x,int l,int r){
t[++tot]=t[k];k=tot;
if(l==r){
t[k].l=t[k].r=;
t[k].sum++;
return tot;
}
int mid=(l+r)>>;
if(x<=mid) t[k].l=insert(t[k].l,x,l,mid);
else t[k].r=insert(t[k].r,x,mid+,r);
t[k].sum=t[t[k].l].sum+t[t[k].r].sum;
return k;
}
int query(int lk,int rk,int x,int l,int r){
if(l==r)return l;
int mid=(l+r)>>;
if(t[t[rk].l].sum-t[t[lk].l].sum>=x)return query(t[lk].l,t[rk].l,x,l,mid);
else return query(t[lk].r,t[rk].r,x-(t[t[rk].l].sum-t[t[lk].l].sum),mid+,r);
}
int main(){
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++)scanf("%d",&a[i]),b[i]=a[i];
sort(b+,b++n);
siz=unique(b+,b++n)-b-;
for(int i=;i<=n;i++)a[i]=lower_bound(b+,b++n,a[i])-b;
tot=;
for(int i=;i<=n;i++)
rt[i]=insert(rt[i-],a[i],,n);
for(int i=,l,r,k;i<=m;i++){
scanf("%d%d%d",&l,&r,&k);
printf("%d\n",b[query(rt[l-],rt[r],k,,n)]);
}
return ;
}

2104 -- K-th Number的更多相关文章

  1. POJ 2104:K-th Number(主席树静态区间k大)

    题目大意:对于一个序列,每次询问区间[l,r]的第k大树. 分析: 主席树模板题 program kthtree; type point=record l,r,s:longint; end; var ...

  2. POJ 2104:K-th Number(整体二分)

    http://poj.org/problem?id=2104 题意:给出n个数和m个询问求区间第K小. 思路:以前用主席树做过,这次学整体二分来做.整体二分在yr大佬的指点下,终于大概懂了点了.对于二 ...

  3. 【POJ 2104】 K-th Number 主席树模板题

    达神主席树讲解传送门:http://blog.csdn.net/dad3zz/article/details/50638026 2016-02-23:真的是模板题诶,主席树模板水过.今天新校网不好,没 ...

  4. ACM-ICPC 2018 沈阳赛区网络预赛 K. Supreme Number

    A prime number (or a prime) is a natural number greater than 11 that cannot be formed by multiplying ...

  5. [POJ2104] K – th Number (可持久化线段树 主席树)

    题目背景 这是个非常经典的主席树入门题--静态区间第K小 数据已经过加强,请使用主席树.同时请注意常数优化 题目描述 如题,给定N个正整数构成的序列,将对于指定的闭区间查询其区间内的第K小值. 输入输 ...

  6. POJ 2104:K-th Number 整体二分

    感觉整体二分是个很有趣的东西. 在别人的博客上看到一句话 对于二分能够解决的询问,如果有多个,那么如果支持离线处理的话,那么就可以使用整体二分了 树套树写了一天还是WA着,调得焦头烂额,所以决定学cd ...

  7. ACM-ICPC 2018 沈阳赛区网络预赛 K Supreme Number(规律)

    https://nanti.jisuanke.com/t/31452 题意 给出一个n (2 ≤ N ≤ 10100 ),找到最接近且小于n的一个数,这个数需要满足每位上的数字构成的集合的每个非空子集 ...

  8. Count the number of possible triangles

    From: http://www.geeksforgeeks.org/find-number-of-triangles-possible/ Given an unsorted array of pos ...

  9. 大数据热点问题TOP K

    1单节点上的topK (1)批量数据 数据结构:HashMap, PriorityQueue 步骤:(1)数据预处理:遍历整个数据集,hash表记录词频 (2)构建最小堆:最小堆只存k个数据. 时间复 ...

  10. Codeforces Round #350 (Div. 2) F. Restore a Number 模拟构造题

    F. Restore a Number   Vasya decided to pass a very large integer n to Kate. First, he wrote that num ...

随机推荐

  1. linux下的mongodb数据库原生操作

    mongodb,是一种结构最像mysql的nosql mysql中的数据库,mongodb中也有,区别在于, myql中数据库下的是表,字段和数据的形式存在 mongodb数据库下的是叫集合(和pyt ...

  2. 【*2000】【2018-2019 ICPC, NEERC, Southern Subregional Contest C 】Cloud Computing

    [链接] 我是链接,点我呀:) [题意] [题解] 我们可以很容易知道区间的每个位置有哪些安排可以用. 显然 我们优先用那些花费的钱比较少的租用cpu方案. 但一个方案可供租用的cpu有限. 我们可以 ...

  3. Java基础学习总结(39)——Log4j 1使用教程

    1. 配置文件 Log4J配置文件的基本格式如下: #配置根Logger log4j.rootLogger  =   [ level ]   ,  appenderName1 ,  appenderN ...

  4. poj 3006水题打素数表

    #include<stdio.h> #include<string.h> #define N 1100000 int isprim[N],prime[N]; void ispr ...

  5. BA-WG-冷源

    冷源群控系统最好由冷源厂家来做的理由 1.冷机厂家对空调的参数十分的清楚,明确的知道冷机的负荷曲线,可以优化冷机加减载的最合理时间达到最佳的节能效果 2.独立的CSM硬件模块,内置不同冷机的型号特性, ...

  6. POJ 2079

    呃,不知道我用的算不算卡壳,总有点枚举的意思. 先求凸包,然后,枚举其中一点,再枚举另一点作为结尾,这个向量旋转一周后,求出最大值面积.这里面用的是旋转卡壳判断的那个式子. PS:下一篇和这题是一样题 ...

  7. emacs 搭建racket开发环境

    emacs 搭建racket开发环境 emacs下搭建开发racket的环境,笔者之前用过下面两种模式:geiser和racket-mode.相对而言,后一种方式要显得简单.本文主要介绍后一种方式环境 ...

  8. CCNP路由实验之十四 路由器的訪问控制ACL

     年9月1月12:00.还有一种时间叫做周期时间(periodic),即这个时间是会多次反复的.比方每周一,或者每周一到周五 ,"rotary 2″开启3002以此类推. 变成1,1变成 ...

  9. SQL SERVER读书笔记:JOIN

    nested loop join:适用于小数据集,有索引的情况.不占用内存,不用tempdb. merge join:大数据,要排序,多对多,用tempdb: hash join:对大数据集,少用户使 ...

  10. BZOJ1053 反素数

    题目大意 对于任何正整数x,其约数的个数记作g(x).如果某个正整数x满足对任意的0<i<x,都有g(x)>g(i) ,则称x为反质数.现在给定一个数N,求出不超过N的最大的反质数. ...