cogs930找第k小的数(k-th number)
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)的更多相关文章
- [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 ...
- [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 ...
- 算法---数组总结篇2——找丢失的数,找最大最小,前k大,第k小的数
一.如何找出数组中丢失的数 题目描述:给定一个由n-1个整数组成的未排序的数组序列,其原始都是1到n中的不同的整数,请写出一个寻找数组序列中缺失整数的线性时间算法 方法1:累加求和 时间复杂度是O(N ...
- 【COGS 1534】 [NEERC 2004]K小数 &&【COGS 930】 [河南省队2012] 找第k小的数 可持久化01Trie
板子题,只是记得负数加fix最方便 #include <cstdio> ,N=; namespace FIFO { <<],*S=B,*T=B; #define getc() ...
- [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 ...
- 719. 找出第 K 小的数对距离
719. 找出第 K 小的数对距离 这道题其实有那么一点二分猜答案的意思,也有很多类似题目,只不过这道题确实表达的不是很清晰不容易想到,题没问题,我的问题.既然是猜答案,那么二分边界自然就是距离最大值 ...
- #7 找出数组中第k小的数
「HW面试题」 [题目] 给定一个整数数组,如何快速地求出该数组中第k小的数.假如数组为[4,0,1,0,2,3],那么第三小的元素是1 [题目分析] 这道题涉及整数列表排序问题,直接使用sort方法 ...
- COGS 930. [河南省队2012] 找第k小的数
题目描述 看到很短的题目会让人心情愉悦,所以给出一个长度为N的序列A1,A2,A3,...,AN, 现在有M个询问,每个询问都是Ai...Aj中第k小的数等于多少. 输入格式 第一行两个正整数N,M. ...
- [河南省队2012] 找第k小的数
★★☆ 输入文件:kth.in 输出文件:kth.out 简单对比时间限制:1 s 内存限制:128 MB 题目描述 看到很短的题目会让人心情愉悦,所以给出一个长度为N的序列A1,A2 ...
随机推荐
- 指令集 与 cpu
http://cache.baiducontent.com/c?m=9d78d513d9d437ab4f9d9e697c15c0116e4381132ba7a1020ca08448e2732d4050 ...
- 第04章-VTK基础(7)
[译者:这个系列教程是以Kitware公司出版的<VTK User's Guide -11th edition>一书作的中文翻译(出版时间2010年.ISBN: 978-1-930934- ...
- CentOS学习:第一天
阿里的一台ECS还有一个月到期,就趁这一个月的时间,用它来学习一下梦寐已久的CentOS. 由于历史原因,一直使用Windows环境,还从没接触过任何一种Linux系统. 在服务器端部署MySQL/T ...
- 【CSS3】自定义滚动条样式 -webkit-scrollbar
好文推荐:http://m.blog.csdn.net/article/details?id=40398177 http://www.xuanfengge.com/css3-webkit-scroll ...
- Python 多线程 start()和run()方法的区别(三)
上一篇文章最后只是简单介绍了start()方法和run()方法,这篇文章再详细地看下start()和run()的区别. 在实例调用的函数中加入打印当前线程的名字,分别用start()方法和run()方 ...
- jquery 中 attr 和 prop 的区别
问题:在jQuery引入prop方法后,什么时候使用attr,什么时候使用prop,两者区别. 判断: 对于HTML元素本身所有的固有属性,在处理的时候,使用prop方法 对于HTML元素后来我们自己 ...
- 【Mybatis】参数处理
单个参数:mybatis不会做特殊处理, #{参数名/任意名}:取出参数值. 多个参数:mybatis会做特殊处理. 多个参数会被封装成 一个map, key:param1...paramN,或者参数 ...
- 内核调试工具——strace
简介 strace常用来跟踪进程执行时的系统调用和所接收的信号. 在Linux世界,进程不能直接访问硬件设备,当进程需要访问硬件设备(比如读取磁盘文件,接收网络数据等等)时,必须由用户态模式切换至内核 ...
- 编译nginx错误:make[1]: *** [/pcre//Makefile] Error 127
--with-pcre=DIR 是设置源码目录,而不是编译安装后的目录.
- sqlplus连接半天才连上
问题现象: 某oracle数据库服务器发现使用ssh,crt连接半天1-2分钟后才返回输入密码的提示,应用人员发现使用 sys_GUID()函数获取唯一值的时候,第一次调用需要等待很长时间,但是同一s ...