zoukankan      html  css  js  c++  java
  • hdu2665 && poj2104划分树

    K-th Number
    Time Limit: 20000MS   Memory Limit: 65536K
    Total Submissions: 47066   Accepted: 15743
    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
    题意:给你有n个元素的数组,q次查询,每次问L,R之间第k大的数是什么。
     
    思路:
    划分树模板题。
    #include<set>
    #include<map>
    #include<queue>
    #include<stack>
    #include<cmath>
    #include<string>
    #include<vector>
    #include<cstdio>
    #include<cstring>
    #include<iostream>
    #include<algorithm>
    #define INF 1000000001
    #define MOD 1000000007
    #define ll long long
    #define lson l,m,rt<<1
    #define rson m+1,r,rt<<1|1
    #define pi acos(-1.0)
    using namespace std;
    const int MAXN = 100010;
    int num[20][MAXN],cnt[20][MAXN];//num[d][i]表示深度为d第i个数 cnt[d][i]表示深度为d第i个数之前(包括第i个数)小于sor[m]的个数
    int n,sor[MAXN];//sor[i]表示快排后的数组
    void build(int l,int r,int d)
    {
        if(l == r){
            return ;
        }
        int m = (l + r) >> 1;
        int same_m = m - l + 1;//same_m表示在[l,r]中和sor[m]相同值得个数 如果有多个sor[m]的值,那么有一部分会放到左孩子。
                               //这里就是求放到左孩子的值为sor[m]的个数
        for(int i = l; i <= r; i++){
            if(num[d][i] < sor[m])same_m --;
        }
        int pl,pr;
        int cnt_small = 0;//表示当前小于sor[m]的个数
        pl = l,pr = m + 1;//左右孩子开始的位置
        for(int i = l; i <= r; i++){
            if(num[d][i] < sor[m]){//如果当前值比sor[m]小 那么会放在左孩子
                num[d+1][pl++] = num[d][i];
                cnt_small ++;
                cnt[d][i] = cnt_small;
            }
            else if(num[d][i] == sor[m] && same_m){//如果当前的值和sor[m]相同,那么就要判断左边的sor[m]是否放满了
                same_m --;
                cnt_small ++;
                cnt[d][i] = cnt_small;
                num[d+1][pl++] = num[d][i];
            }
            else {//放右孩子 由于右孩子都比sor[m]大,所以cnt_small不需要加
                num[d+1][pr++] = num[d][i];
                cnt[d][i] = cnt_small;
            }
        }
        build(l,m,d + 1);
        build(m + 1,r,d + 1);
    }
    void Init()
    {
        memset(cnt,0,sizeof(cnt));
        sort(sor+1,sor+n+1);
        build(1,n,1);
    }
    int query(int L,int R,int k,int l,int r,int d)//查询区间[L,R]中第K大的值
    {
        if(l == r){//找到
            return num[d][l];
        }
        int m = (l + r) >> 1;
        int s, ss;//s表示[l,L)中小于sor[m]的个数 ss表示[L,R]中小于sor[m]的个数
        if(l == L)s = 0;//如果左边界相同 不能cnt[d][L-1] 因为L-1有可能是别的子树的
        else s = cnt[d][L - 1];
        ss = cnt[d][R] - s;
        if(ss >= k){//如果当前区间内左孩子的个数足够k个,那么就要进入左孩子查询
            int newl = l + s;//左边会变成l + s 因为左孩子的值都小于sor[m] 建树的时候,数组的相对大小顺序没有改变,也就是说这是一个稳定的排序。
                             //也就是说从d到d+1时 加入的数 还是按照相对顺序的
                             //所以这里的新的左边界 就是l + s, 我们要找的d+1层中 左孩子都是小于sor[m]的!!
                             // s是[l,L)的到左孩子的值, 看) 所以不需要减一
            int newr = l + s + ss - 1;//同理 不过由于这里 s + ss后就是[l,R] 连起来了 所以需要减一
            return query(newl,newr,k,l,m,d + 1);
        }
        else {
            int a = L - l - s;//不需要加一 因为L这里不算 所以后面的newl需要加1
                              //a表示[l,L)之间大于sor[m]的个数 也就是要进入右孩子的个数
            int b = R - L + 1 - ss;//b表示[L,R]之间 进入右孩子的个数
            int newl = m + 1 + a;//右孩子的值都大于sor[m] 所以原来的左边界 在右孩子中会变成 m + 1 + a
            int newr = m + a + b;//b是长度 由于m本需要+1 但是由于加了长度就已经+1了 其实是 m + 1 + a + b - 1
            return query(newl,newr,k - ss,m + 1,r,d + 1);
        }
    }
    void solve(int q)
    {
        int x,y,k;
        while(q--){
            scanf("%d%d%d",&x,&y,&k);
            printf("%d
    ",query(x,y,k,1,n,1));
        }
    }
    int main()
    {
        int q;
        int t;
        scanf("%d",&t);
        while(t--){
            scanf("%d%d",&n,&q);
            memset(num,0,sizeof(num));
            for(int i = 1; i <= n; i++){
                scanf("%d",&num[1][i]);
                sor[i] = num[1][i];
            }
            Init();
            solve(q);
        }
        return 0;
    }
  • 相关阅读:
    open文件操作之mode模式剖析
    super函数没有那么简单-super原理剖析
    mac下 将python2.7改为python3
    《图解http》构建Web内容的技术
    《图解HTTP》Web的攻击技术
    java获取服务器信息
    免费的论文查重网站
    gitlab持续集成,自动部署
    mybatis-plus invalid bound statement (not found) insert解决办法
    锁原理:偏向锁、轻量锁、重量锁
  • 原文地址:https://www.cnblogs.com/sweat123/p/5543067.html
Copyright © 2011-2022 走看看