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. BZOJ 3119 Book (贪心+数学推导)

    手动博客搬家: 本文发表于20191029 22:49:41, 原地址https://blog.csdn.net/suncongbo/article/details/78388925 URL: htt ...

  2. 【ZOJ 4067】Books

    [链接] 我是链接,点我呀:) [题意] [题解] 统计a中0的个数cnt0 然后m减去cnt0 因为这cnt0个0是一定会取到的. 如果m==0了 那么直接找到数组中的最小值mi 输出mi-1就好 ...

  3. 【hihocoder 1499】A Box of Coins

    [题目链接]:http://hihocoder.com/problemset/problem/1499 [题意] [题解] 贪心,模拟; 从左往右对于每一列; 如果上下两个格子; ① 有一个格子超过了 ...

  4. 【ACM】poj_2092_Grandpa is Famous_201308021920

    Grandpa is FamousTime Limit: 2000MS  Memory Limit: 30000K Total Submissions: 7256  Accepted: 3670 De ...

  5. 网络编程----堵塞、非堵塞和同步、异步IO

    我是学渣.但我想进步. 本文是面试我的牛人问我的.你知道什么是堵塞.非堵塞和同步.异步IO么?自觉得是分布式系统程序猿的我居然不知道.学习吧. 首先介绍堵塞IO和非堵塞IO: 堵塞IO:是指说程序等待 ...

  6. Project Euler:Problem 77 Prime summations

    It is possible to write ten as the sum of primes in exactly five different ways: 7 + 3 5 + 5 5 + 3 + ...

  7. Cocos2d-x 3.0final 终结者系列教程02-开发环境的搭建

    本文主要以Mac平台和XCode5为基本系统环境和C++编程工具来介绍Cocos2d-x3.0final版的安装. 一.系统准备(预计要花掉半个月工资) MacBook Pro一台(本人的比較老.11 ...

  8. bzoj1833: [ZJOI2010]count 数字计数(数位DP+记忆化搜索)

    1833: [ZJOI2010]count 数字计数 题目:传送门 题解: 今天是躲不开各种恶心DP了??? %爆靖大佬啊!!! 据说是数位DP裸题...emmm学吧学吧 感觉记忆化搜索特别强: 定义 ...

  9. soapui icon以及resource的理解

    https://www.soapui.org/getting-started/soapui-interface/main-window.html 以调用博客园的api为例 第一个是域名 第二个是res ...

  10. 【NOIP 2002】 字串变换

    [题目链接] https://www.luogu.org/problemnew/show/P1032 [算法] 广度优先搜索 用stl库里的map来判重 [代码] #include<bits/s ...