主席树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 大值是个经典的问题,可以使用 ...
随机推荐
- Angular 学习笔记 (Angular 9 & ivy)
refer : https://blog.angularindepth.com/all-you-need-to-know-about-ivy-the-new-angular-engine-9cde47 ...
- 求亲篇:数据库操作,SqlHelper,增删改查
1.利用SqlHelper类 2.简单的数据绑定输出 string strSql = "select * from login"; DataTable dt = SqlHelper ...
- sqlserver exists 与 in 的区别
使用 EXISTS 方式 select * from A a where EXISTS(select b.mainInfoId from B b where b.mainInfoId=a.main ...
- Effective Java 读书笔记(五):Lambda和Stream
1 Lamdba优于匿名内部类 (1)DEMO1 匿名内部类:过时 Collections.sort(words, new Comparator<String>() { public in ...
- Asp.net Core 2.0 OpenId Connect Handler缺失Claims?
原文:https://leastprivilege.com/2017/11/15/missing-claims-in-the-asp-net-core-2-openid-connect-handler ...
- html中标签的英文含义!
html中的标签缩写的英文是什么? 标签 英文全称 含义 ul unordered lists 无序列表 li list item 列表项目 ol ordered lists 有序列表 dl d ...
- werkzeug/routing.py-Rule源码分析
Rule类主要用来定义和表示一个URL的模式.主要定义了一些关键字参数,用来改变url的行为.例如:这个url可以接收的请求方法,url的子域名,默认路径,端点名称,是否强制有斜杠在末尾等等 在最开始 ...
- 11/8 (tell tales web)
1.visual perception gestalt theory:格式塔学派是心理学重要流派之一,兴起于20世纪初的德国,又称为完形心理学.由马科斯·韦特墨.沃尔夫冈·苛勒和科特·考夫卡三位德国心 ...
- 利用shell命令分析服务器日志
在没有专业日志分析系统的情况下,我们有时需要对日志进行简单的分析,下面列出一些常用的shell命令分析日志的方法,一定要收藏 1.查看有多少个ip访问 awk '{print $1}' log_f ...
- 云计算(9)--Gossip:multicast problem
Gossip/Epidemic ptotocol 解决的问题是multicast problem Gossip 协议是电脑之间的通信协议,受启发与现实社会的流言蜚语.现代分布式系统通常用gossip协 ...