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. 1
  2. 10 1
  3. 1 4 2 3 5 6 7 8 9 0
  4. 1 3 2

Sample Output

  1. 2
  • 注意:题目是求第k小,可能写错了还是怎么的。
  • 个人觉得既学习了主席树,也同时对离散的认识也加强了吧,表示以前没有用过unique结合lower_bound来离散操作。
  • 没有格外的插入操作,不需要init和memset。但是要记住加地址符。
  • 从现在开始,代码格式要更加规范,比如符号两边加空格。
  • Persistent line tree ,我姑且函数起名PIT,如果知道了其英文名字再改过来。
  • 每次查询时范围都是(1,sz);和普通的线段树的区别是:普通线段树从root=1开始沿下走。而主席树是沿两个root向下走,走的方向是一样的。
  1. #include<cstdio>
  2. #include<cstdlib>
  3. #include<iostream>
  4. #include<algorithm>
  5. using namespace std;
  6. const int maxn=;
  7. int a[maxn],b[maxn],T,n,sz,q,cnt,ql,qr,x;
  8. int ch[maxn * ][],sum[maxn * ],rt[maxn * ];
  9. struct PLTree
  10. {
  11.  
  12. void build(int& now,int l,int r)
  13. {
  14. now = ++ cnt;
  15. sum[now] = ;//ch[now][0],cnt[now][1]没必要跟新,后面sum会跟新。
  16. if(l == r) return ;
  17. int Mid = (l + r)>>;
  18. build(ch[now][],l,Mid);
  19. build(ch[now][],Mid + ,r);
  20. }
  21. void insert(int& now,int last,int l,int r,int pos)
  22. {
  23. now = ++ cnt;
  24. ch[now][]=ch[last][];//假设公共,不同的部分下面再跟新。
  25. ch[now][]=ch[last][];
  26. sum[now] = sum[last] + ;
  27. if(l == r) return ;
  28. int Mid = (l+r) >> ;
  29. if(pos <= Mid) insert(ch[now][],ch[last][],l,Mid,pos);
  30. else insert(ch[now][],ch[last][],Mid + ,r,pos);
  31. }
  32. int query(int ss,int tt,int l,int r,int k)
  33. {
  34. if(l == r) return l;//要位置 ,不是要值
  35. int Mid =(l + r) >> ,tmp = sum[ch[tt][]] - sum[ch[ss][]];//本身呢?
  36. if(k <= tmp) return query(ch[ss][],ch[tt][],l,Mid,k);
  37. else return query(ch[ss][],ch[tt][],Mid + ,r,k - tmp);
  38. }
  39. void work()
  40. {
  41. scanf("%d%d%d",&ql,&qr,&x);
  42. int ans = query(rt[ql - ],rt[qr],,sz,x);//注意这个范围是(1,sz),和建树的时候的长度一样。
  43. printf("%d\n",b[ans]);
  44. }
  45. };
  46. PLTree P;
  47. int main(){
  48. scanf("%d",&T);
  49. while(T--){
  50. scanf("%d%d",&n,&q);
  51. for(int i = ; i <= n;i ++) scanf("%d",&a[i]) , b[i]=a[i];
  52. sort(b + ,b + n + );
  53. sz = unique(b + ,b + n + )-(b + );
  54. cnt=;
  55. P.build(rt[],,sz);
  56. for(int i = ;i <= n;i ++) a[i]=lower_bound(b + ,b + sz + ,a[i]) - b;//a现在是排名
  57. for(int i = ;i <= n;i ++) P.insert(rt[i],rt[i-],,sz,a[i]);
  58. while(q--) P.work();
  59. }
  60. return ;
  61. }

HDU2665Kth number (主席树+离散)的更多相关文章

  1. poj2104 k-th number 主席树入门讲解

    poj2104 k-th number 主席树入门讲解 定义:主席树是一种可持久化的线段树 又叫函数式线段树   刚开始学是不是觉得很蒙逼啊 其实我也是 主席树说简单了 就是 保留你每一步操作完成之后 ...

  2. poj 2104 K-th Number 主席树+超级详细解释

    poj 2104 K-th Number 主席树+超级详细解释 传送门:K-th Number 题目大意:给出一段数列,让你求[L,R]区间内第几大的数字! 在这里先介绍一下主席树! 如果想了解什么是 ...

  3. POJ2104 K-th Number[主席树]【学习笔记】

    K-th Number Time Limit: 20000MS   Memory Limit: 65536K Total Submissions: 51440   Accepted: 17594 Ca ...

  4. [poj2104] K-th Number (主席树)

    主席树 Description You are working for Macrohard company in data structures department. After failing y ...

  5. poj 2104 K-th Number(主席树 视频)

    K-th Number 题意: 给你一些数,让你求一个区间内,第k大的数是多少. 题解: 主席树第一题,看的qsc视频写的,戳戳戳 学到了unique函数,他的作用是:把相邻的重复的放到后面,返回值是 ...

  6. 主席树:POJ2104 K-th Number (主席树模板题)

    K-th Number Time Limit: 20000MS   Memory Limit: 65536K Total Submissions: 44952   Accepted: 14951 Ca ...

  7. 【POJ2104】【HDU2665】K-th Number 主席树

    [POJ2104][HDU2665]K-th Number Description You are working for Macrohard company in data structures d ...

  8. SPOJ MKTHNUM & POJ 2104 - K-th Number - [主席树模板题]

    题目链接:http://poj.org/problem?id=2104 Description You are working for Macrohard company in data struct ...

  9. Online Judge 2014 K-th Number -主席树

    You are working for Macrohard company in data structures department. After failing your previous tas ...

随机推荐

  1. 【Flask】Flask-Sqlalchemy使用笔记

    ### 安装:```shellpip install flask-sqlalchemy``` ### 数据库连接:1. 跟sqlalchemy一样,定义好数据库连接字符串DB_URI.2. 将这个定义 ...

  2. python__Django 分页

    自定义分页的类: #!/usr/bin/env python # -*- coding: utf-8 -*- # Created by Mona on 2017/9/20 from django.ut ...

  3. 跨平台移动开发_Windows Phone 8 使用 PhoneGap 方法

    第一步,下载Windows Phone SDK 8.0 http://www.microsoft.com/zh-cn/download/details.aspx?id=35471 离线下载地址(推荐使 ...

  4. linux下安装jsp开发运行环境(centos7)

    1 开发环境包括 1)apache-tomcat 2)java-jdk 3)mysql 2 apache-tomcat安装(应该先装java再装tomcat) 1)到官网下载最新版本(不建议用yum安 ...

  5. for循环执行流程

    语句格式: for(表达式1;表达式2;表达式3) { 循环体 } 表达式1:赋值表达式,用来给控制变量赋初值.(只执行一次) 表达式2:逻辑表达式,是循环的控制条件,用来判断控制变量是否符合循环条件 ...

  6. jack server 常见错误解决方法【转】

    本文转载自:https://blog.csdn.net/qq_27061049/article/details/70156200 jack 服务常见错误解决方法 当你编译Android时,你不需要修改 ...

  7. 通过公钥解密密文思路(256bits RSA)

    256bit RSA公钥安全系数极低,只需要几分钟即可破解密文,本文综合其他文章记录了一次解密256bits RSA加密的密文的过程,仅作为备忘. 1.分解公钥,分解出n与e: 1.1使用openss ...

  8. 1.1nginx安装

    1.必要软件准备 安装 pcre为了支持 rewrite 功能,我们需要安装 pcre# yum install pcre* //如过你已经装了,请跳过这一步 安装 openssl需要 ssl 的 ...

  9. Ajax缓存处理

    如果直接用jQuery里的$.ajax()方法的话,去除缓存很简单,只需要配置一下缓存属性cache为false,但如果想要简单写法getJSON(),去除缓存就不能通过配置来解决了.因为getJSO ...

  10. JavaWeb基础

    1.Servlet: Servlet是JavaWeb的3大组件之一,是把url请求转为后台处理的具体类,此类必须实现Servlet接口,一把实际使用时无须我们实现,我们直接使用JavaEE的HTTPS ...