poj 2104 K-th Number(主席树
| Time Limit: 20000MS | Memory Limit: 65536K | |
| Total Submissions: 59058 | Accepted: 20529 | |
| Case Time Limit: 2000MS | ||
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
Source
#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
const int N = ;
int ls[N*],rs[N*],sum[N*];
int n,m,a[N],hash[N],cnt,root[N];
int x,y,k;
void lsh()
{
sort(hash+,hash+n+);
cnt=unique(hash+,hash+n+)-(hash+);
for(int i=;i<=n;i++)a[i]=lower_bound(hash+,hash+cnt+,a[i])-hash;
}
int tot=;
void build(int l,int r,int &rt,int pre,int w)
{
rt=++tot;
sum[rt]=sum[pre]+;
if(l==r)return;
int mid=(l+r)>>;
if(w<=mid)rs[rt]=rs[pre],build(l,mid,ls[rt],ls[pre],w);
else ls[rt]=ls[pre],build(mid+,r,rs[rt],rs[pre],w);
}
int query(int l,int r,int x,int y,int k)
{
if(l==r)return l;
int mid=l+r>>,tmp=sum[ls[y]]-sum[ls[x]];
if(tmp>=k)return query(l,mid,ls[x],ls[y],k);
else return query(mid+,r,rs[x],rs[y],k-tmp);
}
int main()
{
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++)scanf("%d",a+i),hash[i]=a[i];
lsh();
// printf("%d\n",cnt);for(int i=1;i<=cnt;i++) printf("%d ",hash[i]);
for(int i=;i<=n;i++) build(,cnt,root[i],root[i-],a[i]);
for(int i=;i<=m;i++)
{
scanf("%d%d%d",&x,&y,&k);
printf("%d\n",hash[query(,cnt,root[x-],root[y],k)]);
}
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 ...
随机推荐
- 蓝牙nrf52832的架构和开发(转载)
相比TI的CC254X.DIALOG的DA1458X,nordic推出的nrf51822和nrf52832在架构和开发商都有自己独特的地方.这几颗产品都是蓝牙低功耗芯片.DA1458X使用OTP硬件架 ...
- Python并发(二)
并发是指一次处理多件事,而并行是指一次做多件事.二者不同,但互相有联系.打个比方:像Python的多线程,就是并发,因为Python的解释器GIL是线程不安全的,一次只允许执行一个线程的Python字 ...
- configurationChanges
在Android中每次屏幕的切换动会重启Activity,所以应该在Activity销毁前保存当前活动的状态,在Activity再次Create的时候载入配置.在activity加上android:c ...
- 大咖分享 | 一文解锁首届云创大会干货——上篇(文末附演讲ppt文件免费下载)
日,第一届网易云创大会在杭州国际博览中心举办,本次大会由杭州滨江区政府和网易主办,杭州市两创示范工作领导小组办公室协办,网易云承办,以"商业匠心.技术创新"为主题,致力于打通技术创 ...
- foy: 轻量级的基于 nodejs 的通用 build 工具
npm 的 scripts 下写的命令太多就很容易很乱,各种第三方轮子都只能解决一部分问题,总感觉不是很好用,想找个类似 make 的工具只能找到 jake, 可是 jake 的 API 太老,居然很 ...
- js常见的方法
Ajax请求 jquery ajax函数 我自己封装了一个ajax的函数,代码如下: var Ajax = function(url, type success, error) { $.ajax({ ...
- 12章 应用spary制作高级网页组件
比较复杂的用户界面,tab面板 伸缩面板和折叠面板等组件. tab面板和菜单实际上分为两种 一种是切换各个tab页中的内容时并不刷新浏览器窗口,仅仅是被隐藏起来了 利用ja ...
- TOJ 3581: 最简IPv6表示
3581: 最简IPv6表示 Time Limit(Common/Java):1000MS/3000MS Memory Limit:65536KByteTotal Submit: 121 ...
- PHP魔术函数、魔术常量、预定义常量
一.魔术函数(13个) 1.__construct() 实例化对象时被调用, 当__construct和以类名为函数名的函数同时存在时,__construct将被调用,另一个不被调用. 2.__des ...
- POJ 2353 Ministry
Ministry Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4220 Accepted: 1348 Specia ...