主席树 - 查询某区间第 K 大
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
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
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
#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 大的更多相关文章
- 可持久化线段树(主席树)——静态区间第k大
主席树基本操作:静态区间第k大 #include<bits/stdc++.h> using namespace std; typedef long long LL; ,MAXN=2e5+, ...
- 主席树(静态区间第k大)
前言 如果要求一些数中的第k大值,怎么做? 可以先就这些数离散化,用线段树记录每个数字出现了多少次. ... 那么考虑用类似的方法来求静态区间第k大. 原理 假设现在要有一些数 我们可以对于每个数都建 ...
- 主席树入门——询问区间第k大pos2104,询问区间<=k的元素个数hdu4417
poj2104找了个板子..,但是各种IO还可以进行优化 /* 找区间[l,r]第k大的数 */ #include<iostream> #include<cstring> #i ...
- 主席树——求静态区间第k大
例题:poj2104 http://poj.org/problem?id=2104 讲解:http://blog.sina.com.cn/s/blog_6022c4720102w03t.html ht ...
- POJ 2104 【主席树】【区间第K大】
#include<stdio.h> #include<algorithm> #include<string.h> #define MAXN 100010 #defi ...
- Permutation UVA - 11525(值域树状数组,树状数组区间第k大(离线),log方,log)(值域线段树第k大)
Permutation UVA - 11525 看康托展开 题目给出的式子(n=s[1]*(k-1)!+s[2]*(k-2)!+...+s[k]*0!)非常像逆康托展开(将n个数的所有排列按字典序排序 ...
- ZOJ 2112 Dynamic Rankings(树状数组套主席树 可修改区间第k小)题解
题意:求区间第k小,节点可修改 思路:如果直接用静态第k小去做,显然我更改一个节点后,后面的树都要改,这个复杂度太高.那么我们想到树状数组思路,树状数组是求前缀和,那么我们可以用树状数组套主席树,求出 ...
- 主席树铺垫——总区间第k小
题目描述(口糊) 先给定一个长度为n的数列,然后给m次操作,每次输入b,求第b小的数. 样例输入 5 7 4 10 9 23 5 1 2 3 4 5 样例输出 4 7 9 10 23 数据范围及温馨提 ...
- 【转载】【树状数组区间第K大/小】
原帖:http://www.cnblogs.com/zgmf_x20a/archive/2008/11/15/1334109.html 回顾树状数组的定义,注意到有如下两条性质: 一,c[ans]=s ...
随机推荐
- H3C OSPF协议区域LSA发布
- document.getElementById()
使用两个for循环取json数据的时候出错: 代码简化如下: for(var a=0;a<3;a++){ for(var b=0;b<3;b++){ document.getElement ...
- linux 位操作
atomic_t 类型在进行整数算术时是不错的. 但是, 它无法工作的好, 当你需要以原子方 式操作单个位时. 为此, 内核提供了一套函数来原子地修改或测试单个位. 因为整个操作 在单步内发生, 没有 ...
- python类中的一些神奇方法
__str__:用于在print(对象)时,直接打印__str__的返回值 class Animal: def __init__(self, name): self.name = name def _ ...
- 函数闭包模拟session
根据上一个认证功能的问题 要解决的就是只需要登录一次 也就是登录一次之后的用户名跟密码可以保存下来让其他函数用-->全局变量 user_dic = {"user_name": ...
- 0009 CSS基础选择器( 标签、类、id、通配符)
typora-copy-images-to: media 第01阶段.前端基础.CSS基础选择器 CSS选择器(重点) 学习目标: 理解 能说出选择器的作用 id选择器和类选择器的区别 应用 能够使用 ...
- webapp开发之IIS进程调试
1.背景 1.当我的手机连接电脑的时候想要调试居然连接不上,之后我将项目发布之后才可以请求(同一局域网下) 2.你们不觉得发布到IIS再附加进程太烦了么?看了看网上全是这种方法,这不科学!VS已经提供 ...
- ConcurrentHashMap 原理解析
为什么要用ConcurrentHashMap HashMap线程不安全,而Hashtable是线程安全,但是它使用了synchronized进行方法同步,插入.读取数据都使用了synchronized ...
- appium启动app(android)
android Appium 启动APP至少需要5个参数 'platformVersion','deviceName'.'appPackage'.'appActivity'.'platform ...
- DEVOPS技术实践_23:判断文件下载成功作为执行条件
在实际生产中,我们经常会需要通过判断一个结果作为一个条件去执行另一个内容,比如判断一个文件是否存在,判官一个命令是否执行成功等等 现在我们选择其中一个场景进行实验,当某个目录下存在,则执行操作 1. ...