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 ...
随机推荐
- Contiki事件
一.事件相关数据结构 1.事件结构体 struct event_data { process_event_t ev; process_data_t data; struct process *p; } ...
- spawning cl.exe
可能很多人在安装VC 6.0后有过点击“Compile”或者“Build”后被出现的“Compiling... ,Error spawning cl.exe”错误提示给郁闷过.很多人的选择是重装,实际 ...
- JavaScript多态
function Master(){ //给动物喂食 this.feed=function(animal,food){ window.alert(animal.constructor); docume ...
- 关于跨域获取cookie问题的解决
需求是有2个域名:www.a.com和www.b.com,b.com需要获取a.com中的cookie,解决方法是这样的: 在a.com编写一个设置cookie的页面:set_cookie.php 代 ...
- P2762 [网络流24题]太空飞行计划问题(最小割)
地址 最大权闭合子图裸题,不说了吧,求方案就是把s集遍历一遍. 错误记录:dfs那块忘判断残量了,11分×1. #include<cstdio> #include<iostream& ...
- poj1637 Sightseeing tour[最大流+欧拉回路]
混合图的欧拉回路定向问题. 顺便瞎说几句,有向图定欧拉回路的充要条件是每个点入度等于出度,并且图联通.无向图的话只要联通无奇点即可. 欧拉路径的确定应该是无向图联通且奇点数0个或2个,有向图忘了,好像 ...
- Codeforces 755B. PolandBall and Game 贪心
题目大意: 有两个人轮流说单词,已经说过的单词不能再说.给出两人掌握的不同的单词,两人可能掌握相同的单词,但是这个单词也只能说一边.问在两人都是最优策略下先手是否必胜. 题解: 我们发现最优策略一定是 ...
- OGG 11g Checkpoint 详解
OGG Checkpoint 详解 定位中断的位置,下次启动从中断的位置开始恢复. 1.target 端配置: 2.一条记录对应一个replicat 一. Extract Check ...
- Session与Cookie(1)
Session session是一种位于服务端,用于存储一个会话(指打开浏览器访问某个域名,及其下面的资源,然后关掉浏览器的过程)中所需的配置信息.也就是在一个会话中,只存在一个session.对于J ...
- 如何增加新的PointT类型
博客转载自:http://www.pclcn.org/study/shownews.php?lang=cn&id=286 为了增加新的point类型,首先需要进行定义,例如: struct M ...