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. [bzoj3680]吊打XXX_模拟退火

    吊打XXX bzoj-3680 题目大意:在平面上给定n个点,每个点有一个权值.请在平面上找出一个点(不一定在这n个点内找)使得这个点到n个点的距离*权值最小,即求这n个点的重心. 注释:$1\le ...

  2. POJ 2374

    挺水的一道线段树+DP题.可以从底往上添加线段,每添加线段之前查询端点所被覆盖的区间线段.再从最顶往下DP,每次从端点出发,递推覆盖该端点的区间线段的两端的值即可. #include <cstd ...

  3. POJ 1329

    模板题,注意一下输出就可以. #include <iostream> #include <cstdio> #include <cmath> #include < ...

  4. 用css3和canvas实现的蜂窝动画效果

    近期工作时研究了一下css3动画和js动画.主要是工作中为了增强页面的趣味性,大家都有意无意的加入了非常多动画效果.当然大部分都是css3动画效果.能够gpu加速,这会降低移动端的性能需求. 今天主要 ...

  5. 【转载】linux中shell命令test用法和举例

    test 命令最短的定义可能是评估一个表达式:如果条件为真,则返回一个 0 值.如果表达式不为真,则返回一个大于 0 的值 — 也可以将其称为假值.检查最后所执行命令的状态的最简便方法是使用 $? 值 ...

  6. Scala入门到精通——第一节 Scala语言初步

    本节主要内容 Scala简单介绍 为什么要学习Scala Scala语言初步 1. Scala简单介绍 Scala(Scala Language的简称)语言是一种能够执行于JVM和.Net平台之上的通 ...

  7. MySQL数据库——索引与视图

    索引 MySQL的索引包括普通索引.唯一性索引(unique index).全文索引(fulltext index).单列索引.多列索引和空间索引等. 1.索引的创建 ·创建表的时候创建索引 SQL语 ...

  8. Java类集综合练习——信息管理(增、删、改、查)

    一.实现功能——模拟学生选课功能 1.选择课程 2.修改所选课程 二.实现功能——模拟学生信息管理功能 1.添加学生信息 2.修改学生信息 二.主要代码(在同一个包里) 1.课程类 public cl ...

  9. Sublime Text 2 SFTP UnicodeDecodeError错误!

    右键-->SFTP/FTP ->Sync Remote To Local {作者:半条虫(466814195)} 提示下面错误 An unexpected error occurred, ...

  10. [NOIP 2007] 树网的核

    [题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=1999 [算法] 树的直径 + 单调队列 [代码] #include<bits/ ...