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.InputThe 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 10 9 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).OutputFor 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.


  很裸的主席树模板题。不知道可不可以用O(n1.5log2n)的莫队加平衡树来做。

  如果用下面这段代码去过poj的话会T掉,如果不是用new动态分配内存而是直接分配一个数组,在数组中储存所有节点可能就可以过了。

Code

 /**
* OpenJudge
* Bailian#2104
* Accepted
* Time:2247ms
* Memory:65536k
*/
#include<iostream>
#include<fstream>
#include<sstream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<ctime>
#include<cctype>
#include<cmath>
#include<algorithm>
#include<stack>
#include<queue>
#include<set>
#include<map>
#include<vector>
using namespace std;
typedef bool boolean;
#define smin(a, b) (a) = min((a), (b))
#define smax(a, b) (a) = max((a), (b))
template<typename T>
inline void readInteger(T& u){
char x;
int aFlag = ;
while(!isdigit((x = getchar())) && x != '-' && x != -);
if(x == -){
ungetc(x, stdin);
return;
}
if(x == '-'){
x = getchar();
aFlag = -;
}
for(u = x - ''; isdigit((x = getchar())); u = (u << ) + (u << ) + x - '');
ungetc(x, stdin);
u *= aFlag;
} typedef class SegTreeNode{
public:
int s; //size
SegTreeNode *left, *right;
SegTreeNode():left(NULL), right(NULL), s(){ }
void pushUp(){
this->s += this->left->s + this->right->s;
}
}SegTreeNode; typedef class SegTree{
public:
SegTreeNode* root;
SegTree():root(NULL){ }
SegTree(int size, int val){
build(root, , size, val);
}
SegTree(SegTreeNode* root):root(root){ } void build(SegTreeNode*& node, int from, int end, int val){
node = new SegTreeNode();
if(from == end){
node->s = (val == from) ? () : ();
return;
}
int mid = (from + end) >> ;
build(node->left, from, mid, val);
build(node->right, mid + , end, val);
node->pushUp();
}
}SegTree; typedef class Pair{
public:
int data;
int index;
Pair(const int data = , const int index = ):data(data), index(index){ }
boolean operator < (Pair another) const {
return data < another.data;
}
}Pair; typedef class ChairTree{ //主席树
public:
SegTree* st;
int* discrete;
map<int, int> to;
int len;
ChairTree():st(NULL), discrete(NULL){ }
ChairTree(int* lis, int len):len(len){
discrete = new int[(const int)(len + )];
memcpy(discrete, lis, sizeof(int) * (len + ));
sort(discrete + , discrete + len + );
for(int i = ; i <= len; i++)
to[discrete[i]] = i;
st = new SegTree[(const int)(len + )];
st[] = SegTree(len, );
for(int i = ; i <= len; i++){
st[i] = SegTree();
appendSegTree(st[i].root, st[i - ].root, , len, to[lis[i]]);
}
} void appendSegTree(SegTreeNode*& node, SegTreeNode*& before, int from, int end, int val){
node = new SegTreeNode();
if(from == end){
node->s = ;
return;
}
int mid = (from + end) >> ;
if(val <= mid){
node->right = before->right;
appendSegTree(node->left, before->left, from, mid, val);
}else{
node->left = before->left;
appendSegTree(node->right, before->right, mid + , end, val);
}
node->pushUp();
} int query(int l, int r, SegTreeNode*& left, SegTreeNode*& right, int k){
if(l == r){
return l;
}
int ls = left->left->s - right->left->s;
int mid = (l + r) >> ;
if(ls >= k) return query(l, mid, left->left, right->left, k);
return query(mid + , r, left->right, right->right, k - ls);
} int query(int from, int end, int k){
return discrete[query(, len, st[end].root, st[from - ].root, k)];
}
}ChairTree; int n, m;
ChairTree ct;
int *lis; inline void init(){
readInteger(n);
readInteger(m);
lis = new int[(const int)(n + )];
for(int i = ; i <= n; i++){
readInteger(lis[i]);
}
ct = ChairTree(lis, n);
} inline void solve(){
int l, r, k;
while(m--){
readInteger(l);
readInteger(r);
readInteger(k);
int res = ct.query(l, r, k);
printf("%d\n", res);
}
} int main(){
init();
solve();
return ;
}

Online Judge 2014 K-th Number -主席树的更多相关文章

  1. poj2104 k-th number 主席树入门讲解

    poj2104 k-th number 主席树入门讲解 定义:主席树是一种可持久化的线段树 又叫函数式线段树   刚开始学是不是觉得很蒙逼啊 其实我也是 主席树说简单了 就是 保留你每一步操作完成之后 ...

  2. poj 2104 K-th Number 主席树+超级详细解释

    poj 2104 K-th Number 主席树+超级详细解释 传送门:K-th Number 题目大意:给出一段数列,让你求[L,R]区间内第几大的数字! 在这里先介绍一下主席树! 如果想了解什么是 ...

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

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

  4. POJ 2104 K-th Number ( 求取区间 K 大值 || 主席树 || 离线线段树)

    题意 : 给出一个含有 N 个数的序列,然后有 M 次问询,每次问询包含 ( L, R, K ) 要求你给出 L 到 R 这个区间的第 K 大是几 分析 : 求取区间 K 大值是个经典的问题,可以使用 ...

  5. poj2104 K-th Number区间第k小值 主席树

    原来主席树就是可持久化线段树啊,刚知道,,, 作为一道裸题,还是必A的,然而一开始偷懒不写离散化跪了N多遍,后来在缪大的帮助下发现了这个问题,遂A之 ——又是这种破问题,实在不想说自己了 把n个数看成 ...

  6. POJ2104 K-th Number[主席树]【学习笔记】

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

  7. [poj2104] K-th Number (主席树)

    主席树 Description You are working for Macrohard company in data structures department. After failing y ...

  8. poj 2104 K-th Number(主席树 视频)

    K-th Number 题意: 给你一些数,让你求一个区间内,第k大的数是多少. 题解: 主席树第一题,看的qsc视频写的,戳戳戳 学到了unique函数,他的作用是:把相邻的重复的放到后面,返回值是 ...

  9. 主席树:POJ2104 K-th Number (主席树模板题)

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

随机推荐

  1. Centralized Cache Management in HDFS

    Overview(概述) Centralized cache management in HDFS is an explicit caching mechanism that allows users ...

  2. Visibility from other objects

    php.net <?php class Test { private $foo; public function __construct($foo) { $this->foo=$foo; ...

  3. JZ2440裸板烧写(打补丁)

    制作uImage,需要上网下载内核+patch补丁 1.将内核用ftp发送到 打补丁patch -p1 < ../补丁文件名 打补丁文件(目录) 2.配置 :复制cp  configuratio ...

  4. 分布式网格缓存Coherence简介

    Coherence企业级缓存(一) 特点 摘要:Oracle Coherence是一个企业级的分布式集群缓存框架.具有自管理,自恢复,高可用性,高扩展性等优良特点,在电信BOSS等项目中有很大的应用价 ...

  5. 给sql server2005打补丁报错:无法安装Windows Installer MSP文件

    给sql server2005打补丁报错:无法安装Windows Installer MSP文件 在我们安装完SQL2005数据库后,需要安装SP4补丁时,会出错:无法安装Windows Instal ...

  6. dedecms首页调用随机文章全自动时时更新

    dedecms织梦系统是全站生成静态html的,这个对搜索引擎比较友好,但是有时我们要调用文章,让蜘蛛每次来访问都感觉像是有添加新内容一样,要如何做到呢? 可以添加以下dedecms随机文章调用的参数 ...

  7. 【深入理解javascript】原型

    1.一切都是对象 一切(引用类型)都是对象,对象是属性的集合 typeof函数输出的一共有几种类型,在此列出: function show(x) { console.log(typeof(x)); / ...

  8. Redis的五种数据结构的内部编码

    type命令实际返回的就是当前键的数据结构类型,它们分别是:string(字符串).hash(哈希). list(列表).set(集合).zset(有序集合),但这些只是Redis对外的数据结构. 实 ...

  9. (转)跨域的另一种解决方案——CORS(Cross-Origin Resource Sharing)跨域资源共享

    在我们日常的项目开发时使用AJAX,传统的Ajax请求只能获取在同一个域名下面的资源,但是HTML5打破了这个限制,允许Ajax发起跨域的请求.浏览器是可以发起跨域请求的,比如你可以外链一个外域的图片 ...

  10. 最小生成树(kruskal模版 Prim模板)

    http://acm.sdut.edu.cn/sdutoj/showproblem.php?pid=2144&cid=1186 最小生成树,最重要的是了解思想 稠密图用Prim,稀疏图用Kru ...