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

Hint

This problem has huge input,so please use c-style input(scanf,printf),or you may got time limit exceed.
 
题意 : 给你一维的一串数,询问区间第 K 大, 主席树板子题
#define ll long long
const int maxn = 1e5+5;
const int mod = 1e9+7;
const double eps = 1e-9;
const double pi = acos(-1.0);
const int inf = 0x3f3f3f3f; int n, m;
int pre[maxn];
int rank[maxn];
int cnt, ss;
int root[maxn];
struct node
{
int l, r;
int sum;
}t[maxn*20]; void init(){
cnt = 1;
root[0] = 0;
t[0].l = t[0].r = t[0].sum = 0;
} void update(int num, int &rt, int l, int r){
t[cnt++] = t[rt];
rt = cnt-1;
t[rt].sum++;
if (l == r) return;
int m = (l+r)>>1;
if (num <= m) update(num, t[rt].l, l, m);
else update(num, t[rt].r, m+1, r);
} int query(int i, int j, int k, int l, int r){
int d = t[t[j].l].sum - t[t[i].l].sum;
int m = (l+r)>>1; if (l == r) return l;
if (k <= d) return query(t[i].l, t[j].l, k, l, m);
else return query(t[i].r, t[j].r, k-d, m+1, r);
} int main() {
//freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
cin >> n >> m;
for(int i = 1; i <= n; i++){
scanf("%d", &pre[i]);
rank[i] = pre[i];
}
sort(rank+1, rank+1+n);
ss = unique(rank+1, rank+1+n)-rank;
//printf("-- %d\n", cnt);
init();
for(int i = 1; i <= n; i++){
int x = lower_bound(rank+1, rank+ss, pre[i])-rank;
root[i] = root[i-1];
update(x, root[i], 1, n);
}
int a, b, c;
for(int i = 1; i <= m; i++){
scanf("%d%d%d", &a, &b, &c);
int ans = query(root[a-1], root[b], c, 1, n);
printf("%d\n", rank[ans]);
}
return 0;
}

主席树 - 查询某区间第 K 大的更多相关文章

  1. 可持久化线段树(主席树)——静态区间第k大

    主席树基本操作:静态区间第k大 #include<bits/stdc++.h> using namespace std; typedef long long LL; ,MAXN=2e5+, ...

  2. 主席树(静态区间第k大)

    前言 如果要求一些数中的第k大值,怎么做? 可以先就这些数离散化,用线段树记录每个数字出现了多少次. ... 那么考虑用类似的方法来求静态区间第k大. 原理 假设现在要有一些数 我们可以对于每个数都建 ...

  3. 主席树入门——询问区间第k大pos2104,询问区间<=k的元素个数hdu4417

    poj2104找了个板子..,但是各种IO还可以进行优化 /* 找区间[l,r]第k大的数 */ #include<iostream> #include<cstring> #i ...

  4. 主席树——求静态区间第k大

    例题:poj2104 http://poj.org/problem?id=2104 讲解:http://blog.sina.com.cn/s/blog_6022c4720102w03t.html ht ...

  5. POJ 2104 【主席树】【区间第K大】

    #include<stdio.h> #include<algorithm> #include<string.h> #define MAXN 100010 #defi ...

  6. Permutation UVA - 11525(值域树状数组,树状数组区间第k大(离线),log方,log)(值域线段树第k大)

    Permutation UVA - 11525 看康托展开 题目给出的式子(n=s[1]*(k-1)!+s[2]*(k-2)!+...+s[k]*0!)非常像逆康托展开(将n个数的所有排列按字典序排序 ...

  7. ZOJ 2112 Dynamic Rankings(树状数组套主席树 可修改区间第k小)题解

    题意:求区间第k小,节点可修改 思路:如果直接用静态第k小去做,显然我更改一个节点后,后面的树都要改,这个复杂度太高.那么我们想到树状数组思路,树状数组是求前缀和,那么我们可以用树状数组套主席树,求出 ...

  8. 主席树铺垫——总区间第k小

    题目描述(口糊) 先给定一个长度为n的数列,然后给m次操作,每次输入b,求第b小的数. 样例输入 5 7 4 10 9 23 5 1 2 3 4 5 样例输出 4 7 9 10 23 数据范围及温馨提 ...

  9. 【转载】【树状数组区间第K大/小】

    原帖:http://www.cnblogs.com/zgmf_x20a/archive/2008/11/15/1334109.html 回顾树状数组的定义,注意到有如下两条性质: 一,c[ans]=s ...

随机推荐

  1. linux进程延迟

    #include <linux/wait.h> long wait_event_interruptible_timeout(wait_queue_head_t *q, condition, ...

  2. 关于vue-cli打包配置部署404

    在vue脚手架(vue-cli)下我很很快的就可以搭建自己的开发环境,但是我们把项目编写完后,需要进行打包上线会遇到各种问题,在根据版本问题,(vue3的版本跟之前相比少了很多配置项),下面是我用老版 ...

  3. 2018-11-5-win10-uwp-异步转同步

    title author date CreateTime categories win10 uwp 异步转同步 lindexi 2018-11-05 10:18:40 +0800 2018-2-13 ...

  4. C# 通过编程的方法在桌面创建回收站快捷方式

    基本所有的桌面都会存在回收站快捷方式,如果想要多创建几个快捷方式,点开就是回收站,请看本文的方法 在引用 Windows Script Host Object Model 这个 COM 方法之后可以使 ...

  5. dotnet 方法名 To 和 As 有什么不同

    在看到 dotnet 框架里面有很多方法里面用了 ToXx 和 AsXx 好像都是从某个类转换为另一个类,那么这两个方法命名有什么不同 在约定的方法命名里面,用 To 的方法表示从类 A 转为类 B ...

  6. Java数据库操作学习

    JDBC是java和数据库的连接,是一种规范,提供java程序与数据库的连接接口,使用户不用在意具体的数据库.JDBC类型:类型1-JDBC-ODBC桥类型2-本地API驱动类型3-网络协议驱动类型4 ...

  7. AS优化

    第一步:打开AS安装所在的位置,用记事本打开“红色框”选中的文件. 如图: 第二步:打开“studio64.exe.vmoptions”文件后修改里面的值,修改后如下: 1 2 3 4 5 6 7 8 ...

  8. 赋值、关系、逻辑运算、if、switch case

    cout << boolalpha将打印输出0转为false,1转为true 逻辑运算符: &&(与)    且      两个条件为真时结果为真 ||(或) 或   两个 ...

  9. deep Q learning小笔记

    1.loss 是什么 2. Q-Table的更新问题变成一个函数拟合问题,相近的状态得到相近的输出动作.如下式,通过更新参数 θθ 使Q函数逼近最优Q值 深度神经网络可以自动提取复杂特征,因此,面对高 ...

  10. 页面跨域与iframe通信(Blocked a frame with origin)

    项目中有个需求是在前后端分离的情况下,前台页面将后台页面加载在预留的iframe中:但是遇到了iframe和主窗口双滚动条的情况,由此引申出来了问题: 只保留单个滚动条,那么就要让iframe的高度自 ...