Poj 2104区间第k大(归并树)
K-th Number
Time Limit: 20000MS Memory Limit: 65536K Total Submissions: 36890 Accepted: 11860 Case Time Limit: 2000MS 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 3Sample Output
5
6
3Hint
This problem has huge input,so please use c-style input(scanf,printf),or you may got time limit exceed.
/*************************************************************************
> File Name: 2104.cpp
> Author: Stomach_ache
> Mail: sudaweitong@gmail.com
> Created Time: 2014年08月02日 星期六 19时25分26秒
> Propose:
************************************************************************/ #include <cmath>
#include <string>
#include <cstdio>
#include <vector>
#include <fstream>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std; #define lson(x) (x<<1)
#define rson(x) ((x<<1)|1)
const int maxn = ;
int n, m;
int a[maxn], nums[maxn];
vector<int> dat[maxn<<]; void build(int o, int l, int r) {
if (r - l == ) {
dat[o].push_back(a[l]);
} else {
int mid = (l + r) / ;
build(lson(o), l, mid);
build(rson(o), mid, r);
dat[o].resize(r-l);
merge(dat[lson(o)].begin(), dat[lson(o)].end(), dat[rson(o)].begin(), dat[rson(o)].end(), dat[o].begin());
}
} int query(int L, int R, int x, int o, int l, int r) {
if (l >= R || r <= L) return ;
else if (l >= L && r <= R) return upper_bound(dat[o].begin(), dat[o].end(), x) - dat[o].begin();
else {
int md = (l + r) / ;
int lc = query(L, R, x, lson(o), l, md);
int rc = query(L, R, x, rson(o), md, r);
return lc + rc;
}
} int main(void) {
while (~scanf("%d %d", &n, &m)) {
for (int i = ; i < n; i++) scanf("%d", a + i);
build(, , n);
sort(a, a + n);
while (m--) {
int l, r, k;
scanf("%d %d %d", &l, &r, &k);
l--; r--;
int lb = -, ub = n-;
while (ub - lb > ) {
int md = (lb + ub) / ;
int c = query(l, r+, a[md], , , n);
if (c >= k) ub = md;
else lb = md;
}
printf("%d\n", a[ub]);
}
} return ;
}
Poj 2104区间第k大(归并树)的更多相关文章
- POJ 2104 区间第k大(主席树)
题目链接:http://poj.org/problem?id=2104 题目大意:给定还有n个数的序列,m个操作,每个操作含有l,r,k,求区间[l,r]第k大 解题思路:线段树只能维护序列的最大值最 ...
- HDU2665 求区间第K大 主席树
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=2665 代码: //#include<bits/stdc++.h> #include< ...
- POJ-2104-K-th Number(区间第K大+主席树模板题)
Description You are working for Macrohard company in data structures department. After failing your ...
- POJ 2104 K-th Number 主席树(区间第k大)
题目链接: http://poj.org/problem?id=2104 K-th Number Time Limit: 20000MSMemory Limit: 65536K 问题描述 You ar ...
- poj 2104 主席树(区间第k大)
K-th Number Time Limit: 20000MS Memory Limit: 65536K Total Submissions: 44940 Accepted: 14946 Ca ...
- POJ 2104 && POJ 2761 (静态区间第k大,主席树)
查询区间第K大,而且没有修改. 使用划分树是可以做的. 作为主席树的入门题,感觉太神奇了,Orz /* *********************************************** ...
- POJ 2104 HDU 2665 主席树 解决区间第K大
两道题都是区间第K大询问,数据规模基本相同. 解决这种问题, 可以采用平方划分(块状表)复杂度也可以接受,但是实际表现比主席树差得多. 这里大致讲一下我对主席树的理解. 首先,如果对于某个区间[L,R ...
- 静态区间第k大(归并树)
POJ 2104为例 思想: 利用归并排序的思想: 建树过程和归并排序类似,每个数列都是子树序列的合并与排序. 查询过程,如果所查询区间完全包含在当前区间中,则直接返回当前区间内小于所求数的元素个数, ...
- 【POJ】【2104】区间第K大
可持久化线段树 可持久化线段树是一种神奇的数据结构,它跟我们原来常用的线段树不同,它每次更新是不更改原来数据的,而是新开节点,维护它的历史版本,实现“可持久化”.(当然视情况也会有需要修改的时候) 可 ...
随机推荐
- windows环境下运行Elasticsearch
1.Elasticsearch下载地址:https://github.com/medcl/elasticsearch-rtf 直接下载ZIP包: 2.配置JAVA环境 jdk64位地址:jdk-win ...
- 数论专场 Day9 部分题解
// 2019年西电暑期集训 // [7月9日]基础数论:https://cn.vjudge.net/contest/309097 A - Visible Lattice Points 题目大意: 平 ...
- PAT甲级——A1105 Spiral Matrix【25】
This time your job is to fill a sequence of N positive integers into a spiral matrix in non-increasi ...
- Python学习day24-面向对象的三大特征之继承
figure:last-child { margin-bottom: 0.5rem; } #write ol, #write ul { position: relative; } img { max- ...
- wps中,怎么快速查看xls中隐藏的图片
步骤: 页面布局->选择窗格 即可在右方看到"文档中的对象“
- pandas一些基本操作(DataFram和Series)_4
import numpy as np;import pandas as pd;kill_num=pd.Series([10,12,8,5,0,2,6])#击杀数量#青铜1200-2000#白银2001 ...
- 2018-10-19-C#-序列类为-xml-可以使用的特性大全
title author date CreateTime categories C# 序列类为 xml 可以使用的特性大全 lindexi 2018-10-19 9:9:47 +0800 2018-6 ...
- 【深度学习】CNN 中 1x1 卷积核的作用
[深度学习]CNN 中 1x1 卷积核的作用 最近研究 GoogLeNet 和 VGG 神经网络结构的时候,都看见了它们在某些层有采取 1x1 作为卷积核,起初的时候,对这个做法很是迷惑,这是因为之前 ...
- webpack:Cannot find module 'extract-text-webpack-plugin'
问题: 在终端中使用此命令安装了extract-text-webpack-plugin,npm install -g extract-text-webpack-plugin并在webpack.conf ...
- 为互联网业务而生:阿里云全球首发云Cassandra服务!
引言:十年沉淀.全球宽表排名第一.阿里云首发云Cassandra服务 ApsaraDB for Cassandra是基于开源Apache Cassandra,融合阿里云数据库DBaaS能力的分布式No ...