pku2104
传送门:http://poj.org/problem?id=2104
题目大意:给定一个长度为N的数组{A[i]},你的任务是解决Q个询问。每次询问在A[l], A[l+1], ...... , A[r]的子区间(
     下标从1开始)内,第K大的数是多少。这里的第K大数可以理解为,将给定子数组拿出来按升序排序,其中排在第K
     个的数。注意询问操作并不更改原数组。
题解:水题,主席树回顾-----------
代码:
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<cmath>
#define maxn 100005
using namespace std;
int a[maxn],list[maxn];
int root[maxn],ls[maxn*],rs[maxn*],sum[maxn*];
int n,m,sz;
int read()
{
int x=; char ch; bool bo=;
while (ch=getchar(),ch<''||ch>'') if (ch=='-') bo=;
while (x=x*+ch-'',ch=getchar(),ch>=''&&ch<='');
if (bo) return -x; return x;
}
void ins(int l,int r,int x,int &y,int val)
{
y=++sz; sum[y]=sum[x]+;
if (l==r) return;
ls[y]=ls[x]; rs[y]=rs[x];
int mid=(l+r)>>;
if (val<=mid) ins(l,mid,ls[x],ls[y],val);
else ins(mid+,r,rs[x],rs[y],val);
}
void init()
{
n=read(); m=read();
for (int i=; i<=n; i++) list[i]=a[i]=read();
sort(list+,list++n);
for (int i=; i<=n; i++) a[i]=lower_bound(list+,list++n,a[i])-list;
for (int i=; i<=n; i++) ins(,n,root[i-],root[i],a[i]);
}
int query(int L,int R,int k)
{
int l=,r=n,x=root[L-],y=root[R];
while (l!=r)
{
int tmp=sum[ls[y]]-sum[ls[x]];
int mid=(l+r)>>;
if (k<=tmp)
{
r=mid,x=ls[x],y=ls[y];
}
else
{
l=mid+,k-=tmp,x=rs[x],y=rs[y];
}
}
return l;
}
void solve()
{
for (int i=; i<=m; i++)
{
int l=read(),r=read(),k=read();
int ans=query(l,r,k);
printf("%d\n",list[ans]);
}
}
int main()
{
init();
solve();
}
pku2104的更多相关文章
随机推荐
- hdu_3564_Another LIS(线段树+LIS)
			
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=3564 题意:给你N个数的位置.数i的位置为第i个数,比如 0 0 2,表示1插在第0个位置,此时数列为 ...
 - jq的合成事件
			
jq中有两个合成事件 hover()和toggle() 1.hover() hover方法用于模拟光标悬停事件.当光标移动到元素上时,会触发指定的第一个函数(enter),当光标移出这个元素时,会触发 ...
 - table固定宽度高度, 及overflow省略号
			
整体设置标签为:td {text-overflow: ellipsis; white-space: nowrap; overflow: hidden; } table fix设置 <table ...
 - C++读取excel特定行列中的数据
			
可以通过COM API调用才是正解,不过需要编写COM接口类,Excel对象库的接口太多了……不过可以用工具自动生成. 我最近也在用VC操作Excel,在VC中可以这样做,在任何一个cpp文件中加入下 ...
 - 认识和选用常用的几种 GPRS 模块(转)
			
源:http://blog.sina.com.cn/s/blog_4d80055a0100e8kr.html 我在这里把常见的GPRS模块分成3种: (1)GPRS DTU(GPRS数传单元,常称GP ...
 - 实例:SSH结合Easyui实现Datagrid的新增功能和Validatebox的验证功能
			
在我前面一篇分页的基础上,新增了添加功能和添加过程中的Ajax与Validate的验证功能.其他的功能在后面的博客写来,如果对您有帮助,敬请关注. 先看一下实现的效果: (1)点击添加学生信息按键后跳 ...
 - ural1682 Crazy Professor
			
Crazy Professor Time limit: 1.0 secondMemory limit: 64 MB Professor Nathan Mathan is crazy about mat ...
 - PHP :Call to undefined function mysql_connect()
			
今天配置apache ,php,mysql 的时候,一直报(Call to undefined function mysql_connect()),PHP一直连接不上数据库,从网上查,答案也都是千篇一 ...
 - mysql安装使用--2 用户管理
			
1 修改mysql.user表 添加用户 mysql> INSERT INTO mysql.user (Host,User,Password) VALUES (\'%\',\'system\', ...
 - C语言5种存储区域
			
C语言5种存储区域 转发至:http://www.mamicode.com/info-detail-927635.html 系统为了管理内存 把内存划分了几个区域 1> 栈区 栈区之中的数据在栈 ...