K-th Number

来源:互联网 发布:图片特效软件 编辑:程序博客网 时间:2024/06/06 18:55

OpenJudge 线段树练习题 K-th Number


题目描述

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.

输入

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).

输出

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

样例输入

7 31 5 2 6 3 7 42 5 34 4 11 7 3
样例输出

563
提示

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


分析

题目难度比较大,如果用一棵线段树,线段树的每一个结点都保存一个排好序的区间,不仅可能会TLE,还可能会MLE。在网上发现一个神奇的东西叫做主席树,可以用它来解决这个问题。其基本思路就是对输入的数据序列(即array)的每一个前缀建立一棵线段树,每一棵树都相当于保存着一段排好序的序列。由于这些线段树有很多相同的部分,所以在建树的过程中,会复用这些共同的部分以减小存储空间。

引用殇雪的主席树介绍http://www.cnblogs.com/zyf0163/p/4749042.html


AC代码

#include <iostream>#include <algorithm>using namespace std;int cnt = 0, pos;struct node {int a, b;      //区间[a,b] int left, right;      //左右子树在tree中的下标 int sum;       //区间[a,b]中数的个数 }tree[2000000];    //存储树的结点 void build(int a, int b)     //建空树 {tree[cnt].a = a;tree[cnt].b = b;tree[cnt].sum = 0;if (a == b)      //树叶 ++cnt; else      //建左右子树 {int mid = (a+b)>>1, temp = cnt;++cnt;tree[temp].left = cnt;build(a,mid);tree[temp].right = cnt;build(mid+1,b);}}void insert(int lasti)       //利用前一棵树的信息建树 {tree[cnt].a = tree[lasti].a;      //区间[a,b] tree[cnt].b = tree[lasti].b;tree[cnt].sum = tree[lasti].sum+1;    //区间[a,b]中数的个数加1(新增加了数array[pi]) if (tree[cnt].a == tree[cnt].b)     //树叶 ++cnt;else         //数array[pi]在左子树或右子树 {int mid = (tree[cnt].a+tree[cnt].b)>>1, temp = cnt;++cnt;if (pos <= mid)       //在左子树 {tree[temp].right = tree[lasti].right;        //复用前一棵树的右子树 tree[temp].left = cnt;         //建左子树 insert(tree[lasti].left);}else            //在右子树 {tree[temp].left = tree[lasti].left;        //复用前一棵树的左子树tree[temp].right = cnt;         //建右子树insert(tree[lasti].right);}}}int query(int pi, int pj, int k)        //访问区间[i,j],返回第k小的数在sort_array中的下标 {if (tree[pj].a == tree[pj].b)       //树叶 return tree[pj].a;int lefti = tree[pi].left, leftj = tree[pj].left, leftsum;leftsum = tree[leftj].sum-tree[lefti].sum;        //左子树所表示区间中数的个数 if (leftsum >= k)          //第k小的数在左子树 return query(lefti,leftj,k);else          //第k小的数在右子树(右子树的第k-leftsum个数) return query(tree[pi].right,tree[pj].right,k-leftsum);}int main(){int n, m, i, j, k, pi;scanf("%d%d",&n,&m);       //n个数m次操作 int array[n], sort_array[n], trees[n+1];for (pi = 0; pi < n; ++pi)    //输入n个数 {scanf("%d",array+pi);sort_array[pi] = array[pi];}sort(sort_array,sort_array+n);      //生成排序后的array trees[0] = 0;      //空树的根结点在tree中的下标 build(0,n-1);      //建一棵空树for (pi = 0; pi < n; ++pi)     //建n棵树 {pos = lower_bound(sort_array,sort_array+n,array[pi]) - sort_array;    //array[pi]在sort_array中的下标 trees[pi+1] = cnt;     //树的根结点在tree中的下标 insert(trees[pi]);     //建树 }while (m--)      //m次操作 {scanf("%d%d%d",&i,&j,&k);    //输入 printf("%d\n",sort_array[query(trees[i-1],trees[j],k)]);    //输出(本程序中区间下标从0开始,题目中的区间[i,j]在本程序中实际上是[i-1,j-1]) }return 0;}