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. [emerg]: getpwnam(“nginx”) failed

    [root@localhost nginx-1.11.2]# /usr/local/nginx/sbin/nginx nginx: [emerg] getpwnam("nginx" ...

  2. Inno Setup 安装

    Inno Setup 安装 一.软件下载:http://www.jrsoftware.org/isdl.php#qsp 二.下载后,双击进行安装(包括inno Setup Compiler和Inno ...

  3. 【ES6】最常用的es6特性(二)

    1.for of 值遍历 for in 循环用于遍历数组,类数组或对象,ES6中新引入的for of循环功能相似,不同的是每次循环它提供的不是序号而是值. var someArray = [ &quo ...

  4. 跳转到系统设置界面 iOS

    NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString]; if ([[UIApplication sharedApp ...

  5. 如何异步的处理restful服务(基础)

    1.使用Runnable 2.使用DeferredResult 3.异步处理的一些配置 正常请求方式 package com.nxz.controller; import lombok.extern. ...

  6. 阿里云阿里免费ssl wap网站在手机微信、手机浏览器无法访问

    图片可以访问,样式无法显示 https://css.cnbuses.com/css/wap/gonggong.css 1,怀疑是开了代理访问. 关闭后还是访问空白. 2.在手机浏览器打开,提示该网站的 ...

  7. SQL Server 数据库每个表占用的空间、大小

    查看MSSQL数据库每个表占用的空间大小 sp_spaceused显示行数.保留的磁盘空间以及当前数据库中的表所使用的磁盘空间,或显示由整个数据库保留和使用的磁盘空间. 语法sp_spaceused ...

  8. 前端调用接口得到的数据跟postman跑出来的数据里数字部份不相等

    昨天碰到这样一个场景,调用后端接口返回的数据发现所有数据都是正常的,只有一个商品ID的最后两位是错的,每一个商品都是,导致无法进行商品的上下架和删除, 经过查资料发现: 浏览器解析数字的坑,一旦超出一 ...

  9. ztz11的noip模拟赛T3:评分系统

    代码: #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> ...

  10. 【Linux】文件、目录权限及归属

    访问权限: 可读(read):允许查看文件内容.显示目录列表 可写(write):允许修改文件内容,允许在目录中新建.移动.删除文件或子目录 可执行(execute):允许运行程序.切换目录 归属: ...