K-th Number
区间第K大
#include <stdio.h>
#include <algorithm>
#include <vector>
#include <iostream>
using namespace std;
typedef long long ll;
inline int read()
{
int x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
} /********************************************************************/ const int maxn = 1e5+;
int n, m, cnt, root[maxn], a[maxn], x, y, k; struct node{
int l, r, sum;
}T[maxn*]; vector<int>v;
int getid(int x){return lower_bound(v.begin(), v.end(), x) - v.begin() + ;} //离散化 void update(int l, int r, int &x, int y, int pos){
T[++cnt] = T[y], T[cnt].sum++, x = cnt;
if(l == r) return ;
int mid = (l+r)>>;
if(mid >= pos) update(l, mid, T[x].l, T[y].l, pos);
else update(mid+, r, T[x].r, T[y].r, pos);
} int query(int l, int r, int x, int y, int k){
if(l == r) return l;
int mid = (l+r)>>;
int sum = T[T[y].l].sum - T[T[x].l].sum;
if(sum >= k) return query(l, mid, T[x].l, T[y].l, k);
else return query(mid+, r, T[x].r, T[y].r, k-sum);
} int main(){
n = read(); m = read();
for(int i = ;i <= n;i++){
a[i] = read();
v.push_back(a[i]);
}
//离散化
sort(v.begin(), v.end());
v.erase(unique(v.begin(), v.end()), v.end());
//unique 去重
for(int i = ;i <= n;i++) update(, n, root[i], root[i-], getid(a[i]));
for(int i = ;i <= m;i++){
x = read(); y = read(); k = read();
printf("%d\n", v[query(, n, root[x-], root[y], k) - ]); //离散化回来
}
return ;
}
K-th Number的更多相关文章
- ACM-ICPC 2018 沈阳赛区网络预赛 K. Supreme Number
A prime number (or a prime) is a natural number greater than 11 that cannot be formed by multiplying ...
- [POJ2104] K – th Number (可持久化线段树 主席树)
题目背景 这是个非常经典的主席树入门题--静态区间第K小 数据已经过加强,请使用主席树.同时请注意常数优化 题目描述 如题,给定N个正整数构成的序列,将对于指定的闭区间查询其区间内的第K小值. 输入输 ...
- ACM-ICPC 2018 沈阳赛区网络预赛 K Supreme Number(规律)
https://nanti.jisuanke.com/t/31452 题意 给出一个n (2 ≤ N ≤ 10100 ),找到最接近且小于n的一个数,这个数需要满足每位上的数字构成的集合的每个非空子集 ...
- Count the number of possible triangles
From: http://www.geeksforgeeks.org/find-number-of-triangles-possible/ Given an unsorted array of pos ...
- 大数据热点问题TOP K
1单节点上的topK (1)批量数据 数据结构:HashMap, PriorityQueue 步骤:(1)数据预处理:遍历整个数据集,hash表记录词频 (2)构建最小堆:最小堆只存k个数据. 时间复 ...
- Codeforces Round #350 (Div. 2) F. Restore a Number 模拟构造题
F. Restore a Number Vasya decided to pass a very large integer n to Kate. First, he wrote that num ...
- C++经典编程题#1:含k个3的数
总时间限制: 1000ms 内存限制: 65536kB 描述 输入两个正整数 m 和 k,其中1 < m < 100000,1 < k < 5 ,判断 m 能否被19整除, ...
- K Best(最大化平均数)_二分搜索
Description Demy has n jewels. Each of her jewels has some value vi and weight wi. Since her husband ...
- LintCode-Kth Prime Number.
Design an algorithm to find the kth number such that the only prime factors are 3, 5, and 7. The eli ...
- POJ3111 K Best
Description Demy has n jewels. Each of her jewels has some value vi and weight wi. Since her husband ...
随机推荐
- POJ 2063 Investment (完全背包)
A - Investment Time Limit:1000MS Memory Limit:30000KB 64bit IO Format:%I64d & %I64u Subm ...
- 深入理解JVM - Java内存模型与线程 - 第十二章
Java内存模型 主内存与工作内存 Java内存模型主要目标:定义程序中各个变量的访问规则,即在虚拟机中将变量存储到内存和从内存中取出变量这样的底层细节.此处的变量(Variable)与Java编程中 ...
- JavaScript中函数的无限次运算问题
开博客有一段时间了,一直没动笔,原因是确实没看到什么值得写的内容.直到今天在司徒正美的博客里看到一个问题. http://www.cnblogs.com/rubylouvre/archive/2012 ...
- 【MFC】动态创建CMFCToolbar图标不显示问题
最近遇到一个问题,需要动态的从xml文件读取一系列图标文件,加载到一个toolbar中,由于使用的是vs2008 with sp1 feature pack,自然想到用CMFCToolbar来做,思路 ...
- 第十四章-MySQL
1 安装 MySQL常见的版本 GA: 广泛使用的版本 RC: 最接近正式版本 Alpha和Bean: 内测版本和公测版本 有两种安装方式: 安装包和压缩包 1) 安装msi文件 2) 解压zip文件 ...
- zepto.fullpage
内容来自:颜海镜 <!DOCTYPE html> <html lang="en"> <head> <meta charset=" ...
- poj1821 Fence(单调队列优化dp)
地址 一排N个木板,M个工匠站在不同位置$S_i$,每个人可以粉刷覆盖他位置的.最长长度为$L_i$木板段,每刷一个有$P_i$报酬.同一木板只刷一次.求最大报酬. 根据每个人的位置dp,设$f[i] ...
- 11g dataguard 类型、保护模式、服务
一. Dataguard中的备库分为物理备库和逻辑备库及快照备库 备库是主库的一致性拷贝,使用一个主库的备份可以创建多到30个备库,将其加入到dataguard环境中,创建成功后,dataguard通 ...
- BZOJ3165:[HEOI2013]Segment
浅谈标记永久化:https://www.cnblogs.com/AKMer/p/10137227.html 题目传送门:https://www.lydsy.com/JudgeOnline/proble ...
- 数据库关键字 (Oracle, SQL Server, DB2)
Oracle SQL Server DB2 ! @@IDENTITY DETERMINISTIC & ADD DISALLOW ( ALL DISCONNECT ) ...