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 & ...
随机推荐
- Python2.7更新pip:UnicodeDecodeError: 'ascii' codec can't decode byte 0xb7 in position 7: ordinal not in range(128)
1.首先更新pip版本的时候出现.这是出现在python2.7.16出现的问题 2.进入你的pyhton目录下的Lib\mimetypes.py 打开它 3.在import下面加入这代码 if sys ...
- solr 下载 有dist目录的(6需要8)
http://archive.apache.org/dist/lucene/solr/ solr6 需要java8
- postgresql 简单入门
安装 https://www.postgresql.org/download/linux/redhat/yum install https://download.postgresql.org/pub/ ...
- CSV Data Set Config设置
Jmeter参数化常用的两种方法: 1.使用函数助手 2.CSV Data Set Config 本章主要讲解CSV Data Set Config设置 1.Filename:文件名,指保存信息的文件 ...
- df换行问题解决
df换行问题解决 df是linux下用来查磁盘空间的命令,而在使用了LVM分区或网络挂载的情况下,再用df取分区的使用率时,发现有些分区显示换行了,这样会导致通过脚本取的数据不对. [root@ ]# ...
- Python学习之二
基础语法 一.起始行 #!/usr/bin/python 或 #!/usr/bin/env python 目的是在运行python脚本的时候告诉系统我们要用Python解释器去运行py脚本 # -*- ...
- django-suit报错解决-----from suit.apps import DjangoSuitConfig
(py27) [root@test SimpletourDevops]# python manage.py makemigrationsTraceback (most recent call last ...
- Java DB访问(一) JDBC
项目说明 项目采用 maven 组织 ,jdbc 唯一的依赖就是 mysql-connector-java pom 依赖如下: <dependency> <groupId>my ...
- How To Scan QRCode For UWP (4)
QR Code的全称是Quick Response Code,中文翻译为快速响应矩阵图码,有关它的简介可以查看维基百科. 我准备使用ZXing.Net来实现扫描二维码的功能,ZXing.Net在Cod ...
- Centos 7 快速搭建IOS可用IPsec
安装 strongswan yum install -y http://ftp.nluug.nl/pub/os/Linux/distr/fedora-epel/7/x86_64/Packages/e/ ...