Kth number

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

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
 
 
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=; int T[N],num[N],san[N];
int ls[N*],rs[N*],sum[N*];
int tot,n,m;
void Update(int last,int p,int l,int r,int &rt){
rt=++tot;
ls[rt]=ls[last];
rs[rt]=rs[last];
sum[rt]=sum[last]+;
if(l==r) return;
int m=(l+r)>>;
if(p<=m) Update(ls[last],p,l,m,ls[rt]);
else Update(rs[last],p,m+,r,rs[rt]);
}
int Query(int ss,int tt,int l,int r,int k){
if(l==r) return l;
int m=(l+r)>>;
int cnt=sum[ls[tt]]-sum[ls[ss]];
if(k<=cnt) return Query(ls[ss],ls[tt],l,m,k);
else return Query(rs[ss],rs[tt],m+,r,k-cnt);
}
void work(){
int l,r,k;
for(int i=;i<=n;++i) {
scanf("%d",num+i);
san[i]=num[i];
}
tot=;
sort(san+,san+n+);
int cnt=unique(san+,san+n+)-san-;for(int i=;i<=n;++i)
num[i]=lower_bound(san+,san+cnt+,num[i])-san;
for(int i=;i<=n;++i) Update(T[i-],num[i],,cnt,T[i]);
while(m--) {
scanf("%d%d%d",&l,&r,&k);
int id=Query(T[l-],T[r],,cnt,k);
printf("%d\n",san[id]);
}
}
int main(){
int T;
for(scanf("%d",&T);T--;){
scanf("%d%d",&n,&m);
work();
}
}
 

最新文章

  1. Android之自定义标题
  2. ZooKeeper个人笔记Session管理
  3. Python开发程序:生产环境下实时统计网站访问日志信息
  4. Swift2.1 语法指南——错误处理
  5. treeMap and treeSet
  6. Flip Game(dfs)
  7. Silverlight学习(四) domainservice动态多条件查询
  8. C# 将List中的数据导入csv文件中
  9. 中国IT职业培训市场经历的几波浪潮,未来的浪潮又是那一波?
  10. 算法-java代码实现计数排序
  11. 使用java客户端调用redis
  12. ubuntu开机后弹出System program problem detected的解决办法
  13. [leetcode]236. Lowest Common Ancestor of a Binary Tree二叉树最近公共祖先
  14. 测试 多线程 实现 callable 带返回值
  15. spring4.0之三:@RestController
  16. Cerebro_变量名搜索插件
  17. java中的互斥锁和信号量的区别
  18. linux发布项目
  19. Python3实现机器学习经典算法(二)KNN实现简单OCR
  20. 概率dp学习记录

热门文章

  1. 【Linux常见命令】vimdiff命令
  2. mysql不同端口的连接
  3. handlebars模板引擎使用初探1
  4. NodeJs mysql 开启事务
  5. cmake安装jsoncpp
  6. c/c++获取硬盘序列号
  7. 前端——Vue.js学习总结一
  8. B. Silly Mistake Codeforces Round #600 (Div. 2)
  9. Gym 101170A Arranging Hat dp
  10. 使用 vi 命令创建一个cpp文件