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

裸题不说。主席树水过。

  1. #include <cstdio>
  2. #include <iostream>
  3. #include <algorithm>
  4. using namespace std;
  5. #define dbg(x) cout << #x << "=" << x << endl
  6. #define read(x) x=getint()
  7. #define for1(i, a, n) for(int i=a; i<=(n); ++i)
  8. #define MID (l+r)>>1
  9. inline int getint() { char c; int ret=0, k=1; for(c=getchar(); c<'0' || c>'9'; c=getchar()) if(c=='-') k=-1; for(; c>='0' && c<='9'; c=getchar()) ret=ret*10+c-'0'; return ret*k; }
  10.  
  11. const int N=100010;
  12. int id[N], a[N], b[N], n, m, root[N], cnt, key;
  13. struct node { int l, r, s; } T[N*50];
  14. void update(const int &l, const int &r, int &pos) {
  15. T[++cnt]=T[pos]; pos=cnt; ++T[pos].s;
  16. if(l==r) return;
  17. int m=MID;
  18. if(key<=m) update(l, m, T[pos].l); else update(m+1, r, T[pos].r);
  19. }
  20. int query(const int &l, const int &r, const int &x, const int &y, const int &k) {
  21. if(l==r) return l;
  22. int m=MID, s=T[T[y].l].s-T[T[x].l].s;
  23. if(k<=s) return query(l, m, T[x].l, T[y].l, k); else return query(m+1, r, T[x].r, T[y].r, k-s);
  24. }
  25. bool cmp(const int &x, const int &y) { return a[x]<a[y]; }
  26. int main() {
  27. read(n); read(m);
  28. for1(i, 1, n) read(a[i]), id[i]=i;
  29. sort(id+1, id+1+n, cmp);
  30. for1(i, 1, n) b[id[i]]=i;
  31. for1(i, 1, n) {
  32. root[i]=root[i-1]; key=b[i];
  33. update(1, n, root[i]);
  34. }
  35. int x, y;
  36. while(m--) {
  37. read(x); read(y); read(key);
  38. printf("%d\n", a[id[query(1, n, root[x-1], root[y], key)]]);
  39. }
  40. return 0;
  41. }

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 109 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

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

Sample Output

  1. 5
  2. 6
  3. 3

Hint

This problem has huge input,so please use c-style input(scanf,printf),or you may got time limit exceed.

Source

Northeastern Europe 2004, Northern Subregion

【POJ】2104 K-th Number(区间k大+主席树)的更多相关文章

  1. 计蒜客 38229.Distance on the tree-1.树链剖分(边权)+可持久化线段树(区间小于等于k的数的个数)+离散化+离线处理 or 2.树上第k大(主席树)+二分+离散化+在线查询 (The Preliminary Contest for ICPC China Nanchang National Invitational 南昌邀请赛网络赛)

    Distance on the tree DSM(Data Structure Master) once learned about tree when he was preparing for NO ...

  2. POJ 2104:K-th Number(主席树静态区间k大)

    题目大意:对于一个序列,每次询问区间[l,r]的第k大树. 分析: 主席树模板题 program kthtree; type point=record l,r,s:longint; end; var ...

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

    K-th Number Time Limit: 20000MS   Memory Limit: 65536K Total Submissions: 31790   Accepted: 9838 Cas ...

  4. POJ 2104 求序列里第K大 主席树裸题

    给定一个n的序列,有m个询问 每次询问求l-r 里面第k大的数字是什么 只有询问,没有修改 可以用归并树和划分树(我都没学过..囧) 我是专门冲着弄主席树来的 对主席树的建树方式有点了解了,不过这题为 ...

  5. POJ 2104:K-th Number(整体二分)

    http://poj.org/problem?id=2104 题意:给出n个数和m个询问求区间第K小. 思路:以前用主席树做过,这次学整体二分来做.整体二分在yr大佬的指点下,终于大概懂了点了.对于二 ...

  6. Count on a tree(SPOJ COT + 树上第k大 + 主席树 + LCA)

    题目链接:https://www.spoj.com/problems/COT/en/ 题目: 题意: 给你一棵有n个节点的树,求节点u到节点v这条链上的第k大. 思路: 我们首先用dfs进行建题目给的 ...

  7. 静态区间第K小(整体二分、主席树)

    题目链接 题解 主席树入门题 但是这里给出整体二分解法 整体二分顾名思义是把所有操作放在一起二分 想想,如果求\([1-n]\)的第\(k\)小怎么二分求得? 我们可以二分答案\(k\), \(O(n ...

  8. 【POJ 2104】 K-th Number 主席树模板题

    达神主席树讲解传送门:http://blog.csdn.net/dad3zz/article/details/50638026 2016-02-23:真的是模板题诶,主席树模板水过.今天新校网不好,没 ...

  9. POJ 2104:K-th Number 整体二分

    感觉整体二分是个很有趣的东西. 在别人的博客上看到一句话 对于二分能够解决的询问,如果有多个,那么如果支持离线处理的话,那么就可以使用整体二分了 树套树写了一天还是WA着,调得焦头烂额,所以决定学cd ...

随机推荐

  1. HDU 1203 I NEED A OFFER (01背包&&概率dp)

    M - I NEED A OFFER! Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u ...

  2. Linux upstart启动方式详解

     Ubuntu从6.10开始逐步用Upstart()代替原来的SysVinit进行服务进程的管理.RHEL(CentOS)也都从版本6开始转用Upstart代替以往的init.d/rcX.d的线性启动 ...

  3. 07 day 2

    又是惨烈的一天 第一题 多重背包.二进制拆分即可. #include <stdio.h> #define max(a,b) ((a)>(b)?(a):(b)) int n,m,i,j ...

  4. js 实现图片预加载 (js操作 Image对象属性complete ,事件onload 异步加载图片)

    通过js操纵DOM很多情况下都是为了实现和当前页html元素的异步载入,我谈谈对Image对象的一些认识.看个例子:<input type="button" name=&qu ...

  5. ali2015校园招聘笔试大题

    [本文链接] http://www.cnblogs.com/hellogiser/p/ali-2015-questions.html 1. 写一个函数,输入一个二叉树,树中每个节点存放了一个整数值,函 ...

  6. linux 多个文件中查找字符串

    2015年2月9日 14:36:38 # find <directory> -type f -name "*.c" | xargs grep "<str ...

  7. mysql 我的学习

    安装要求 安装环境:CentOS-6.3安装方式:源码编译安装 软件名称:mysql-cluster-gpl-7.2.6-linux2.6-x86_64.tar.gz下载地址:http://mysql ...

  8. 转关于垂直切分Vertical Sharding的粒度

    垂直切分的粒度指的是在做垂直切分时允许几级的关联表放在一个shard里.这个问题对应用程序和sharding实现有着很大的影响. 关联打断地越多,则受影响的join操作越多,应用程序为此做出的妥协就越 ...

  9. mysql源码:关于innodb中两次写的探索

    两次写可以说是在Innodb中很独特的一个功能点,而关于它的说明或者解释非常少,至于它存在的原因更没有多少文章来说,所以我打算专门对它做一次说明. 首先说明一下为什么会有两次写这个东西:因为innod ...

  10. Java for LeetCode 162 Find Peak Element

    A peak element is an element that is greater than its neighbors. Given an input array where num[i] ≠ ...