poj 2104 (划分树模板)
Description
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 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
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
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
using namespace std;
typedef long long LL;
const int maxn = 1e5 + ;
int a[maxn], sorted[maxn];
int num[][maxn], val[][maxn]; void build(int l, int r, int ceng) {
if (l == r) return ;
int mid = (l + r) >> , same = mid - l + ;
for (int i = l ; i <= r ; i++)
if (val[ceng][i] < sorted[mid]) same--;
int ln = l, rn = mid + ;
for (int i = l ; i <= r ; i++) {
if (i == l) num[ceng][i] = ;
else num[ceng][i] = num[ceng][i - ];
if (val[ceng][i] < sorted[mid] || val[ceng][i] == sorted[mid] && same > ) {
val[ceng + ][ln++] = val[ceng][i];
num[ceng][i]++;
if (val[ceng][i] == sorted[mid]) same--;
} else val[ceng + ][rn++] = val[ceng][i];
}
build(l, mid, ceng + );
build(mid + , r, ceng + );
} int query(int ceng, int L, int R, int l, int r, int k) {
if (L == R) return val[ceng][L];
int lsum;
if (l == L) lsum = ;
else lsum = num[ceng][l - ];
int tot = num[ceng][r] - lsum;
if (tot >= k) return query(ceng + , L, (L + R) / , L + lsum, L + num[ceng][r] - , k);
else {
int lr = (L + R) / + + (l - L - lsum);
return query(ceng + , (L + R) / + , R, lr, lr + r - l + - tot - , k - tot);
}
} int main() {
int n, m, l, r, k;
while(scanf("%d%d", &n, &m) != EOF) {
for (int i = ; i <= n ; i++) {
scanf("%d", &val[][i]);
sorted[i] = val[][i];
}
sort(sorted + , sorted + n + );
build(, n, );
while(m--) {
scanf("%d%d%d", &l, &r, &k);
printf("%d\n", query(, , n, l, r, k ));
}
}
return ;
}
poj 2104 (划分树模板)的更多相关文章
- K-th Number POJ - 2104 划分树
K-th Number You are working for Macrohard company in data structures department. After failing your ...
- poj 2104 划分树
思路:裸的划分树 #include<iostream> #include<algorithm> #include<cstring> #include<cstd ...
- hdu 4417,poj 2104 划分树(模版)归并树(模版)
这次是彻底把划分树搞明确了,与此同一时候发现了模版的重要性.敲代码一个字符都不能错啊~~~ 划分树具体解释:点击打开链接 题意:求一组数列中随意区间不大于h的个数. 这个题的做法是用二分查询 求给定 ...
- POJ 2104 主席树模板题
#include <iostream> #include <cstdio> #include <algorithm> int const maxn = 200010 ...
- hdu 2665 Kth number(划分树模板)
http://acm.hdu.edu.cn/showproblem.php?pid=2665 [ poj 2104 2761 ] 改变一下输入就可以过 http://poj.org/problem? ...
- K-th Number Poj - 2104 主席树
K-th Number Poj - 2104 主席树 题意 给你n数字,然后有m次询问,询问一段区间内的第k小的数. 解题思路 这个题是限时训练做的题,我不会,看到这个题我开始是拒绝的,虽然题意清晰简 ...
- poj2104(划分树模板)
poj2104 题意 给出一个序列,每次查询一个区间,要求告诉这个区间排序后的第k个数. 分析 划分树模板,O(mlogn). 建树.根据排序之后的数组,对于一个区间,找到中点的数,将整个区间分为左右 ...
- poj2104(划分树模板)
poj2104 题意 给出一个序列,每次查询一个区间,要求告诉这个区间排序后的第k个数. 分析 划分树模板,O(mlogn). 建树.根据排序之后的数组,对于一个区间,找到中点的数,将整个区间分为左右 ...
- [poj 2104]主席树+静态区间第k大
题目链接:http://poj.org/problem?id=2104 主席树入门题目,主席树其实就是可持久化权值线段树,rt[i]维护了前i个数中第i大(小)的数出现次数的信息,通过查询两棵树的差即 ...
随机推荐
- 函数:引用file类对象及io类对象作为参数打印文本及显示文本
#include <iostream> #include <fstream> #include <cstdlib> using namespace std; voi ...
- POJ1236 tarjan
Network of Schools Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 19613 Accepted: 77 ...
- "Mon Dec 31 00:00:00 CST 2012" java日期装换 "yyyy-MM-dd"
import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import ja ...
- R语言学习笔记(十一):零碎知识点(26-30)
26--aggregate( ) 函数aggregate()对分组中的每一个变量调用tapply()函数. aggregate(a,list,f) 第二个参数必须是列表.也就是因子部分. 第三个参数即 ...
- SAN---第二网的概念
网络技术的优缺点:优点:连接能力,超强路由,管理能力,远距离缺点:低速以及高负载,强烈的软件需求,错误检测能力 SAN:storage area network(存储区域网络)--是一种基于光网的特殊 ...
- List中的FindAll用法
在泛型List中查找符合某个字段的全部数据,可以采用如下方式: //1.现将实体数据listList<ADDaAn> objDAList = db.ADDaAns.ToList(); // ...
- P1331 海战
P1331 海战 题目描述 在峰会期间,武装部队得处于高度戒备.警察将监视每一条大街,军队将保卫建筑物,领空将布满了F-2003飞机.此外,巡洋船只和舰队将被派去保护海岸线.不幸的是因为种种原因,国防 ...
- luogu4172 [WC2006]水管局长
就是用 lct 维护最小生成树 ref #include <algorithm> #include <iostream> #include <cstdio> #in ...
- 最后一片蓝海的终极狂欢-写在Win10发布前夕
作为一名Windows8.x+系统平台从业者,从工作伊始,耳边不断充斥着Windows将走向没落的言论,Win10今日晚些时候即将发布,笔者借此机会,说说自己的看法. 早在2012年的时候,IDC曾预 ...
- 【紫书】(UVa1347)Tour
继续考虑dp题目. 题意分析 其实这里只是更加仔细的做一个lrj的复读机(Orz 他分析了一个很重要的结果:如果是一个人从左到右再回来,并且每个点恰经过一次,那么等价于两个人从左到右每个点经过一次地遍 ...