Kth number

Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 16941    Accepted Submission(s): 5190

Problem Description
Give you a sequence and ask you the kth big number of a inteval.
 
Input
The 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]
 
Output
For 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
 
Source
 
 
 
 
 
题意就是查询区间第K小,可持久化线段树就可以上场了。
 
没学可持久化数据结构之前,感觉这个东西是个很高大上,很厉害的东西,等到了解之后发现,就是因为问题的产生所以在原有线段树的基础上有所更新,支持查询历史版本。
如果每更新一次就新建一整棵线段树简直是丧心病狂,所以直接新开节点保存更新的那一条链就可以。
这种东西还是需要自己慢慢体会的,加油,咸鱼。
 
代码:
 //无修改区间第K小-可持久化线段树(权值线段树+可持久化)
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<bitset>
#include<cassert>
#include<cctype>
#include<cmath>
#include<cstdlib>
#include<ctime>
#include<deque>
#include<iomanip>
#include<list>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<vector>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii; const double PI=acos(-1.0);
const double eps=1e-;
const ll mod=1e9+;
const int inf=0x3f3f3f3f;
const int maxn=1e5+;
const int maxm=+;
#define ios ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define lson l,m
#define rson m+1,r int a[maxn],b[maxn],sum[maxn<<],L[maxn<<],R[maxn<<];//sum线段树里保存的值,L左儿子,R右儿子
int n,m,sz=; void build(int &rt,int l,int r)//建棵空树
{
rt=++sz;sum[rt]=;//动态开点,初始值为0,空树
if(l==r){
return ;
} int m=(l+r)>>;
build(L[rt],lson);
build(R[rt],rson);
} void update(int pr,int &rt,int l,int r,int x)
{
rt=++sz;sum[rt]=sum[pr]+;//插入序列,首先继承以前的线段树 然后直接单点+1就可以
L[rt]=L[pr];R[rt]=R[pr];
if(l==r){
return ;
} int m=(l+r)>>;
if(x<=m) update(L[pr],L[rt],lson,x);//因为右边不需要更新,所以覆盖掉左边
else update(R[pr],R[rt],rson,x);
} int query(int pr,int rt,int l,int r,int x)//查询l到r区间就是第r次插入减去第l-1次插入后的线段树的样子
{
if(l==r){
return l;
}
//因为我们建立的是权值线段树,所以直接查找就可以
int m=(l+r)>>;
int now=sum[L[rt]]-sum[L[pr]];//now为左子树新树-旧树
if(now>=x) return query(L[pr],L[rt],lson,x);
else return query(R[pr],R[rt],rson,x-now);
} int rt[maxn]; int main()
{
int t;
scanf("%d",&t);
while(t--){
scanf("%d%d",&n,&m);
sz=;
for(int i=;i<=n;i++){
scanf("%d",&a[i]);
b[i]=a[i];
}
sort(b+,b++n);//首先把值全部排序去重,用于建权值线段树,权值线段树保存的内容是值的数量。
int d=unique(b+,b++n)-(b+);
build(rt[],,d);
for(int i=;i<=n;i++){//按照序列顺序插入值
int x=lower_bound(b+,b++d,a[i])-b;
update(rt[i-],rt[i],,d,x);
}
for(int i=;i<=m;i++){
int l,r,k;
scanf("%d%d%d",&l,&r,&k);
printf("%d\n",b[query(rt[l-],rt[r],,d,k)]);
}
}
return ;
}

你来到人间,就是要看看太阳,看看外面有趣的世界。

 

HDU 2665.Kth number-可持久化线段树(无修改区间第K小)模板 (POJ 2104.K-th Number 、洛谷 P3834 【模板】可持久化线段树 1(主席树)只是输入格式不一样,其他几乎都一样的)的更多相关文章

  1. HDU 4417.Super Mario-可持久化线段树(无修改区间小于等于H的数的个数)

    Super Mario Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  2. 洛谷P3834 [模板]可持久化线段树1(主席树) [主席树]

    题目传送门 可持久化线段树1(主席树) 题目背景 这是个非常经典的主席树入门题——静态区间第K小 数据已经过加强,请使用主席树.同时请注意常数优化 题目描述 如题,给定N个正整数构成的序列,将对于指定 ...

  3. 【洛谷 P3834】 可持久化线段树1(主席树)

    题目链接 主席树=可持久化权值线段树. 如果你不会可持久化线段树,请右转 如果你不会权值线段树,请自行脑补,就是线段树维护值域里有多少个数出现. 可持久化线段树是支持查询历史版本的. 我们对每个数都进 ...

  4. 洛谷$P$2468 粟粟的书架 $[SDOI2010]$ 主席树

    正解:主席树 解题报告: 传送门! 题目大意是说,给定一个矩形,然后每次会给一个,这个大矩形中的一个小矩形,询问从小矩形中最少选多少个数字能满足它们之和大于等于给定数字$x$ 看起来很神的样子,完全不 ...

  5. ☆ [洛谷P2633] Count on a tree 「树上主席树」

    题目类型:主席树+\(LCA\) 传送门:>Here< 题意:给出一棵树.每个节点有点权.问某一条路径上排名第\(K\)小的点权是多少 解题思路 类似区间第\(K\)小,但放在了树上. 考 ...

  6. 洛谷P4559 [JSOI2018]列队 【70分二分 + 主席树】

    题目链接 洛谷P4559 题解 只会做\(70\)分的\(O(nlog^2n)\) 如果本来就在区间内的人是不用动的,区间右边的人往区间最右的那些空位跑,区间左边的人往区间最左的那些空位跑 找到这些空 ...

  7. 【洛谷 P2633】 Count on a tree(主席树,树上差分)

    题目链接 思维难度0 实现难度7 建出主席树后用两点的状态减去lca和lca父亲的状态,然后在新树上跑第\(k\)小 #include <cstdio> #include <cstr ...

  8. 主席树[可持久化线段树](hdu 2665 Kth number、SP 10628 Count on a tree、ZOJ 2112 Dynamic Rankings、codeforces 813E Army Creation、codeforces960F:Pathwalks )

    在今天三黑(恶意评分刷上去的那种)两紫的智推中,突然出现了P3834 [模板]可持久化线段树 1(主席树)就突然有了不详的预感2333 果然...然后我gg了!被大佬虐了! hdu 2665 Kth ...

  9. hdu 2665 Kth number

    划分树 /* HDU 2665 Kth number 划分树 */ #include<stdio.h> #include<iostream> #include<strin ...

随机推荐

  1. tcp/ip 学习-通过视频学习

    视频下载地址:http://down.51cto.com/zt/5518/ http://www.icoolxue.com/album/show/328 每天可以拿两个番茄钟看视频,主要目的还是了解, ...

  2. 洛谷 P1722 矩阵 II

    题目背景 usqwedf 改编系列题. 题目描述 如果你在百忙之中抽空看题,请自动跳到第六行. 众所周知,在中国古代算筹中,红为正,黑为负…… 给定一个1*(2n)的矩阵(usqwedf:这不是一个2 ...

  3. C11简洁之道:lambda表达式

    1.  定义 lambda表达式是C++11非常重要也是很常用的特性之一,来源于函数式编程的概念,也是现代编程语言的一个特点.它有如下特点: 声明式编程风格:就地匿名定义目标函数或者函数,不需要额外写 ...

  4. 【BZOJ4884】太空猫 [DP]

    太空猫 Time Limit: 1 Sec  Memory Limit: 256 MB[Submit][Status][Discuss] Description 太空猫(SpaceCat)是一款画面精 ...

  5. 人人都能掌握的Java服务端性能优化方案

    作为一个Java后端开发,我们写出的大部分代码都决定着用户的使用体验.如果我们的后端代码性能不好,那么用户在访问我们的网站时就要浪费一些时间等待服务器的响应.这就可能导致用户投诉甚至用户的流失. 关于 ...

  6. eureka服务端

    服务注册与发现——Eureka Eureka Server:服务的注册中心,负责维护注册的服务列表. Service Provider:服务提供方,作为一个Eureka Client,向Eureka ...

  7. rhel-server srpms iso

    http://ftp.redhat.com/pub/redhat/linux/enterprise/7Server/en/ ftp://ftp.pslib.cz/pub/linux/redhat-cz ...

  8. Deep Learning基础--CNN的反向求导及练习

    前言: CNN作为DL中最成功的模型之一,有必要对其更进一步研究它.虽然在前面的博文Stacked CNN简单介绍中有大概介绍过CNN的使用,不过那是有个前提的:CNN中的参数必须已提前学习好.而本文 ...

  9. 【hihocoder】sam-2

    原意是把sam那一堆做完…… 这题还是很sb的,$\sum{maxlen(s)-minlen(s)+1}$就是本质不同的子串数量 然后因为suffix link的性质,maxlen[fa[s]]=mi ...

  10. jQuery 选中tr下面的第某个td

    1.问题描述 点击 table 中的某行 tr,获取该 tr 下的第一个 td 标签下的< input type="hidden" value="92"/ ...