【POJ】2104 K-th Number(区间k大+主席树)

时间:2023-02-12 23:50:03

http://poj.org/problem?id=2104

裸题不说。主席树水过。

#include <cstdio>
#include <iostream>
#include <algorithm>
using namespace std;
#define dbg(x) cout << #x << "=" << x << endl
#define read(x) x=getint()
#define for1(i, a, n) for(int i=a; i<=(n); ++i)
#define MID (l+r)>>1
inline int getint() { char c; int ret=0, k=1; for(c=getchar(); c<'0' || c>'9'; c=getchar()) if(c=='-') k=-1; for(; c>='0' && c<='9'; c=getchar()) ret=ret*10+c-'0'; return ret*k; } const int N=100010;
int id[N], a[N], b[N], n, m, root[N], cnt, key;
struct node { int l, r, s; } T[N*50];
void update(const int &l, const int &r, int &pos) {
T[++cnt]=T[pos]; pos=cnt; ++T[pos].s;
if(l==r) return;
int m=MID;
if(key<=m) update(l, m, T[pos].l); else update(m+1, r, T[pos].r);
}
int query(const int &l, const int &r, const int &x, const int &y, const int &k) {
if(l==r) return l;
int m=MID, s=T[T[y].l].s-T[T[x].l].s;
if(k<=s) return query(l, m, T[x].l, T[y].l, k); else return query(m+1, r, T[x].r, T[y].r, k-s);
}
bool cmp(const int &x, const int &y) { return a[x]<a[y]; }
int main() {
read(n); read(m);
for1(i, 1, n) read(a[i]), id[i]=i;
sort(id+1, id+1+n, cmp);
for1(i, 1, n) b[id[i]]=i;
for1(i, 1, n) {
root[i]=root[i-1]; key=b[i];
update(1, n, root[i]);
}
int x, y;
while(m--) {
read(x); read(y); read(key);
printf("%d\n", a[id[query(1, n, root[x-1], root[y], key)]]);
}
return 0;
}

Description

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

Source

Northeastern Europe 2004, Northern Subregion

【POJ】2104 K-th Number(区间k大+主席树)的更多相关文章

  1. 计蒜客 38229&period;Distance on the tree-1&period;树链剖分&lpar;边权&rpar;&plus;可持久化线段树&lpar;区间小于等于k的数的个数&rpar;&plus;离散化&plus;离线处理 or 2&period;树上第k大&lpar;主席树&rpar;&plus;二分&plus;离散化&plus;在线查询 &lpar;The Preliminary Contest for ICPC China Nanchang National Invitational 南昌邀请赛网络赛&rpar;

    Distance on the tree DSM(Data Structure Master) once learned about tree when he was preparing for NO ...

  2. POJ 2104:K-th Number(主席树静态区间k大)

    题目大意:对于一个序列,每次询问区间[l,r]的第k大树. 分析: 主席树模板题 program kthtree; type point=record l,r,s:longint; end; var ...

  3. POJ 2104&lpar;K-th Number-区间第k大-主席树&rpar;

    K-th Number Time Limit: 20000MS   Memory Limit: 65536K Total Submissions: 31790   Accepted: 9838 Cas ...

  4. POJ 2104 求序列里第K大 主席树裸题

    给定一个n的序列,有m个询问 每次询问求l-r 里面第k大的数字是什么 只有询问,没有修改 可以用归并树和划分树(我都没学过..囧) 我是专门冲着弄主席树来的 对主席树的建树方式有点了解了,不过这题为 ...

  5. POJ 2104:K-th Number(整体二分)

    http://poj.org/problem?id=2104 题意:给出n个数和m个询问求区间第K小. 思路:以前用主席树做过,这次学整体二分来做.整体二分在yr大佬的指点下,终于大概懂了点了.对于二 ...

  6. Count on a tree(SPOJ COT &plus; 树上第k大 &plus; 主席树 &plus; LCA)

    题目链接:https://www.spoj.com/problems/COT/en/ 题目: 题意: 给你一棵有n个节点的树,求节点u到节点v这条链上的第k大. 思路: 我们首先用dfs进行建题目给的 ...

  7. 静态区间第K小(整体二分、主席树)

    题目链接 题解 主席树入门题 但是这里给出整体二分解法 整体二分顾名思义是把所有操作放在一起二分 想想,如果求\([1-n]\)的第\(k\)小怎么二分求得? 我们可以二分答案\(k\), \(O(n ...

  8. 【POJ 2104】 K-th Number 主席树模板题

    达神主席树讲解传送门:http://blog.csdn.net/dad3zz/article/details/50638026 2016-02-23:真的是模板题诶,主席树模板水过.今天新校网不好,没 ...

  9. POJ 2104:K-th Number 整体二分

    感觉整体二分是个很有趣的东西. 在别人的博客上看到一句话 对于二分能够解决的询问,如果有多个,那么如果支持离线处理的话,那么就可以使用整体二分了 树套树写了一天还是WA着,调得焦头烂额,所以决定学cd ...

随机推荐

  1. PB代码动态解析执行器

    当你看到VB.VFP等开发语言提供的强大的宏执行功能,是不是很羡慕呢?当你寻遍PB的帮助.关于PB开发的书籍或网站而不可得的时候,是不是感到有一丝的遗憾?如果你看到这篇文章,你应该感到振奋,因为你终于 ...

  2. 学好Javascript是有方法的

    先声明下噢,这篇文章不是自个儿写的,看着好,希望前端小孩们可以和我一起加油,大家都来借鉴借鉴吧- 首先要说明的是,咱现在不是高手,最多还是一个半桶水,算是入了JS的门. 谈不上经验,都是一些教训. 这 ...

  3. DataGuard实战1

    DataGuard实战1 -------------------------------------------2013/10/27   一.Primary数据库的配置及操作 1. 确定主库处于归档日 ...

  4. BeautifulSoup 抓取网站url

    1 # -*- coding:utf-8 -*- 2 import urlparse 3 import urllib2 4 from bs4 import BeautifulSoup 5 6 url ...

  5. UI事务重叠引发的crash

    在ios开发的世界里,通过动画来切换界面使我们早就习以为常的事情,但动画将一个原本同步执行的事务,变成一个异步事务,并由此引发了一系列的陷阱. 最近对公司产品的crashlytics报告进行了一些分析 ...

  6. HDU6278 Just h-index

    主席树+二分 每次对给定区间从1-区间长度len二分mid,查询区间内第mid大的数是不是大于等于mid.. #include <bits/stdc++.h> #define INF 0x ...

  7. ES6模板字符串之标签模板

    首先,模板字符串和标签模板是两个东西. 标签模板不是模板,而是函数调用的一种特殊形式.“标签”指的就是函数,紧跟在后面的模板字符串就是它的参数. 但是,如果模板字符串中有变量,就不再是简单的调用了,而 ...

  8. JS禁用键盘浏览器退格键

    我们在真实的项目开发中经常会使用JS 对键盘上的一些按键进行禁用,常见的比如说退格键(backspace/ 后退键),我在一个项目中就遇到过在页面编辑的时候禁用掉退格键,因为退格键会发生页面后退,这样 ...

  9. sqlserver 用一个表的值 更新另一个表

    update cas set cas.DocumentHeaderIdOfTransferredForForm = apply.Id from dbo.CaseTransfer cas join db ...

  10. 2018&period;12&period;08 codeforces 939E&period; Maximize&excl;(二分答案)

    传送门 二分答案好题. 题意简述:要求支持动态在一个数列队尾加入一个新的数(保证数列单增),查询所有子数列的 最大值减平均值 的最大值. 然而网上一堆高人是用三分做的. 我们先考虑当前的答案有可能由什 ...