题目

Source

http://poj.org/problem?id=2104

Description

You are working for Macrohard company in data structures department. After failing your previous task about key insertion you were asked to write a new data structure that would be able to return quickly k-th order statistics in the array segment.
That is, given an array a[1...n] of different integer numbers, your program must answer a series of questions Q(i, j, k) in the form: "What would be the k-th number in a[i...j] segment, if this segment was sorted?"
For example, consider the array a = (1, 5, 2, 6, 3, 7, 4). Let the question be Q(2, 5, 3). The segment a[2...5] is (5, 2, 6, 3). If we sort this segment, we get (2, 3, 5, 6), the third number is 5, and therefore the answer to the question is 5.

Input

The first line of the input file contains n --- the size of the array, and m --- the number of questions to answer (1 <= n <= 100 000, 1 <= m <= 5 000).
The second line contains n different integer numbers not exceeding 10 9 by their absolute values --- the array for which the answers should be given.
The following m lines contain question descriptions, each description consists of three numbers: i, j, and k (1 <= i <= j <= n, 1 <= k <= j - i + 1) and represents the question Q(i, j, k).

Output

For each question output the answer to it --- the k-th number in sorted a[i...j] segment.

Sample Input

7 3
1 5 2 6 3 7 4
2 5 3
4 4 1
1 7 3

Sample Output

5
6
3

分析

题目是给一个序列,多次询问序列区间中第k小的元素。

经典主席树入门题。。相当于序列各个前缀建一棵权值线段树,区间上的信息就是两个前缀和的差分了。。复习复习,已经忘了。

代码

  1. #include<cstdio>
  2. #include<cstring>
  3. #include<algorithm>
  4. using namespace std;
  5. #define MAXN 111111
  6.  
  7. int root[MAXN],tree[MAXN*20],lch[MAXN*20],rch[MAXN*20],x,y,N;
  8. void update(int i,int j,int a,int &b){
  9. b=++N;
  10. if(i==j){
  11. tree[b]=tree[a]+1;
  12. return;
  13. }
  14. lch[b]=lch[a];
  15. rch[b]=rch[a];
  16. int mid=i+j>>1;
  17. if(x<=mid) update(i,mid,lch[a],lch[b]);
  18. else update(mid+1,j,rch[a],rch[b]);
  19. tree[b]=tree[lch[b]]+tree[rch[b]];
  20. }
  21. int query(int i,int j,int l,int r,int k){
  22. if(i==j) return i;
  23. int mid=i+j>>1;
  24. int lcnt=tree[lch[r]]-tree[lch[l]];
  25. if(k<=lcnt) return query(i,mid,lch[l],lch[r],k);
  26. return query(mid+1,j,rch[l],rch[r],k-lcnt);
  27. }
  28.  
  29. int a[MAXN],b[MAXN];
  30. int main(){
  31. int n,m;
  32. scanf("%d%d",&n,&m);
  33. for(int i=1; i<=n; ++i){
  34. scanf("%d",&a[i]);
  35. }
  36.  
  37. memcpy(b,a,sizeof(b));
  38. sort(b+1,b+1+n);
  39. int bn=unique(b+1,b+1+n)-b-1;
  40.  
  41. for(int i=1; i<=n; ++i){
  42. x=lower_bound(b+1,b+1+n,a[i])-b;
  43. update(1,bn,root[i-1],root[i]);
  44. }
  45.  
  46. int l,r,k;
  47. while(m--){
  48. scanf("%d%d%d",&l,&r,&k);
  49. printf("%d\n",b[query(1,bn,root[l-1],root[r],k)]);
  50. }
  51. return 0;
  52. }

POJ2104 K-th Number(主席树)的更多相关文章

  1. 【poj2104】K-th Number 主席树

    题目描述 You are working for Macrohard company in data structures department. After failing your previou ...

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

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

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

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

  4. [POJ2104] K – th Number (可持久化线段树 主席树)

    题目背景 这是个非常经典的主席树入门题--静态区间第K小 数据已经过加强,请使用主席树.同时请注意常数优化 题目描述 如题,给定N个正整数构成的序列,将对于指定的闭区间查询其区间内的第K小值. 输入输 ...

  5. poj2104 K-th Number区间第k小值 主席树

    原来主席树就是可持久化线段树啊,刚知道,,, 作为一道裸题,还是必A的,然而一开始偷懒不写离散化跪了N多遍,后来在缪大的帮助下发现了这个问题,遂A之 ——又是这种破问题,实在不想说自己了 把n个数看成 ...

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

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

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

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

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

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

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

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

  10. POJ 2104 K-th Number 主席树(区间第k大)

    题目链接: http://poj.org/problem?id=2104 K-th Number Time Limit: 20000MSMemory Limit: 65536K 问题描述 You ar ...

随机推荐

  1. windows安装rabbitmq

    官网下载windows安装版本:http://www.rabbitmq.com/install-windows.html ,安装文件rabbitmq-server-3.6.5.exe 前提:安装erl ...

  2. Yii rbac原理和实践

    Yii框架中集成分层的 RBAC,代码位于vendor\yiisoft\yii2\rbac中,rbac工作原理分为两部分,建立授权数据和进行权限检查. 如上,一个角色拥有某个权限,如果希望用户拥有这个 ...

  3. python 文件操作(转)

    python中对文件.文件夹(文件操作函数)的操作需要涉及到os模块和shutil模块. 得到当前工作目录,即当前Python脚本工作的目录路径: os.getcwd() 返回指定目录下的所有文件和目 ...

  4. basic use of sidekiq (2)

    vim Gemfile source "https://rubygems.org" gem "sidekiq"gem 'rack-protection' gem ...

  5. 域名管理系统DNS

    域名系统DNS,将域名转化为ip地址.域名到ip地址解析过程是以这种方式进行的,当某一程序需要把主机名解析为IP地址时,该应用进程就调用解析程序(本地程序),这时候该进程就变成了DNS的一个客户,将待 ...

  6. mysql 查询表结构

    use information_schema; select column_name, column_type, data_type, is_nullable, column_comment from ...

  7. Iterm2 ssh tab title

    vim ~/.bashrc 添加一行 p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; line-height: 19.0px; font: 13.0px "H ...

  8. win7系统下的飞秋发送文件失败问题

    飞秋发送文件失败这个问题大多数是由防火墙引起的1.检查windows自带的防火墙设置,在左侧的"允许程序通过windows防火墙"查看飞秋是否存在,不存在则增加之,公网.专网都勾选 ...

  9. Selenium FF WebDriver 加载firebug 和设置代理

    首先这次使用的webDriver for Firefox的 由于项目的原因,需要在测试的时候加载Firebug和使用vpn,加载代理 Firefox 加载代理,可以从FF菜单上看,代理分为好几种 我这 ...

  10. ModelMapper 中高级使用 java

    ModelMapper 是一个java对象自动映射的第三方架包,用起来很方便,配合阿里的frstjson可以极大简化后台代码. 但是ModelMapper 中文使用说明很少,官网http://mode ...