cogs930找第k小的数(k-th number)


原题链接


题解

好题。。。

终极版是bzoj3065(然而并不会)

先讲这个题。。。

维护\(n+1\)个值域线段树(用主席树),标号\(0\) ~ \(n\),第\(i\)个表示前i个的。然后区间\([l,r]\)就可以通过第\(r\)个线段树减去第\(l-1\)个线段树来得到。这里就在查询操作里把一个根改成两个,边查询边减法。。。

考虑最终减完以后的线段树(并不需要生成这个),树根>=k,就查询\(ls\),否则查询\(rs\)。然后直到叶子节点输出就可以辣!

(≧▽≦)

当然要加个离散化。


Code

// It is made by XZZ
#include<cstdio>
#include<algorithm>
#define Fname "kth"
using namespace std;
#define rep(a,b,c) for(rg int a=b;a<=c;a++)
#define drep(a,b,c) for(rg int a=b;a>=c;a--)
#define erep(a,b) for(rg int a=fir[b];a;a=nxt[a])
#define il inline
#define rg register
#define vd void
#define mid ((l+r)>>1)
typedef long long ll;
il int gi(){
rg int x=0;rg char ch=getchar();
while(ch<'0'||ch>'9')ch=getchar();
while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();
return x;
}
const int maxn=100010;
int n,m;
typedef struct node* point;
point null;
struct node{
int data;point ls,rs;
node(point _ls=null,point _rs=null,int _data=0){ls=_ls,rs=_rs,data=_data;}
};
point root[maxn];
int num[maxn],data[maxn];
il point build(int l,int r){
if(l==r)return new node;
return new node(build(l,mid),build(mid+1,r));
}
il vd copy(point&a,point b){
if(b==null)a=null;
else a=new node(b->ls,b->rs,b->data);
}
il vd Update(point&s,point now,int l,int r,int&pos){
copy(s,now);++s->data;
if(l==r)return;
if(mid<pos)Update(s->rs,now->rs,mid+1,r,pos);
else Update(s->ls,now->ls,l,mid,pos);
}
il int Query(int a,int b,int l,int r,int k){
point x=root[a],y=root[b];
while(l<r)
if(x->ls->data-y->ls->data>=k)x=x->ls,y=y->ls,r=mid;
else k-=x->ls->data-y->ls->data,x=x->rs,y=y->rs,l=mid+1;
return r;
}
int main(){
n=gi(),m=gi();
rep(i,1,n)num[i]=data[i]=gi();
sort(data+1,data+n+1);
int tot=unique(data+1,data+n+1)-data-1;
rep(i,1,n)num[i]=lower_bound(data+1,data+tot+1,num[i])-data;
null=new node;
null->ls=null->rs=null;
root[0]=build(1,tot);
rep(i,1,n)Update(root[i],root[i-1],1,tot,num[i]);
int l,r,k;
while(m--)l=gi(),r=gi(),k=gi(),printf("%d\n",data[Query(r,l-1,1,tot,k)]);
return 0;
}

cogs930找第k小的数(k-th number)的更多相关文章

  1. [Swift]LeetCode668. 乘法表中第k小的数 | Kth Smallest Number in Multiplication Table

    Nearly every one have used the Multiplication Table. But could you find out the k-th smallest number ...

  2. [LeetCode] Find K-th Smallest Pair Distance 找第K小的数对儿距离

    Given an integer array, return the k-th smallest distance among all the pairs. The distance of a pai ...

  3. 算法---数组总结篇2——找丢失的数,找最大最小,前k大,第k小的数

    一.如何找出数组中丢失的数 题目描述:给定一个由n-1个整数组成的未排序的数组序列,其原始都是1到n中的不同的整数,请写出一个寻找数组序列中缺失整数的线性时间算法 方法1:累加求和 时间复杂度是O(N ...

  4. 【COGS 1534】 [NEERC 2004]K小数 &&【COGS 930】 [河南省队2012] 找第k小的数 可持久化01Trie

    板子题,只是记得负数加fix最方便 #include <cstdio> ,N=; namespace FIFO { <<],*S=B,*T=B; #define getc() ...

  5. [LeetCode] 719. Find K-th Smallest Pair Distance 找第K小的数对儿距离

    Given an integer array, return the k-th smallest distance among all the pairs. The distance of a pai ...

  6. 719. 找出第 K 小的数对距离

    719. 找出第 K 小的数对距离 这道题其实有那么一点二分猜答案的意思,也有很多类似题目,只不过这道题确实表达的不是很清晰不容易想到,题没问题,我的问题.既然是猜答案,那么二分边界自然就是距离最大值 ...

  7. #7 找出数组中第k小的数

    「HW面试题」 [题目] 给定一个整数数组,如何快速地求出该数组中第k小的数.假如数组为[4,0,1,0,2,3],那么第三小的元素是1 [题目分析] 这道题涉及整数列表排序问题,直接使用sort方法 ...

  8. COGS 930. [河南省队2012] 找第k小的数

    题目描述 看到很短的题目会让人心情愉悦,所以给出一个长度为N的序列A1,A2,A3,...,AN, 现在有M个询问,每个询问都是Ai...Aj中第k小的数等于多少. 输入格式 第一行两个正整数N,M. ...

  9. [河南省队2012] 找第k小的数

    ★★☆   输入文件:kth.in   输出文件:kth.out   简单对比时间限制:1 s   内存限制:128 MB 题目描述 看到很短的题目会让人心情愉悦,所以给出一个长度为N的序列A1,A2 ...

随机推荐

  1. C/C++读取一行

    C语言 1. char buf[80]={0};     gets(buf);   //可以读取空格, 回车结束输入 2. char buf[10] = {0}; scanf("%[^\n] ...

  2. 连接池中的maxIdle,MaxActive,maxWait等参数详解

    转: 连接池中的maxIdle,MaxActive,maxWait等参数详解 2017年06月03日 15:16:22 阿祥小王子 阅读数:6481   版权声明:本文为博主原创文章,未经博主允许不得 ...

  3. iOS沙盒目录文件操作

    简介 沙盒(NSHomeDirectory())中总共有四个文件夹,documents.tmp.app.Library; 手动保存的文件在documents文件里; Nsuserdefaults保存的 ...

  4. 基于物理文件的HBase备份还原

    前提说明: 1.HBase数据分表,所以备份的粒度是表. 2.备份的内容为Azure的Blob存储. HBase Blob备份 备份时,需要先将表disable,以保持数据一致性. 备份的工具可以用A ...

  5. ASP.Net MVC的学习

    套种间作,也挺有意思的——近来学习感悟.DRP学习的同时,折腾了点以前不曾学习但是却很多次耳闻过的东西——Asp.Net中的MVC架构模式. 一.是什么? MVC,即(Model-View-Contr ...

  6. CVPR2018_Crafting a Toolchain for Image Restoration by Deep Reinforcement Learning

    CVPR2018_Crafting a Toolchain for Image Restoration by Deep Reinforcement Learning http://mmlab.ie.c ...

  7. Dubbo实践(十六)集群容错

    Dubbo作为一个分布式的服务治理框架,提供了集群部署,路由,软负载均衡及容错机制.下图描述了Dubbo调用过程中的对于集群,负载等的调用关系: 集群 Cluster 将Directory中的多个In ...

  8. ansible实用例子

    寻找/etc/ 名为"hosts" 递归查找 ansible webserver -m find -a ' path=/etc/ file_type=any recurse=yes ...

  9. 404 Note Found 队-Alpha9

    目录 组员情况 组员1(组长):胡绪佩 组员2:胡青元 组员3:庄卉 组员4:家灿 组员5:凯琳 组员6:翟丹丹 组员7:何家伟 组员8:政演 组员9:黄鸿杰 组员10:刘一好 组员11:何宇恒 展示 ...

  10. 优先队列之二叉堆与d-堆

    二叉堆简介 平时所说的堆,若没加任何修饰,一般就是指二叉堆.同二叉树一样,堆也有两个性质,即结构性和堆序性.正如AVL树一样,对堆的以此操作可能破坏者两个性质中的一个,因此,堆的操作必须要到堆的所有性 ...