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 & ...
随机推荐
- 友链&&日记
上面友链,下面日记 友人链 最喜欢galgameの加藤聚聚 初三一本&&\(ACG\)姿势比我还丰厚的yx巨巨 更喜欢galgame的shadowice czx ZigZag胖胖 文文 ...
- UVA12888 【Count LCM】(莫比乌斯反演)
题意:求\(\sum_{i=1}^{n}\sum_{j=1}^{m}[gcd(i,j)==1]\) \(assume\ n<m\) \(\sum_{i=1}^{n}\sum_{j=1}^{m}[ ...
- Mac下通过 brew 安装 Apache 和 PHP
Mac 自带的是php5.6 ,这里讲一下如果要升级到php7.1需要做的. 1.安装brew https://brew.sh/(官网有提供安装命令 建议使用) ruby -e "$(cur ...
- Spring中使用StandardServletMultipartResolver进行文件上传
从Spring3.1开始,Spring提供了两个MultipartResolver的实现用于处理multipart请求,分别是:CommonsMultipartResolver和StandardSer ...
- JFrame、JPanel 、Layout开发的简单例子
写了Java这么久,居然发现想手写一个带网格袋布局的JFrame,还不记得怎么写,写了这么多代码真不敢说记得所有细节. 幸好,只要记清楚概念就能快速开发.首先,明确一下3种容器类的差别和用途: No. ...
- sql开启远程访问
我们用的是SQL Server 数据库 2008 版本,数据库配置完之后从另一台电脑访问数据库死活连接不上,提示信息如下 “ 无法连接到 *.*.*.*. 在于SQL Server建立连接时出现与网络 ...
- [原创]K8 CMS GoastGuard 密码解密工具
工具: K8 CMS GoastGuard PASS Decrypt编译: VS2012 C# (.NET Framework v4.5)组织: K8搞基大队[K8team]作者: K8拉登哥哥博客 ...
- Android Design Support Library——Navigation View
前沿 Android 从5.0开始引入了Material design元素的设计,这种新的设计语言让整个安卓的用户体验焕然一新,google在Android Design Support Librar ...
- Python基础之好玩的字符串格式化f-string格式
转自白月黑羽 Python3教程 : http://www.python3.vip/doc/tutorial/python/0010/#f-string-格式化 f-string 格式化 f-stri ...
- ES6箭头函数this指向
普通函数中的this: 1. this总是代表它的直接调用者(js的this是执行上下文), 例如 obj.func ,那么func中的this就是obj 2.在默认情况(非严格模式下,未使用 'us ...