[poj2104] K-th Number (主席树)
主席树
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 3
Sample Output
5
6
3
Hint
This problem has huge input,so please use c-style input(scanf,printf),or you may got time limit exceed.
题目大意:
给定n个数,询问 l ~ r 之间第k大的数。主席树裸题。
第 i 棵树保存的是前 i 个点的信息,每次询问就用树 r 减去树 l-1 ,即可得到答案。在树上直接二分,就可以少一个log。
PS: 看qsc视频学到了一种神奇的离散化方法,用vector,很方便,虽然要慢一些咯。
代码
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
const int maxn = 100000 + 5;
int n,m;
int cnt;
int a[maxn];
int rt[maxn];
vector <int>v;
inline int getid(int x){return lower_bound(v.begin(),v.end(),x) - v.begin() + 1;}
struct node{
int sum;
int ls,rs;
}nod[maxn * 40];
void update(int l,int r,int &x,int y,int pos){
nod[++cnt] = nod[y];nod[cnt].sum++;x = cnt;
if(l == r)return;
int mid = (l + r) >> 1;
if(pos <= mid)update(l,mid,nod[x].ls,nod[y].ls,pos),nod[x].rs = nod[y].rs;
else update(mid+1,r,nod[x].rs,nod[y].rs,pos),nod[x].ls = nod[y].ls;
}
int query(int l,int r,int x,int y,int k){
if(l == r)return l;
int mid = (l + r) >> 1;
int xx = nod[nod[y].ls].sum - nod[nod[x].ls].sum;
if(xx >= k)return query(l,mid,nod[x].ls,nod[y].ls,k);
else return query(mid+1,r,nod[x].rs,nod[y].rs,k - xx);
}
int main(){
scanf("%d%d",&n,&m);
for(int i = 1;i <= n;i++)scanf("%d",a+i),v.push_back(a[i]);
sort(v.begin(), v.end()),v.erase(unique(v.begin(),v.end()),v.end());
for(int i = 1;i <= n;i++)
update(1,n,rt[i],rt[i-1],getid(a[i]));
for(int i = 1;i <= m;i++){
int a,b,c;scanf("%d%d%d",&a,&b,&c);
printf("%d\n",v[query(1,n,rt[a-1],rt[b],c) - 1]);
}
return 0;
}
[poj2104] K-th Number (主席树)的更多相关文章
- 【poj2104】K-th Number 主席树
题目描述 You are working for Macrohard company in data structures department. After failing your previou ...
- poj2104 k-th number 主席树入门讲解
poj2104 k-th number 主席树入门讲解 定义:主席树是一种可持久化的线段树 又叫函数式线段树 刚开始学是不是觉得很蒙逼啊 其实我也是 主席树说简单了 就是 保留你每一步操作完成之后 ...
- poj 2104 K-th Number 主席树+超级详细解释
poj 2104 K-th Number 主席树+超级详细解释 传送门:K-th Number 题目大意:给出一段数列,让你求[L,R]区间内第几大的数字! 在这里先介绍一下主席树! 如果想了解什么是 ...
- [POJ2104] K – th Number (可持久化线段树 主席树)
题目背景 这是个非常经典的主席树入门题--静态区间第K小 数据已经过加强,请使用主席树.同时请注意常数优化 题目描述 如题,给定N个正整数构成的序列,将对于指定的闭区间查询其区间内的第K小值. 输入输 ...
- poj2104 K-th Number区间第k小值 主席树
原来主席树就是可持久化线段树啊,刚知道,,, 作为一道裸题,还是必A的,然而一开始偷懒不写离散化跪了N多遍,后来在缪大的帮助下发现了这个问题,遂A之 ——又是这种破问题,实在不想说自己了 把n个数看成 ...
- 【POJ2104】【HDU2665】K-th Number 主席树
[POJ2104][HDU2665]K-th Number Description You are working for Macrohard company in data structures d ...
- POJ2104 K-th Number[主席树]【学习笔记】
K-th Number Time Limit: 20000MS Memory Limit: 65536K Total Submissions: 51440 Accepted: 17594 Ca ...
- 主席树:POJ2104 K-th Number (主席树模板题)
K-th Number Time Limit: 20000MS Memory Limit: 65536K Total Submissions: 44952 Accepted: 14951 Ca ...
- POJ 2104 K-th Number 主席树(区间第k大)
题目链接: http://poj.org/problem?id=2104 K-th Number Time Limit: 20000MSMemory Limit: 65536K 问题描述 You ar ...
随机推荐
- CentOs5.2中PHP的升级
最近一个项目中需要使用到PHP5.2的版本,而服务器上使用了官方的yum源进行安装,默认的版本是5.1.6,需要升级.但是因为不是一个非常 正式的服务器环境,所以想通过简单的yum update一下了 ...
- MVC3/4 自定义HtmlHelper截断文本内容(截取)
在MVC目录下新建一个名为 Extersions 的文件夹,在该文件夹中新建一个截断文本类,取名为:CutOfTextExtersions 该类代码如下: using System; using S ...
- 导出Excel offer2007以上
package cn.knet.data.untils; import java.awt.Color; import java.io.File; import java.io.FileOutputSt ...
- 夺命雷公狗ThinkPHP项目之----企业网站14之文章修改页的完成
这个其实也是挺容易的,我们思路先将栏目页给遍历出来: 这里用了catTree的方法,因为我们要对遍历出来的数据进行排序的,然后来到前端进行完成列表: <!doctype html> < ...
- 五、Java基础---------if else、switch总结
在前几篇博客中主要是以笔者遇到的一些典型的题目为例子而展开的讨论,接下来几篇将是以知识点的结构进行讲述.本文主要是讲述if ()else .if() else if().switch() case 的 ...
- SSAS更改默认端口号,使用非默认端口号的时候Olap连接字符串的格式
Sql server的Analysis Service服务默认使用的是2382或2383端口,但是实际上我们可以通过配置文件手动更改SSAS使用其它端口号. 修改SSAS使用端口号的方法如下,找到你的 ...
- 通过进程检测服务时脚本文件名不要起要检测的服务名字命名 shell程序从上到下执行若定义函数或引用系统函数需先定义 kill -USR2
通过进程检测服务时脚本文件名不要起要检测的服务名字命名 kill -USR2 `cat /var/run/mysqld.pid`
- sql数据库(资料库)的基本操作
1 Character Set与Collation 任何资讯技术在处理资料的时候,如果只是单纯的数值和运算,那就不会有太复杂的问题:如果处理的资料是文字的话,就会面临世界上各种不同语言的问题.以资料库 ...
- django models数据类型
Django Models的数据类型 AutoField IntegerField BooleanField true/false CharField maxlength,必填 TextField C ...
- 【python cookbook】【字符串与文本】5.查找和替换文本
问题:对字符串中的文本做查找和替换 解决方案: 1.对于简单模式:str.replace(old, new[, max]) 2.复杂模式:使用re模块中的re.sub(匹配的模式, newstring ...