题目链接

K-th Number
Time Limit: 20000MS Memory Limit: 65536K
Total Submissions: 36890 Accepted: 11860
Case Time Limit: 2000MS

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.
题意很简单,给定长度为n的数组,有m次查询,每次长须L到R中第k大的数。
很早便想A掉这道题,开始用平方分割写的这题,妥妥的TLE. 后来知道这类题目应该
使用归并树,或者划分树,或是更高级的主席树,刚刚对归并树有一点了解,便来1A了他。
做法就是:如果x是某个区间的第k大数,那么区间内不超过x的数不小于k个。
而且区间内小于x的数不足k个。
二分x, 查询区间内不大于x的数的个数。
Accepted Code:
 /*************************************************************************
> File Name: 2104.cpp
> Author: Stomach_ache
> Mail: sudaweitong@gmail.com
> Created Time: 2014年08月02日 星期六 19时25分26秒
> Propose:
************************************************************************/ #include <cmath>
#include <string>
#include <cstdio>
#include <vector>
#include <fstream>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std; #define lson(x) (x<<1)
#define rson(x) ((x<<1)|1)
const int maxn = ;
int n, m;
int a[maxn], nums[maxn];
vector<int> dat[maxn<<]; void build(int o, int l, int r) {
if (r - l == ) {
dat[o].push_back(a[l]);
} else {
int mid = (l + r) / ;
build(lson(o), l, mid);
build(rson(o), mid, r);
dat[o].resize(r-l);
merge(dat[lson(o)].begin(), dat[lson(o)].end(), dat[rson(o)].begin(), dat[rson(o)].end(), dat[o].begin());
}
} int query(int L, int R, int x, int o, int l, int r) {
if (l >= R || r <= L) return ;
else if (l >= L && r <= R) return upper_bound(dat[o].begin(), dat[o].end(), x) - dat[o].begin();
else {
int md = (l + r) / ;
int lc = query(L, R, x, lson(o), l, md);
int rc = query(L, R, x, rson(o), md, r);
return lc + rc;
}
} int main(void) {
while (~scanf("%d %d", &n, &m)) {
for (int i = ; i < n; i++) scanf("%d", a + i);
build(, , n);
sort(a, a + n);
while (m--) {
int l, r, k;
scanf("%d %d %d", &l, &r, &k);
l--; r--;
int lb = -, ub = n-;
while (ub - lb > ) {
int md = (lb + ub) / ;
int c = query(l, r+, a[md], , , n);
if (c >= k) ub = md;
else lb = md;
}
printf("%d\n", a[ub]);
}
} return ;
}

Poj 2104区间第k大(归并树)的更多相关文章

  1. POJ 2104 区间第k大(主席树)

    题目链接:http://poj.org/problem?id=2104 题目大意:给定还有n个数的序列,m个操作,每个操作含有l,r,k,求区间[l,r]第k大 解题思路:线段树只能维护序列的最大值最 ...

  2. HDU2665 求区间第K大 主席树

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=2665 代码: //#include<bits/stdc++.h> #include< ...

  3. POJ-2104-K-th Number(区间第K大+主席树模板题)

    Description You are working for Macrohard company in data structures department. After failing your ...

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

    题目链接: http://poj.org/problem?id=2104 K-th Number Time Limit: 20000MSMemory Limit: 65536K 问题描述 You ar ...

  5. poj 2104 主席树(区间第k大)

    K-th Number Time Limit: 20000MS   Memory Limit: 65536K Total Submissions: 44940   Accepted: 14946 Ca ...

  6. POJ 2104 && POJ 2761 (静态区间第k大,主席树)

    查询区间第K大,而且没有修改. 使用划分树是可以做的. 作为主席树的入门题,感觉太神奇了,Orz /* *********************************************** ...

  7. POJ 2104 HDU 2665 主席树 解决区间第K大

    两道题都是区间第K大询问,数据规模基本相同. 解决这种问题, 可以采用平方划分(块状表)复杂度也可以接受,但是实际表现比主席树差得多. 这里大致讲一下我对主席树的理解. 首先,如果对于某个区间[L,R ...

  8. 静态区间第k大(归并树)

    POJ 2104为例 思想: 利用归并排序的思想: 建树过程和归并排序类似,每个数列都是子树序列的合并与排序. 查询过程,如果所查询区间完全包含在当前区间中,则直接返回当前区间内小于所求数的元素个数, ...

  9. 【POJ】【2104】区间第K大

    可持久化线段树 可持久化线段树是一种神奇的数据结构,它跟我们原来常用的线段树不同,它每次更新是不更改原来数据的,而是新开节点,维护它的历史版本,实现“可持久化”.(当然视情况也会有需要修改的时候) 可 ...

随机推荐

  1. 查询sitemap中重复的记录

    ; ;

  2. Vuejs之Component slot 插槽详解

    Vuejs的component的数据进行了沙箱隔离,除js全局变量如Math, Date之类外无法访问用户自定义的变量,所以使用component写组件或嵌套组件时明白变量的访问非常重要 编译作用域 ...

  3. MacOS 读写 NTFS 即插即用.

    1. 安装osxfusehttps://osxfuse.github.io/ 2. 安装brewhttps://brew.sh/index_zh-cn.html 3. 安装ntfs-3gbrew in ...

  4. Python学习之enumerate

         enumerate还可以接收第二个参数,用于指定索引起始值   2.     注意open返回文件对象,可迭代,而os.open返回的是文件指针,int类型, <wiz_tmp_tag ...

  5. LOJ#3119 随机立方体

    解:极大值至少为1.我们尝试把最大那个数的影响去掉. 最大那个数所在的一层(指一个三维十字架)都是不可能成为最大值的. 考虑容斥.我们试图求除了最大值以外至少有k个极大值的概率. 我们钦定某k个位置是 ...

  6. 方法的重写(override)两同两小一大原则:

    方法名相同,参数类型相同 子类返回类型小于等于父类方法返回类型, 子类抛出异常小于等于父类方法抛出异常, 子类访问权限大于等于父类方法访问权限.

  7. Thinkphp 加载更多

    要实现的效果是这样的: 每次点击显示更多按钮,都会往下显示2条数据,直到后面没有数据了.. 数据表: articleList模板文件 <include file="./Applicat ...

  8. RSA加密算法在WEB中的应用

    加密算法有很多,如不可逆的摘要算法MD5.SHA(安全哈希算法),可逆的Base64编码,对称加密算法DES.AES,还有非对称加密算法DH.RSA等.那是不是说明我们可以使用任何一种加密算法就能保证 ...

  9. DOM4J -(XML解析包)

    DOM4J - 简介 是dom4j.org出品的一个开源XML解析包.Dom4j是一个易用的.开源的库,用于XML,XPath和XSLT.它应用于Java平台,采用了Java集合框架并完全支持DOM, ...

  10. MySQL数据库 数据库的引擎,模式,数据类型(更新中...)

    数据库的引擎:驱动数据的方式-数据库优化 前提:引擎是建表的时候规定的,提供给表使用的,不是数据库 mysql> show engines #展示所有引擎 #几种比较常见的引擎 # innodb ...