POJ 2104 —— 平方分割 || 归并树 || 划分树(未完)

K-th Number
Time Limit: 20000MS Memory Limit: 65536K
Total Submissions: 33371 Accepted: 10510
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 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

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

题意是给你一个序列a,问你从a[i]到a[j]中第k小的数是多少

解题方法据我所知有快排水过的,平方分割,归并树,主席树,划分数,函数式线段树,莫队算法+树状数组。

我这里介绍两种吧。

一、平方分割:

平方分割用的是分桶法的思想,每个桶的大小为数组大小的平方根.时间复杂度O(n * logn + m * sqrt(n) * log(1.5)(n) )

#include <cstdio>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <map>
#include <string>
#include <stack>
#include <cctype>
#include <vector>
#include <queue>
#include <set>
#include <utility>
#include <cassert>
using namespace std;
///#define Online_Judge
#define outstars cout << "***********************" << endl;
#define clr(a,b) memset(a,b,sizeof(a))
#define lson l , mid  , rt << 1
#define rson mid + 1 , r , rt << 1 | 1
#define mk make_pair
#define FOR(i , x , n) for(int i = (x) ; i < (n) ; i++)
#define FORR(i , x , n) for(int i = (x) ; i <= (n) ; i++)
#define REP(i , x , n) for(int i = (x) ; i > (n) ; i--)
#define REPP(i ,x , n) for(int i = (x) ; i >= (n) ; i--)
const int MAXN = 100000 + 50;
const int MAXS = 10000 + 50;
const int sigma_size = 26;
const long long LLMAX = 0x7fffffffffffffffLL;
const long long LLMIN = 0x8000000000000000LL;
const int INF = 0x7fffffff;
const int IMIN = 0x80000000;
const int inf = 1 << 30;
#define eps 1e-8
const long long MOD = 1000000000 + 7;
const int mod = 100000;
typedef long long LL;
const double PI = acos(-1.0);
typedef double D;
typedef pair<int , int> pii;
#define Bug(s) cout << "s = " << s << endl;
///#pragma comment(linker, "/STACK:102400000,102400000")
int n , m;
int a[MAXN] , I[MAXN] , J[MAXN] , K[MAXN];
int nums[MAXN];
const int B = 1000;
vector <int> bucket[MAXN / B];
void solve()
{
    FOR(i , 0 , n)
    {
        bucket[i / B].push_back(a[i]);
        nums[i] = a[i];
    }
    sort(nums , nums + n);
    for(int i = 0; i < n / B ; i++)sort(bucket[i].begin() , bucket[i].end());
    for(int i = 0 ;i < m  ; i++)
    {
        int l = I[i] - 1, r = J[i], k = K[i];
        int lb = -1 , ub = n - 1;
        while(ub - lb > 1)
        {
            int mid = (lb + ub) / 2;
            int x = nums[mid];
            int tl = l , tr = r , c = 0;
            while(tl < tr && tl % B != 0)if(a[tl++] <= x)c++;
            while(tl < tr && tr % B != 0)if(a[--tr] <= x)c++;
            while(tl < tr)
            {
                int b = tl / B;
                c += upper_bound(bucket[b].begin() , bucket[b].end() , x) - bucket[b].begin();
                tl += B;
            }
            if(c >= k)ub = mid;
            else lb = mid;
        }
        printf("%d\n",nums[ub]);
    }
}
int main()
{
    while(~scanf("%d%d" ,&n , &m))
    {
        for(int  i = 0 ; i < n ; i++)scanf("%d" , &a[i]);
        for(int  i = 0 ; i < m ; i++)scanf("%d%d%d" , &I[i] , &J[i] , &K[i]);
        solve();
    }
    return 0;
}

未完待续。。。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值