HDU2665Kth number (主席树+离散)
InputThe first line is the number of the test cases.
For each test case, the first line contain two integer n and m (n, m <= 100000), indicates the number of integers in the sequence and the number of the quaere.
The second line contains n integers, describe the sequence.
Each of following m lines contains three integers s, t, k.
[s, t] indicates the interval and k indicates the kth big number in interval [s, t]OutputFor each test case, output m lines. Each line contains the kth big number.Sample Input
1
10 1
1 4 2 3 5 6 7 8 9 0
1 3 2
Sample Output
2
- 注意:题目是求第k小,可能写错了还是怎么的。
- 个人觉得既学习了主席树,也同时对离散的认识也加强了吧,表示以前没有用过unique结合lower_bound来离散操作。
- 没有格外的插入操作,不需要init和memset。但是要记住加地址符。
- 从现在开始,代码格式要更加规范,比如符号两边加空格。
- Persistent line tree ,我姑且函数起名PIT,如果知道了其英文名字再改过来。
- 每次查询时范围都是(1,sz);和普通的线段树的区别是:普通线段树从root=1开始沿下走。而主席树是沿两个root向下走,走的方向是一样的。
#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn=;
int a[maxn],b[maxn],T,n,sz,q,cnt,ql,qr,x;
int ch[maxn * ][],sum[maxn * ],rt[maxn * ];
struct PLTree
{ void build(int& now,int l,int r)
{
now = ++ cnt;
sum[now] = ;//ch[now][0],cnt[now][1]没必要跟新,后面sum会跟新。
if(l == r) return ;
int Mid = (l + r)>>;
build(ch[now][],l,Mid);
build(ch[now][],Mid + ,r);
}
void insert(int& now,int last,int l,int r,int pos)
{
now = ++ cnt;
ch[now][]=ch[last][];//假设公共,不同的部分下面再跟新。
ch[now][]=ch[last][];
sum[now] = sum[last] + ;
if(l == r) return ;
int Mid = (l+r) >> ;
if(pos <= Mid) insert(ch[now][],ch[last][],l,Mid,pos);
else insert(ch[now][],ch[last][],Mid + ,r,pos);
}
int query(int ss,int tt,int l,int r,int k)
{
if(l == r) return l;//要位置 ,不是要值
int Mid =(l + r) >> ,tmp = sum[ch[tt][]] - sum[ch[ss][]];//本身呢?
if(k <= tmp) return query(ch[ss][],ch[tt][],l,Mid,k);
else return query(ch[ss][],ch[tt][],Mid + ,r,k - tmp);
}
void work()
{
scanf("%d%d%d",&ql,&qr,&x);
int ans = query(rt[ql - ],rt[qr],,sz,x);//注意这个范围是(1,sz),和建树的时候的长度一样。
printf("%d\n",b[ans]);
}
};
PLTree P;
int main(){
scanf("%d",&T);
while(T--){
scanf("%d%d",&n,&q);
for(int i = ; i <= n;i ++) scanf("%d",&a[i]) , b[i]=a[i];
sort(b + ,b + n + );
sz = unique(b + ,b + n + )-(b + );
cnt=;
P.build(rt[],,sz);
for(int i = ;i <= n;i ++) a[i]=lower_bound(b + ,b + sz + ,a[i]) - b;//a现在是排名
for(int i = ;i <= n;i ++) P.insert(rt[i],rt[i-],,sz,a[i]);
while(q--) P.work();
}
return ;
}
HDU2665Kth number (主席树+离散)的更多相关文章
- 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-th Number Time Limit: 20000MS Memory Limit: 65536K Total Submissions: 51440 Accepted: 17594 Ca ...
- [poj2104] K-th Number (主席树)
主席树 Description You are working for Macrohard company in data structures department. After failing y ...
- poj 2104 K-th Number(主席树 视频)
K-th Number 题意: 给你一些数,让你求一个区间内,第k大的数是多少. 题解: 主席树第一题,看的qsc视频写的,戳戳戳 学到了unique函数,他的作用是:把相邻的重复的放到后面,返回值是 ...
- 主席树:POJ2104 K-th Number (主席树模板题)
K-th Number Time Limit: 20000MS Memory Limit: 65536K Total Submissions: 44952 Accepted: 14951 Ca ...
- 【POJ2104】【HDU2665】K-th Number 主席树
[POJ2104][HDU2665]K-th Number Description You are working for Macrohard company in data structures d ...
- SPOJ MKTHNUM & POJ 2104 - K-th Number - [主席树模板题]
题目链接:http://poj.org/problem?id=2104 Description You are working for Macrohard company in data struct ...
- Online Judge 2014 K-th Number -主席树
You are working for Macrohard company in data structures department. After failing your previous tas ...
随机推荐
- Django基础(一)_URLconf、Views、template、ORM
一 什么是web框架? 框架,即framework,特指为解决一个开放性问题而设计的具有一定约束性的支撑结构,使用框架可以帮你快速开发特定的系统,简单地说,就是你用别人搭建好的舞台来做表演. 对于所有 ...
- 流量分析系统---echarts模拟迁移中 ,geocoord从后台获取动态数值
由于在echarts的使用手册中说了 {Object} geoCoord (geoCoord是Object类型) ,所以不能用传统的字符串拼接或数组的方式赋值.在后台的controller中用Map& ...
- JAVA 文件转字节数组转字符串
public static void main(String[] args) throws IOException { byte[] bytes = FileUtils.readFileToByteA ...
- 玩转python主题模型程序库gensim
gensim是python下一个极易上手的主题模型程序库(topic model),网址在:http://radimrehurek.com/gensim/index.html 安装过程较为繁琐,参考h ...
- python的常用的内置函数
使用内置函数的好处:简单,快速. 1.zip():以多个序列为参数,返回元祖列表. 长度:在多个序列长度不一时,以最短的为准. 常见用途:构建多参数列表,构建字典. 2.map():在python2旧 ...
- iostream与iostream.h的区别
简单来说: .h的是标准C的头文件,没有.h的是标准C++的头文件,两种都是头文件. 造成这两种形式不同的原因,是C++的发展历史决定的,刚才正好有别的人也问这个问题,这里我再回答一下(注意vs200 ...
- spark学习5(hbase集群搭建)
第一步:Hbase安装 hadoop,zookeeper前面都安装好了 将hbase-1.1.3-bin.tar.gz上传到/usr/HBase目录下 [root@spark1 HBase]# chm ...
- gulp的安装和配置
gulp的安装和使用方法 1先是有node为前提的, 2安装淘宝镜像 2.1因为很多npm包都是国外的,所以安装起来很慢,所以我们可以利用淘宝的镜像服务器来进行安装后续的包,速度和成功率会高很多. ...
- codevs1907 方格取数 3
«问题描述:在一个有m*n 个方格的棋盘中,每个方格中有一个正整数.现要从方格中取数,使任意2 个数所在方格没有公共边,且取出的数的总和最大.试设计一个满足要求的取数算法.«编程任务:对于给定的方格棋 ...
- Codeforces Round #273 (Div. 2) A , B , C 水,数学,贪心
A. Initial Bet time limit per test 1 second memory limit per test 256 megabytes input standard input ...