http://poj.org/problem?id=2104

裸题不说。主席树水过。

#include <cstdio>
#include <iostream>
#include <algorithm>
using namespace std;
#define dbg(x) cout << #x << "=" << x << endl
#define read(x) x=getint()
#define for1(i, a, n) for(int i=a; i<=(n); ++i)
#define MID (l+r)>>1
inline int getint() { char c; int ret=0, k=1; for(c=getchar(); c<'0' || c>'9'; c=getchar()) if(c=='-') k=-1; for(; c>='0' && c<='9'; c=getchar()) ret=ret*10+c-'0'; return ret*k; } const int N=100010;
int id[N], a[N], b[N], n, m, root[N], cnt, key;
struct node { int l, r, s; } T[N*50];
void update(const int &l, const int &r, int &pos) {
T[++cnt]=T[pos]; pos=cnt; ++T[pos].s;
if(l==r) return;
int m=MID;
if(key<=m) update(l, m, T[pos].l); else update(m+1, r, T[pos].r);
}
int query(const int &l, const int &r, const int &x, const int &y, const int &k) {
if(l==r) return l;
int m=MID, s=T[T[y].l].s-T[T[x].l].s;
if(k<=s) return query(l, m, T[x].l, T[y].l, k); else return query(m+1, r, T[x].r, T[y].r, k-s);
}
bool cmp(const int &x, const int &y) { return a[x]<a[y]; }
int main() {
read(n); read(m);
for1(i, 1, n) read(a[i]), id[i]=i;
sort(id+1, id+1+n, cmp);
for1(i, 1, n) b[id[i]]=i;
for1(i, 1, n) {
root[i]=root[i-1]; key=b[i];
update(1, n, root[i]);
}
int x, y;
while(m--) {
read(x); read(y); read(key);
printf("%d\n", a[id[query(1, n, root[x-1], root[y], key)]]);
}
return 0;
}

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

Hint

This problem has huge input,so please use c-style input(scanf,printf),or you may got time limit exceed.

Source

Northeastern Europe 2004, Northern Subregion

【POJ】2104 K-th Number(区间k大+主席树)的更多相关文章

  1. 计蒜客 38229.Distance on the tree-1.树链剖分(边权)+可持久化线段树(区间小于等于k的数的个数)+离散化+离线处理 or 2.树上第k大(主席树)+二分+离散化+在线查询 (The Preliminary Contest for ICPC China Nanchang National Invitational 南昌邀请赛网络赛)

    Distance on the tree DSM(Data Structure Master) once learned about tree when he was preparing for NO ...

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

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

  3. POJ 2104(K-th Number-区间第k大-主席树)

    K-th Number Time Limit: 20000MS   Memory Limit: 65536K Total Submissions: 31790   Accepted: 9838 Cas ...

  4. POJ 2104 求序列里第K大 主席树裸题

    给定一个n的序列,有m个询问 每次询问求l-r 里面第k大的数字是什么 只有询问,没有修改 可以用归并树和划分树(我都没学过..囧) 我是专门冲着弄主席树来的 对主席树的建树方式有点了解了,不过这题为 ...

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

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

  6. Count on a tree(SPOJ COT + 树上第k大 + 主席树 + LCA)

    题目链接:https://www.spoj.com/problems/COT/en/ 题目: 题意: 给你一棵有n个节点的树,求节点u到节点v这条链上的第k大. 思路: 我们首先用dfs进行建题目给的 ...

  7. 静态区间第K小(整体二分、主席树)

    题目链接 题解 主席树入门题 但是这里给出整体二分解法 整体二分顾名思义是把所有操作放在一起二分 想想,如果求\([1-n]\)的第\(k\)小怎么二分求得? 我们可以二分答案\(k\), \(O(n ...

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

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

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

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

随机推荐

  1. CH round #55 Streaming #6

    T^T Saffah大神照样刷我这样诚心诚意想做一套NOIP模拟题的蒟蒻. 第一题 九九归一 好diao的名字... 题意就是给定一队$n,q$,求在模$n$意义下一个数$x$自乘的循环节长度. 当$ ...

  2. ASP.NET 画图与图像处理-如何直接输出到页面

    有时候我们生成的图片并不需要保存到磁盘中,而是直接输出到页面,比如验证码.实时报表等,如何做呢?请参考如下:     protected void Page_Load(object sender, E ...

  3. Linux下PS1、PS2、PS3、PS4使用详解

    参考印象笔记:

  4. 双操作系统Grub 引导修护

    ,只要进入ubuntu :sudo update-grub 就行了! 它会自动给Grub添加NTFS模块,以支持NTFS下的文件读取 转自: http://zhidao.baidu.com/link? ...

  5. 【云计算】Netflix 开源持续交付平台 Spinnaker

    oschina        发布于: 2015年11月19日 (0评)          分享到:    收藏 +1 CDS首都在线全球云主机.全球私有网络,开工送礼,免费试用! »   日前,Ne ...

  6. C++ traits

    [本文链接] http://www.cnblogs.com/hellogiser/p/cplusplus-traits.html [分析] 什么是traits?其实它并不是一个新的概念,上个世纪90年 ...

  7. mysql 我的学习

    安装要求 安装环境:CentOS-6.3安装方式:源码编译安装 软件名称:mysql-cluster-gpl-7.2.6-linux2.6-x86_64.tar.gz下载地址:http://mysql ...

  8. Java for LeetCode 057 Insert Interval

    Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa ...

  9. Java数据类型和运算符

    一,数据类型分类(2种) 1. 基本数据类型(3种) 数值型: 整数类型(4种): byte(1字节):范围(-128~127): short(2字节):范围(-32768~32767): int(4 ...

  10. Linux中编译、安装nginx

    Nginx ("engine x") 是一个高性能的HTTP和反向代理服务器,也是一个IMAP/POP3/SMTP 代理服务器. Nginx 是由Igor Sysoev为俄罗斯访问 ...