poj 2104 K-th Number(主席树)
Description
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 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
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
【思路】
主席树
戳这 click here
【代码】
- #include<cstdio>
- #include<cstring>
- #include<iostream>
- #include<algorithm>
- #define FOR(a,b,c) for(int a=(b);a<=(c);a++)
- using namespace std;
- typedef long long ll;
- const int N = 1e5+;
- const int M = *1e6+;
- int n,m,tot,sz;
- int v[N],hash[N],root[N];
- int sum[M],ls[M],rs[M];
- ll read() {
- char c=getchar();
- ll x=,f=;
- while(!isdigit(c)) {
- if(c=='-') f=-; c=getchar();
- }
- while(isdigit(c))
- x=x*+c-'',c=getchar();
- return x*f;
- }
- void update(int l,int r,int x,int& y,int num)
- {
- y=++sz;
- sum[y]=sum[x]+;
- if(l==r) return ;
- ls[y]=ls[x],rs[y]=rs[x];
- int mid=(l+r)>>;
- if(num<=mid) update(l,mid,ls[x],ls[y],num);
- else update(mid+,r,rs[x],rs[y],num);
- }
- int query(int x,int y,int rk)
- {
- int a=root[x-],b=root[y];
- int l=,r=tot;
- while(l<r) {
- int mid=(l+r)>>;
- int now=sum[ls[b]]-sum[ls[a]];
- if(rk<=now) r=mid,a=ls[a],b=ls[b];
- else l=mid+,rk-=now,a=rs[a],b=rs[b];
- }
- return hash[l];
- }
- int main() {
- n=read(),m=read();
- FOR(i,,n)
- v[i]=read(),hash[i]=v[i];
- sort(hash+,hash+n+);
- tot=unique(hash+,hash+n+)-hash-;
- FOR(i,,n)
- v[i]=lower_bound(hash+,hash+tot+,v[i])-hash;
- FOR(i,,n) {
- update(,tot,root[i-],root[i],v[i]);
- }
- int x,y,z;
- FOR(i,,m) {
- x=read(),y=read(),z=read();
- printf("%d",query(x,y,z));
- if(i!=m) puts("");
- }
- return ;
- }
poj 2104 K-th Number(主席树)的更多相关文章
- 【POJ 2104】 K-th Number 主席树模板题
达神主席树讲解传送门:http://blog.csdn.net/dad3zz/article/details/50638026 2016-02-23:真的是模板题诶,主席树模板水过.今天新校网不好,没 ...
- 静态区间第k大(主席树)
POJ 2104为例(主席树入门题) 思想: 可持久化线段树,也叫作函数式线段树,也叫主席树(高大上). 可持久化数据结构(Persistent data structure):利用函数式编程的思想使 ...
- poj 2104 K-th Number 主席树+超级详细解释
poj 2104 K-th Number 主席树+超级详细解释 传送门:K-th Number 题目大意:给出一段数列,让你求[L,R]区间内第几大的数字! 在这里先介绍一下主席树! 如果想了解什么是 ...
- poj2104 k-th number 主席树入门讲解
poj2104 k-th number 主席树入门讲解 定义:主席树是一种可持久化的线段树 又叫函数式线段树 刚开始学是不是觉得很蒙逼啊 其实我也是 主席树说简单了 就是 保留你每一步操作完成之后 ...
- POJ 2104 K-th Number 主席树(区间第k大)
题目链接: http://poj.org/problem?id=2104 K-th Number Time Limit: 20000MSMemory Limit: 65536K 问题描述 You ar ...
- POJ 2104:K-th Number(主席树静态区间k大)
题目大意:对于一个序列,每次询问区间[l,r]的第k大树. 分析: 主席树模板题 program kthtree; type point=record l,r,s:longint; end; var ...
- POJ 2104 K-th Number ( 求取区间 K 大值 || 主席树 || 离线线段树)
题意 : 给出一个含有 N 个数的序列,然后有 M 次问询,每次问询包含 ( L, R, K ) 要求你给出 L 到 R 这个区间的第 K 大是几 分析 : 求取区间 K 大值是个经典的问题,可以使用 ...
- SPOJ MKTHNUM & POJ 2104 - K-th Number - [主席树模板题]
题目链接:http://poj.org/problem?id=2104 Description You are working for Macrohard company in data struct ...
- poj 2104 K-th Number(主席树 视频)
K-th Number 题意: 给你一些数,让你求一个区间内,第k大的数是多少. 题解: 主席树第一题,看的qsc视频写的,戳戳戳 学到了unique函数,他的作用是:把相邻的重复的放到后面,返回值是 ...
- Poj 2104 K-th Number(主席树&&整体二分)
K-th Number Time Limit: 20000MS Memory Limit: 65536K Case Time Limit: 2000MS Description You are wor ...
随机推荐
- 荣誉,还是苦逼?| 也议全栈工程师和DevOps
引言 全栈工程师(本文称「全栈」开发者)和 DevOps 无疑是近期最火的词汇,无论是国外还是国内.而且火爆程度远超于想象. 全栈和 DevOps,究竟是我们的新职业方向,还是仅仅创业公司老板的心头所 ...
- 《Introduction to Tornado》中文翻译计划——第五章:异步Web服务
http://www.pythoner.com/294.html 本文为<Introduction to Tornado>中文翻译,将在https://github.com/alioth3 ...
- Apache Flume 简介
转自:http://blog.163.com/guaiguai_family/blog/static/20078414520138100562883/ Flume 是 Cloudera 公司开源出来的 ...
- http://jingyan.baidu.com/article/e4511cf33479812b855eaf67.html
http://jingyan.baidu.com/article/e4511cf33479812b855eaf67.html
- Android 通过http访问服务器
目前Android 与服务器交互有两种方式:1.Socket 2. Http : 但由于Http的封装性以及性能比socket要好,所以推荐使用http方式和服务器交互: 通过http访问服务器有三种 ...
- POJ2635——The Embarrassed Cryptographer(高精度取模+筛选取素数)
The Embarrassed Cryptographer DescriptionThe young and very promising cryptographer Odd Even has imp ...
- Android Editext监听光标位置
因为项目需要,需要实时监听光标的位置变化,网上提出的用TextWatcher和onTouchListener中调用contentText.getSelectionStart()都是获取的上一次位置. ...
- Hadoop分布式文件系统(HDFS)详解
HDFS简介: 当数据集的大小超过一台独立物理计算机的存储能力时,就有必要对它进行分区 (partition)并存储到若干台单独的计算机上.管理网络中跨多台计算机存储的文件系统成为分布式文件系统 (D ...
- P102、面试题14:调整数组顺序使奇数位于偶数前面
题目:输入一个整数数组,实现一个函数来调整该数组中数字的属性怒,使得所有奇数位于数组的前半部分,所有偶数位于数组的后半部分. 思路:其实就是用快速排序法的第一轮排序,从左右夹逼,左边遇到偶数,停下来, ...
- 怎样成为一名PHP专家?
当浏览各类与PHP相关的博客时,比如Quora上的问题,谷歌群组,简讯和杂志,我经常注意到技能的等级分化.问题都类似于“我如何连接到MySQL数据库?”或者“我该如何扩展邮件系统才能在每小时发送超过一 ...