Give you a sequence and ask you the kth big number of a inteval.

InputThe 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]OutputFor 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
  • 注意:题目是求第k小,可能写错了还是怎么的。
  • 个人觉得既学习了主席树,也同时对离散的认识也加强了吧,表示以前没有用过unique结合lower_bound来离散操作。
  • 没有格外的插入操作,不需要init和memset。但是要记住加地址符。
  • 从现在开始,代码格式要更加规范,比如符号两边加空格。
  • Persistent line tree ,我姑且函数起名PIT,如果知道了其英文名字再改过来。
  • 每次查询时范围都是(1,sz);和普通的线段树的区别是:普通线段树从root=1开始沿下走。而主席树是沿两个root向下走,走的方向是一样的。
#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn=;
int a[maxn],b[maxn],T,n,sz,q,cnt,ql,qr,x;
int ch[maxn * ][],sum[maxn * ],rt[maxn * ];
struct PLTree
{ void build(int& now,int l,int r)
{
now = ++ cnt;
sum[now] = ;//ch[now][0],cnt[now][1]没必要跟新,后面sum会跟新。
if(l == r) return ;
int Mid = (l + r)>>;
build(ch[now][],l,Mid);
build(ch[now][],Mid + ,r);
}
void insert(int& now,int last,int l,int r,int pos)
{
now = ++ cnt;
ch[now][]=ch[last][];//假设公共,不同的部分下面再跟新。
ch[now][]=ch[last][];
sum[now] = sum[last] + ;
if(l == r) return ;
int Mid = (l+r) >> ;
if(pos <= Mid) insert(ch[now][],ch[last][],l,Mid,pos);
else insert(ch[now][],ch[last][],Mid + ,r,pos);
}
int query(int ss,int tt,int l,int r,int k)
{
if(l == r) return l;//要位置 ,不是要值
int Mid =(l + r) >> ,tmp = sum[ch[tt][]] - sum[ch[ss][]];//本身呢?
if(k <= tmp) return query(ch[ss][],ch[tt][],l,Mid,k);
else return query(ch[ss][],ch[tt][],Mid + ,r,k - tmp);
}
void work()
{
scanf("%d%d%d",&ql,&qr,&x);
int ans = query(rt[ql - ],rt[qr],,sz,x);//注意这个范围是(1,sz),和建树的时候的长度一样。
printf("%d\n",b[ans]);
}
};
PLTree P;
int main(){
scanf("%d",&T);
while(T--){
scanf("%d%d",&n,&q);
for(int i = ; i <= n;i ++) scanf("%d",&a[i]) , b[i]=a[i];
sort(b + ,b + n + );
sz = unique(b + ,b + n + )-(b + );
cnt=;
P.build(rt[],,sz);
for(int i = ;i <= n;i ++) a[i]=lower_bound(b + ,b + sz + ,a[i]) - b;//a现在是排名
for(int i = ;i <= n;i ++) P.insert(rt[i],rt[i-],,sz,a[i]);
while(q--) P.work();
}
return ;
}

最新文章

  1. AngularJS---自定义指令
  2. hiho一下 第一百零七周 Give My Text Back(微软笔试题)
  3. ElasticSearch实战使用
  4. Object C学习笔记13-Dictionary字典
  5. Oracle 建表常用数据类型的详解
  6. BZOJ2718: [Violet 4]毕业旅行
  7. Python 2.7 学习笔记 模块和包
  8. 设计模式学习一:strategyPattern
  9. 读书笔记 effective c++ Item 18 使接口容易被正确使用,不容易被误用
  10. 每天一个Linux命令(11)--nl命令
  11. 解决hadoop中 bin/hadoop fs -ls ls: `.&#39;: No such file or directory问题
  12. Mysql 根据一个表数据更新另外一个表
  13. JS模块化工具require.js教程(二):基本知识
  14. C# 获取文件MD5值的方法
  15. 手动建立mapping以及增加属性
  16. 解决Oracle死锁问题,及产生的原因
  17. 撩课-Web大前端每天5道面试题-Day1
  18. 快速定位iOS线上BUG在哪个控制器崩溃
  19. pop3设置
  20. 升级vue-cli为 cli3 并创建项目

热门文章

  1. Easyui 遮罩实现方式
  2. 前端基础之JavaScript_(2)_BOM对象
  3. 异常:Retrieving the COM class factory for component with CLSID {00024500-0000-0000-C000-000000000046} failed due to the following error: 80070005.
  4. pyhton3 sys模块
  5. 自定义Cell需要注意的问题
  6. HNOI2019梦游记
  7. ACM训练小结-2018年6月15日
  8. INSPIRED启示录 读书笔记 - 第33章 新瓶装老酒
  9. Windows系统 本地文件如何复制到远程服务器
  10. php记录代码执行时间