用线段树求区间第K大(POJ 2104 K-th Number)

K-th Number
Time Limit: 20000MS Memory Limit: 65536K
Total Submissions: 59428 Accepted: 20702
Case Time Limit: 2000MS

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

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

Sample Output

5
6
3

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

1.题目大意:
给定你一个长度为S的无序序列,每次询问其间一段[L,R],问在这一段中的第k大数是多少。

2.做法及思路:
此题用线段树来解决也是可以的(不要用那些高大上的东西)。
我们首先要转变一下思路,
我们用线段树的每一个节点来维护一个区间,也就是说每个节点所储存的内容不再是一个值,而因该是一个区间。看到数据范围较大,直接开数组会炸空间,考虑用不定长数组vector来存。

这里写图片描述

那么我们现在的目的是找第k大的数,也就是说区间内小于等于这个数的个数为k个。
很显然这就是一个二分可以解决的。
注意这里我们并不是二分区间的一个点,而是二分一个具体值R!!!

我们二分得到具体值R,然后对于R的检验利用线段树就因该不难了。
线段树每次返回当前段中小于等于R的数的个数。

对于给定区间(目标区间L,R),和当前线段树区间LL,RR,总共分为以下三种情况:

1.[LL,RR]与[L,R]不相交,此时返回0;
2. [LL,RR]包含于[L,R](即完全包含),此时二分查找个数,返回此个数(vector中所存为有序列)
3. [LL,RR]与[L,R]有交集,但不完全包含,此时继续查找左右子树。

由于对应同一深度的节点最多只访问常数个,因此可以在O(log2n)时间里求出不超过x的数的个数。所以算法的复杂度是O(nlogn + mlog3n)。

3.具体实现代码(注释内置):

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<iostream>
#include<vector>
#define maxn 100001
using namespace std;

vector<int>date[maxn*4+5];  //存线段树节点信息
int s[maxn+5];               //初始的序列值
int n,m;                     

inline int gi()
{
  int date1 = 0,m = 1; char ch = 0;
  while(ch!='-' && (ch<'0'||ch>'9'))ch = getchar();
  if(ch == '-'){ch = getchar();m = -1;}
  while(ch>='0' && ch<='9')
    {
      date1 = date1*10+ch-'0';
      ch = getchar();
    }return date1*m;
}

//建树!
//注意一下,建议vector的0号位置乱塞一个数(不要用),方便二分。
void build(int ro,int l,int r)
{
  if(l == r)
  {
    date[ro].push_back(-1e7);             //乱塞一个数(0号位置)
    date[ro].push_back(s[l]); return;
  }
  int mid = (l+r)>>1;
  build(ro<<1,l,mid);
  build(ro<<1|1,mid+1,r);

  //这一部分类似于归并排序(应该说就是一样的),保证时间复杂度。
  int size1 = date[ro<<1].size()-1;
  int size2 = date[ro<<1|1].size()-1;
  int i = 1,j = 1;
  date[ro].push_back(-1e7);
  while(i<=size1 && j<=size2)
    {
      if(date[ro<<1][i]>date[ro<<1|1][j])date[ro].push_back(date[ro<<1|1][j++]);
      else date[ro].push_back(date[ro<<1][i++]);
    }
  while(i<=size1)date[ro].push_back(date[ro<<1][i++]);
  while(j<=size2)date[ro].push_back(date[ro<<1|1][j++]);
  return;

}

//二分查找完全包含的区间中小于等于R的数的个数
int low_bound(int ro,int target)  
{
  int L,R,res;
  L =1 ; R = date[ro].size()-1;res = 0;
  while(L<=R)
    {
      int mid = (L+R)>>1;
      if(date[ro][mid]<=target){res = mid; L = mid + 1;}
      else R = mid - 1;
    }
  return res;
}

//线段树查询区间内小于等于R的数的个数
//tl--target L ;  tr--target R;  l--now L;  r--now R 。
int query(int ro,int l,int r,int tl,int tr,int K)
{
  if(tr<l || tl>r)return 0;   //没有交集
  else if(tl<=l && r<=tr)     //完全包含
    {
      int temp_num = low_bound(ro,K);
      return temp_num;
    }
  else             //有交集但不完全包含,递归查找左右子树
    {
      int mid = (l + r)>>1;
      int temp1 = query(ro<<1,l,mid,tl,tr,K);
      int temp2 = query(ro<<1|1,mid+1,r,tl,tr,K);
      return (temp1+temp2);     //合并答案
    }
}

//二分查找目标值R,二分出值R后再用线段树进行验证
int search(int l,int r,int k)
{
  int L,R,RES,NUM;
  L = -1000000007; R = 1000000007;
  while(L<=R)
    {
      int mid = (L+R)>>1;
      NUM = query(1,1,n,l,r,mid);  //RES不断向下逼近,保证精确性。
      if(NUM >= k){RES = mid ; R = mid-1;}
      else L = mid+1;
    }
  return RES;
}

int main()
{
  //freopen("segment_tree.in","r",stdin);
  int i,j,l,r,k,Ans;
  n = gi();m = gi();
  for(i = 1; i <= n ; i ++)s[i] = gi();
  build(1,1,n);                    //建树
  for(j = 1 ; j <= m ; j ++)
    {
      l = gi(); r = gi(); k = gi();
      Ans = search(l,r,k);          //二分
      printf("%d\n",Ans);
    }
  return 0;
}

。。。。。。

  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值