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 ...
随机推荐
- 《机器学习实战》学习笔记第七章 —— AdaBoost元算法
主要内容: 一.提升方法与AdaBoost算法的简介 二.AdaBoost算法 三.代码解释 一.提升方法与AdaBoost算法的简介 1.提升方法:从弱学习算法出发,反复学习,得到一系列弱分类器(又 ...
- POJ1625 Censored! —— AC自动机 + DP + 大数
题目链接:https://vjudge.net/problem/POJ-1625 Censored! Time Limit: 5000MS Memory Limit: 10000K Total S ...
- interceptors
<mvc:interceptors> <mvc:interceptor> <mvc:mapping path="/**"/> <bean ...
- C语言中的指针(二)
指针指向谁,就把谁的地址赋给指针,指针变量和指针指向的内存变量是不一样的.不停的给指针赋值,相当于是不断的改变指针的指向. 在开发中要避免野指针的存在,在指针使用完毕之后,记得要给指针赋值成为NULL ...
- bzoj3513
给定n个长度分别为$a_i$的木棒,问随机选择3个木棒能够拼成三角形的概率. $a_i$和$n$都在$10^5$以内 对于每一个i,我们统计比i短的边有多少组合能组成长度<=i的 用1减去这个概 ...
- CodeForces Div1: 995 D. Game(数学期望)
Allen and Bessie are playing a simple number game. They both know a function f:{0,1}n→Rf:{0,1}n→R, i ...
- PowerDesigner根据SQL文件生成PDM数据字典
当PowerDesigner不能直接连接到数据库的时候,可以用到下面这个方法生成数据字典(直接连接数据库生成数据字典在上次随笔中有介绍). 具体操作步骤截图: ↓↓↓这里选择的sql文件是从数据库导出 ...
- 【Python】File IO
1.numpy.genfromtxt() numpy.genfromtxt() CSV文件很容易被numpy类库的genfromtxt方法解析 2.
- Python3解leetcode Symmetric Tree
问题描述: Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). ...
- 面向对象——final关键字
继承的弊端:打破了封装性 解决方式:final final关键字的特点: 1.final是一个修饰符,即可以修饰类,也可以修饰方法,还可以修饰变量 2.final修饰的类不可以被继承 3.final修 ...