区间第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的更多相关文章

  1. 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 ...

  2. [POJ2104] K – th Number (可持久化线段树 主席树)

    题目背景 这是个非常经典的主席树入门题--静态区间第K小 数据已经过加强,请使用主席树.同时请注意常数优化 题目描述 如题,给定N个正整数构成的序列,将对于指定的闭区间查询其区间内的第K小值. 输入输 ...

  3. ACM-ICPC 2018 沈阳赛区网络预赛 K Supreme Number(规律)

    https://nanti.jisuanke.com/t/31452 题意 给出一个n (2 ≤ N ≤ 10100 ),找到最接近且小于n的一个数,这个数需要满足每位上的数字构成的集合的每个非空子集 ...

  4. Count the number of possible triangles

    From: http://www.geeksforgeeks.org/find-number-of-triangles-possible/ Given an unsorted array of pos ...

  5. 大数据热点问题TOP K

    1单节点上的topK (1)批量数据 数据结构:HashMap, PriorityQueue 步骤:(1)数据预处理:遍历整个数据集,hash表记录词频 (2)构建最小堆:最小堆只存k个数据. 时间复 ...

  6. 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 ...

  7. C++经典编程题#1:含k个3的数

    总时间限制:  1000ms 内存限制:  65536kB 描述 输入两个正整数 m 和 k,其中1 < m < 100000,1 < k < 5 ,判断 m 能否被19整除, ...

  8. K Best(最大化平均数)_二分搜索

    Description Demy has n jewels. Each of her jewels has some value vi and weight wi. Since her husband ...

  9. 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 ...

  10. POJ3111 K Best

    Description Demy has n jewels. Each of her jewels has some value vi and weight wi. Since her husband ...

随机推荐

  1. Contiki进程间的交互

    之前都是从各个模块开始看起,从底层开始看起.应该改变一下思路,从高往下看,站得高看得远. 一.Main函数 源码:contiki-release-2-7\platform\stm32test\cont ...

  2. POJ 3071 Football:概率dp

    题目链接:http://poj.org/problem?id=3071 题意: 给定n,有2^n支队伍参加足球赛. 给你所有的p[i][j],表示队伍i打败队伍j的概率. 淘汰赛制.第一轮(1,2)两 ...

  3. 从TS流到PAT和PMT

    转自:https://blog.csdn.net/rongdeguoqian/article/details/18214627 一 从TS流开始 最近开始学习数字电视机顶盒的开发,从MPEG-2到DV ...

  4. [acm]HDOJ 2673 shǎ崽 OrOrOrOrz

    题目地址: http://acm.hdu.edu.cn/showproblem.php?pid=2673 拍两次序,交替输出 #include<iostream> #include< ...

  5. [acm]HDOJ 3082 Simplify The Circuit

    题目地址: http://acm.hdu.edu.cn/showproblem.php?pid=3082 字符串处理+并联电阻公式 //11481261 2014-08-18 16:52:47 Acc ...

  6. bzoj 4319 Suffix reconstruction —— 贪心构造

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4319 思维还是不行...这样的构造都没思路... 首先,我们可以按 rank 的顺序从小到大 ...

  7. Elasticsearch官方安装

    Installationedit Elasticsearch requires at least Java 8. Specifically as of this writing, it is reco ...

  8. Microsoft SQL Server 数据库

    1. master 数据库 master 数据库记录 SQL Server 系统的所有系统级别信息.它记录所有的登录帐户和系统配置设置.master 数据库是这样一个数据库,它记录所有其它的数据库,其 ...

  9. qt 安装编译

    项目编译的时候提示 cannot find -lGL sudo apt-get install libgl1-mesa-dev

  10. TripAdvisor architecture 2011/06

    http://highscalability.com/blog/2011/6/27/tripadvisor-architecture-40m-visitors-200m-dynamic-page-vi ...