ACM: 划分树 poj 2104

K-th Number

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. 一开始, 看见题目求出序列中第k大的数, 第一反应是快排. 但是直接排序是不可行.

        2. 在快排的思想上, 每次partition一次之后可以得到划分序列的一个mid值, 根据mid

        可以判断是在序列的左边,还是右边. 快排的基础上将递归树去掉一半的分支, 提交TLE了.

        3. 一点点的改进似乎解决不了. 因为是访问式的查询结构, 查询次数多了复杂度挺高了.

        4. 网上发现一种新方法, 划分树. 快速查找法.

        划分树分析:

        建树: build_tree(int l, int r, int deep) //区间[l,r], 深度deep

               (1).先将原序列排序, 得到一个已经排好序的序列. 可以求出区间[l,r]的中位数a[mid];

               (2).将原序列与a[mid]比较, 小于它的归类到左子树[l,mid-1], 否则归类右子树[mid,r];

               (3).并且同时记录, 每个节点i, 在区间[l,i]中归类到左子树节点个数.

               (4).递归建立左右子树[l,mid-1], [mid,r];

        

        查找: find(int start,int end,int s,int e,int k,int deep) //查询[start,end]在[s,e]上

               (1).在大区间[s,e]上查找小区间[start,end]第k小元素.

               (2).先根据区间[start,end]上有多少个归类到左子树上, 如果个数比k大即第k元素在左子树,

               否则在右子树上.

               (3).继续递归在相应的区间上查找, 最后当start == end只有一个元素即使要查找的元素.

        查找问题分析:

             (1).前面我们记录了第deep层中, 第start元素中归类到左子树节点个数,

             设区间[s,start-1], p = num[deep][start-1]个节点归类到了左子树,

               区间[s,end], q = num[deep][end]个节点归类到了左子树.

             即在区间[start,end]归入到左子树的节点个数是 x = (q-p);

               if(x >= k) find(左子树);

               else find(右子树);

        怎么左右区间确定分析:

              (1). 左子树: 小区间应该是[s+p,s+q-1]; 大区间[s,mid]; k不变.

              (2). 右子树: 小区间[mid+start-(s+p)+1, mid+end-(s-q)+1], 大区间[mid+1,e],

                   这里的k显然要改变, 因为要去除已经进入左子树部分, 即: k = k-x;

 

代码:

#include <cstdio>
#include <iostream>
#include <cstring>
#include <cstdlib>
using namespace std;
#define MAX 100005

int n, m;
int a[MAX], val[25][MAX], num[25][MAX];
int start, end, k;

int cmp(const void *a, const void *b)
{
 return *(int*)a - *(int*)b;
}

void build_tree(int l, int r, int deep)
{
 if(l == r) return ;
 int mid = (l+r)/2;
 int left_child = l, right_child = mid+1;
 for(int i = l; i <= r; ++i)
 {
  if(val[deep][i] <= a[mid])
  {
   if(i == l) num[deep][i] = 1;
   else num[deep][i] = num[deep][i-1]+1;
   val[deep+1][left_child++] = val[deep][i];
  }
  else
  {
   if(i == l) num[deep][i] = 0;
   else num[deep][i] = num[deep][i-1];
   val[deep+1][right_child++] = val[deep][i];
  }
 }
 build_tree(l,mid,deep+1);
 build_tree(mid+1,r,deep+1);
}

int find(int start, int end, int s, int e, int k, int deep)
{
 if(start == end) return val[deep][start];
 int mid = (s+e)/2;
 int p, x;
 if(start == s) p = 0;
 else p = num[deep][start-1];

 x = num[deep][end] - p;
 if(x >= k)
  return find(s+p,s+num[deep][end]-1, s, mid, k, deep+1);
 else
  return find(mid+start-s-p+1, mid+end-s-num[deep][end]+1, mid+1, e, k-x, deep+1); 
}

int main()
{
// freopen("input.txt","r",stdin);
 while(scanf("%d %d",&n, &m) != EOF)
 {
  for(int i = 1; i <= n; ++i)
  {
   scanf("%d",&a[i]);
   val[0][i] = a[i];
  }
  qsort(a+1,n,sizeof(a[0]),cmp);

  build_tree(1,n,0);
  for(int i = 0; i < m; ++i)
  {
   scanf("%d %d %d",&start, &end, &k);
   int result = find(start, end, 1, n, k, 0);
   printf("%d\n",result);
  }
 }
 return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值