POJ 2104-K-th Number(平方分割 / 区域树)

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

题目大意:给你一个n个数组成的数列,有m次查询(l,r,k),让你输出[l, r]内第k大的数。

解法1(平方分割法 11735ms):就是把原来的数组a分成许多小数组(分块),在用一个新的数组b装入,并排好序,然后在排好序的数组内二分搜索[l, r] (l = -1, r = n-1) 答案s,二分的条件就是目标区间内小于s的的个数是否大于等于k,就这样二分搜索,最后输出b[r]即可。(尽量定义局部变量, 全局变量可能会超时!
AC代码:
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
const int k = 1000; //我们每块分成1000个

int n, m, a[100010], b[100010], c[100010];

vector<int> bucket[110];

int main()
{
    int u, v, w;
    while(~scanf("%d%d", &n, &m)) {
        for(int i = 0; i < n; i ++) {
            scanf("%d", &a[i]);
            bucket[i/k].push_back(a[i]);
            b[i] = a[i];
        }
        sort(b, b+n);
        for(int i = 0; i < n/k; i ++)
            sort(bucket[i].begin(), bucket[i].end()); //给每块都进行排序,以便二分查找
        while(m --) {
            scanf("%d%d%d", &u, &v, &w);
            int l = -1, r = n-1;
            while(l+1 < r) {
                int mid = (l+r)/2;
                int s = b[mid];
                int L = u-1, R = v, cnt = 0;
                while(L < R && L%k) if(a[L ++] <= s) cnt ++; //不在完整的分块区域内的,我们直接判断
                while(L < R && R%k) if(a[-- R] <= s) cnt ++;
                while(L < R) //在完整的分块区域内的,我们直接二分查找
                {
                    int q = L/k;
                    cnt += upper_bound(bucket[q].begin(), bucket[q].end(), s) - bucket[q].begin();
                    L += k;
                }
                if(cnt >= w) r = mid;
                else l = mid;
            }
            printf("%d\n", b[r]);
        }
    }
    return 0;
}

解法二(区域树 6375ms):我们同样可以运用线段树来解决这一题,只不过这里树的节点为一个数组,每个节点存左右子节点的数,并排好序,最后我们同样在目标区间二分搜索答案即可。
AC代码:
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
typedef long long LL;
const int k = 1000;

vector<int> dat[100010*4];
int a[100010], ans, cnt, x;

struct node
{
    int l;
    int r;
}e[100010*4];

void build(int l, int r, int k)
{
    e[k].l = l;
    e[k].r = r;
    dat[k].clear(); //注意清空
    if(l == r) {
        scanf("%d", &x);
        a[cnt ++] = x;
        dat[k].push_back(x);
        return;
    }
    int mid = (l+r)/2;
    build(l, mid, 2*k);
    build(mid+1, r, 2*k+1);
    dat[k].resize(r-l+1); //这里要分配空间,不然会报错
    merge(dat[2*k].begin(), dat[2*k].end(), dat[2*k+1].begin(), dat[2*k+1].end(), dat[k].begin()); //合并函数
    //该函数有自动排序功能
}

void quary(int l, int r, int k, int num)
{
    if(e[k].l == l && e[k].r == r) {
        ans += upper_bound(dat[k].begin(), dat[k].end(), num) - dat[k].begin(); //在目标区间进行二分搜索
        return;
    }
    int mid = (e[k].l+e[k].r)/2;
    if(r <= mid) quary(l, r, 2*k, num);
    else if(l >= mid+1) quary(l, r, 2*k+1, num);
    else {
        quary(l, mid, 2*k, num);
        quary(mid+1, r, 2*k+1, num);
    }
}

int main()
{
    int n, m;
    int u, v, w;
    while(~scanf("%d%d", &n, &m)) {
        build(1, n, 1);
        cnt = 0;
        sort(a, a+n);
        while(m --) {
            scanf("%d%d%d", &u, &v, &w);
            int l = -1, r = n-1;
            while(l+1 < r) {
                int mid = (l+r)/2;
                int s = a[mid];
                ans = 0;
                quary(u, v, 1, s);
                if(ans >= w) r = mid;
                else l = mid;
            }
            printf("%d\n", a[r]);
        }
    }
    return 0;
}


后面补上图解
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值