主席树K-th Number
/*K-th Number
Time Limit: 20000MS Memory Limit: 65536K
Total Submissions: 44535 Accepted: 14779
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*/
#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
int ls[8000008],rs[8000008],sum[8000008],root[100008],num[100009],n,m,hash[100009];
int tmp,size,a[100008];
void jia(int l,int r,int x,int &y,int v)
{
y=++size;
sum[y]=sum[x]+1;
if(l==r)
return;
ls[y]=ls[x];
rs[y]=rs[x];
int mid=(l+r)>>1;
if(v<=mid)
jia(l,mid,ls[x],ls[y],v);
else
jia(mid+1,r,rs[x],rs[y],v);
return;
}
int xun(int L,int R,int V)
{
int x=root[L-1],y=root[R],l=1,r=tmp,mid=(l+r)/2;
for(;l!=r;)
if(sum[ls[y]]-sum[ls[x]]>=V)
{
x=ls[x];
y=ls[y];
r=mid;
mid=(l+r)>>1;
}
else
{
V-=sum[ls[y]]-sum[ls[x]];
x=rs[x];
y=rs[y];
l=mid+1;
mid=(l+r)>>1;
}
return l;
}
int main()
{
scanf("%d%d",&n,&m);
for(int i=0;i<n; i++)
{
scanf("%d",&num[i]);
a[i+1]=num[i];
}
sort(num,num+n);
hash[++tmp]=num[0];
for(int i=1;i<n;i++)
if(hash[tmp]!=num[i])
hash[++tmp]=num[i];
for(int i=1;i<=n;i++)
jia(1,tmp,root[i-1],root[i],lower_bound(hash+1,hash+n+1,a[i])-hash);
for(int i=0;i<m;i++)
{
int l,r,v;
scanf("%d%d%d",&l,&r,&v);
printf("%d\n",hash[xun(l,r,v)]);
}
return 0;
}
主席树K-th Number的更多相关文章
- 【静态主席树】POJ2104-K-th Number
求区间第k大.裸线段树. 莫队版本:☆ #include<iostream> #include<cstdio> #include<cstring> #include ...
- 可持久化线段树(主席树)快速简洁教程 图文并茂 保证学会。kth number例题
如果学不会也不要打我. 假设你会线段树 开始! --- 主席树也叫可持久化线段树 顾名思义,它能够保存线段树在每个时刻的版本. 什么叫每个时刻的版本?你可能对一棵普通线段树进行各种修改,这每种样子就是 ...
- bzoj3932 / P3168 [CQOI2015]任务查询系统(主席树+差分)
P3168 [CQOI2015]任务查询系统 看到第k小,就是主席树辣 对于每一段任务(a,b,k),在版本a的主席树+k,版本b+1的主席树-k 同一时间可能有多次修改,所以开个vector存操作, ...
- 【POJ】2104 K-th Number(区间k大+主席树)
http://poj.org/problem?id=2104 裸题不说.主席树水过. #include <cstdio> #include <iostream> #includ ...
- POJ 2104 K-th Number 主席树(区间第k大)
题目链接: http://poj.org/problem?id=2104 K-th Number Time Limit: 20000MSMemory Limit: 65536K 问题描述 You ar ...
- HDU 2665 Kth number(主席树静态区间第K大)题解
题意:问你区间第k大是谁 思路:主席树就是可持久化线段树,他是由多个历史版本的权值线段树(不是普通线段树)组成的. 具体可以看q学姐的B站视频 代码: #include<cmath> #i ...
- A - 低阶入门膜法 - K-th Number (主席树查询区间第k小)
题目链接:https://cn.vjudge.net/contest/284294#problem/A 题目大意:主席树查询区间第k小. 具体思路:主席树入门. AC代码: #include<i ...
- POJ 2104:K-th Number(主席树静态区间k大)
题目大意:对于一个序列,每次询问区间[l,r]的第k大树. 分析: 主席树模板题 program kthtree; type point=record l,r,s:longint; end; var ...
- [POJ2104] K – th Number (可持久化线段树 主席树)
题目背景 这是个非常经典的主席树入门题--静态区间第K小 数据已经过加强,请使用主席树.同时请注意常数优化 题目描述 如题,给定N个正整数构成的序列,将对于指定的闭区间查询其区间内的第K小值. 输入输 ...
- POJ 2104 K-th Number ( 求取区间 K 大值 || 主席树 || 离线线段树)
题意 : 给出一个含有 N 个数的序列,然后有 M 次问询,每次问询包含 ( L, R, K ) 要求你给出 L 到 R 这个区间的第 K 大是几 分析 : 求取区间 K 大值是个经典的问题,可以使用 ...
随机推荐
- Web Services使用SOAP Header
在Web Services方法进行通信使用SOAP遵循标准的SOAP格式,该格式的一部分是在XML文档中编码的数据.XML文档包含一个Envelope根元素(由必需的Body元素和可选的Header元 ...
- javascript 之 扩展对象 jQuery.extend
在JQuery的API手册中,extend方法挂载在JQuery 和 JQuery.fn两个不同的对象上,但在JQuery内部代码实现的是相同的,只是功能各不相同. 官方解释: jQuery.exte ...
- core直接获取报异常数据
报异常直接跳转到/Home/Error [ResponseCache(Duration = , Location = ResponseCacheLocation.None, NoStore = tru ...
- 使用VS2012编译和使用C++ STL(STLport)
使用VS2012编译和使用C++ STL(STLport) http://cstriker1407.info/blog/use-vs2012-to-compile-and-use-the-c-stl- ...
- python之函数基本使用
函数的定义: 函数是一段具有特定功能的.可重用的语句组,用函数名来表示并通过函数名进行功能调用. 使用函数主要有两个目的:降低编程难度和代码重用. python定义一个函数是通过使用def保留字的方式 ...
- R_基础_01
R语言介绍:R是一种区分大小写的解释型语言.R中有多种数据类型,包括向量.矩阵.数据框(与数据集类似)以及列表(各种对象的集合),广泛用于数据统计. R的特点:一次交互式会话期间的所有数据对象都被保存 ...
- CSS选取第一个、最后一个、偶数、奇数、第n个标签元素
1.first-child first-child表示选择列表中的第一个标签.例如:li:first-child{background:#fff} 2.last-child last-child表示选 ...
- HibernateTemplate的queryForList(sql)用法
转自:https://blog.csdn.net/huanghaijin/article/details/7763580 List<User> list = jdbcTemplate.q ...
- javascript_09-数组
数组 //数组 // var array = new Array(); // array[0]="zs"; // array[1]="ls"; var name ...
- Linux误删python导致yum不可用,删除重装方法。
Linux 系统为 CentOS Linux release 7.4.1708 手贱.手贱.手贱 删了python 导致yum不可用.百度一大圈,重装yum和python后,老是报各种各样的错.历经磨 ...