POJ2104(可持久化线段树)
K-th Number
| Time Limit: 20000MS | Memory Limit: 65536K | |
| Total Submissions: 58759 | Accepted: 20392 | |
| Case Time Limit: 2000MS | ||
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
Source
//2017-08-07
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define ll long long
#define mid ((l+r)>>1) using namespace std; const int N = ;
const int M = N * ;
struct node{//第i棵线段树的节点维护插入i个数字,每个区间的数字个数。
int lson, rson, sum;
}tree[M];
int root[N], arr[N], arr2[N], tot;
int n, m, q; void init(){//将原数列排序并去重
tot = ;
for(int i = ; i <= n; i++)
arr2[i] = arr[i];
sort(arr2+, arr2++n);
m = unique(arr2+, arr2++n)-arr2-;
} int getID(int x){
return lower_bound(arr2+, arr2++m, x) - arr2;
} int build(int l, int r){
int rt = tot++;
tree[rt].sum = ;
if(l != r){
tree[rt].lson = build(l, mid);
tree[rt].rson = build(mid+, r);
}
return rt;
} int update(int rt, int pos, int value){
int newroot = tot++, tmp = newroot;
tree[newroot].sum = tree[rt].sum + value;
int l = , r = m;
while(l < r){
if(pos <= mid){
tree[newroot].lson = tot++;
tree[newroot].rson = tree[rt].rson;
newroot = tree[newroot].lson;
rt = tree[rt].lson;
r = mid;
}else{
tree[newroot].rson = tot++;
tree[newroot].lson = tree[rt].lson;
newroot = tree[newroot].rson;
rt = tree[rt].rson;
l = mid+;
}
tree[newroot].sum = tree[rt].sum + value;
}
return tmp;
} int query(int lroot, int rroot, int k){
int l = , r = m;
while(l < r){
if(tree[tree[lroot].lson].sum - tree[tree[rroot].lson].sum >= k){
r = mid;
lroot = tree[lroot].lson;
rroot = tree[rroot].lson;
}else{
l = mid + ;
k -= tree[tree[lroot].lson].sum - tree[tree[rroot].lson].sum;
lroot = tree[lroot].rson;
rroot = tree[rroot].rson;
}
}
return l;
} int main()
{
while(scanf("%d%d", &n, &q)!=EOF){
for(int i = ; i <= n; i++)
scanf("%d", &arr[i]);
init();
root[n+] = build(, m);
for(int i = n; i > ; i--){
int pos = getID(arr[i]);
root[i] = update(root[i+], pos, );
}
while(q--){
int l, r, k;
scanf("%d%d%d", &l, &r, &k);
printf("%d\n", arr2[query(root[l], root[r+], k)]);
}
} return ;
}
POJ2104(可持久化线段树)的更多相关文章
- [poj2104]可持久化线段树入门题(主席树)
解题关键:离线求区间第k小,主席树的经典裸题: 对主席树的理解:主席树维护的是一段序列中某个数字出现的次数,所以需要预先离散化,最好使用vector的erase和unique函数,很方便:如果求整段序 ...
- POJ- 2104 hdu 2665 (区间第k小 可持久化线段树)
可持久化线段树 也叫函数式线段树也叫主席树,其主要思想是充分利用历史信息,共用空间 http://blog.sina.com.cn/s/blog_4a0c4e5d0101c8fr.html 这个博客总 ...
- 【可持久化线段树】POJ2104 查询区间第k小值
K-th Number Time Limit: 20000MS Memory Limit: 65536K Total Submissions: 61284 Accepted: 21504 Ca ...
- [POJ2104] 区间第k大数 [区间第k大数,可持久化线段树模板题]
可持久化线段树模板题. #include <iostream> #include <algorithm> #include <cstdio> #include &l ...
- 主席树(可持久化线段树) 静态第k大
可持久化数据结构介绍 可持久化数据结构是保存数据结构修改的每一个历史版本,新版本与旧版本相比,修改了某个区域,但是大多数的区域是没有改变的, 所以可以将新版本相对于旧版本未修改的区域指向旧版本的该区域 ...
- PYOJ 44. 【HNSDFZ2016 #6】可持久化线段树
#44. [HNSDFZ2016 #6]可持久化线段树 统计 描述 提交 自定义测试 题目描述 现有一序列 AA.您需要写一棵可持久化线段树,以实现如下操作: A v p x:对于版本v的序列,给 A ...
- 【BZOJ-3673&3674】可持久化并查集 可持久化线段树 + 并查集
3673: 可持久化并查集 by zky Time Limit: 5 Sec Memory Limit: 128 MBSubmit: 1878 Solved: 846[Submit][Status ...
- 【BZOJ-2653】middle 可持久化线段树 + 二分
2653: middle Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 1298 Solved: 734[Submit][Status][Discu ...
- HDU 4866 Shooting(持久化线段树)
view code//第二道持久化线段树,照着别人的代码慢慢敲,还是有点不理解 #include <iostream> #include <cstdio> #include & ...
随机推荐
- solr 加载 停用/扩展词典
startup.bat 停止词典的效果
- Caffe 使用记录(一)mnist手写数字识别
1. 运行它 1. 安装caffe请参考 http://www.cnblogs.com/xuanyuyt/p/5726926.html 此例子在官网 http://caffe.berkeleyvis ...
- [Leetcode]146.LRU缓存机制
Leetcode难题,题目为: 运用你所掌握的数据结构,设计和实现一个 LRU (最近最少使用) 缓存机制.它应该支持以下操作: 获取数据 get 和 写入数据 put . 获取数据 get(key ...
- Flask-WTF
Flask-WTF 提供了简单地 WTForms 的集成. 官方文档:http://www.pythondoc.com/flask-wtf/index.html 功能 集成 wtforms. 带有 c ...
- odoo开发 相关知识点
(1)导入模块可以起别名: (2) 新的模型前端要调用显示有关联的另一个模型的相关字段 (3) 传递上下文 搜索视图打开默认按照接收的参数搜索显示: 发起端视图 上下文写法: 目标端 触发显示,搜索视 ...
- (转)使用 db2pd 命令进行监视和故障诊断
原文:https://www.ibm.com/support/knowledgecenter/zh/SSEPGG_9.7.0/com.ibm.db2.luw.admin.trb.doc/doc/c00 ...
- Maven install [WARNING] The artifact aspectj:aspectjrt:jar:1.5.4 has been relocated to org.aspectj:aspectjrt:jar:1.5.4
一.背景 最近在给项目打包的时候,在控制台老是出现一行警告:[WARNING] The artifact aspectj:aspectjrt:jar:1.5.4 has been relocated ...
- JVM Scan
1.jmap -histo pid|head -100 2.jstat -gcutil pid cycle 3.jmap -heap pid
- Chapter 2 Open Book——13
"People in this town," he muttered. "Dr. Cullen is a brilliant surgeon who could prob ...
- 高可用Hadoop平台-启航
1.概述 在上篇博客中,我们搭建了<配置高可用Hadoop平台>,接下来我们就可以驾着Hadoop这艘巨轮在大数据的海洋中遨游了.工欲善其事,必先利其器.是的,没错:我们开发需要有开发工具 ...