zoukankan      html  css  js  c++  java
  • hdu 2665 Kth number(划分树)

    Kth number

    Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 4602 Accepted Submission(s): 1468


    Problem Description
    Give you a sequence and ask you the kth big number of a inteval.

    Input
    The first line is the number of the test cases.
    For each test case, the first line contain two integer n and m (n, m <= 100000), indicates the number of integers in the sequence and the number of the quaere.
    The second line contains n integers, describe the sequence.
    Each of following m lines contains three integers s, t, k.
    [s, t] indicates the interval and k indicates the kth big number in interval [s, t]

    Output
    For each test case, output m lines. Each line contains the kth big number.

    Sample Input
    1 10 1 1 4 2 3 5 6 7 8 9 0 1 3 2

    Sample Output
    2
    刚学了划分树。来做这一题。仅仅知道划分树的原理,代码是自己写的,不是通用的写法。

    G++AC C++超内存。。。

    #include <iostream>
    #include <cstdio>
    #include <vector>
    #include <algorithm>
    using namespace std;
    
    const int maxn = 100000;
    struct tree{
        int l , r;
        vector<int> element , lft;
    }a[3*maxn];
    vector<int> seq;
    int n , m;
    
    bool cmp(int a , int b){ return a<b;}
    
    void build(int l , int r , int k){
        a[k].l = l;
        a[k].r = r;
        if(l != r){
            int mid = (l+r)/2;
            int lson = 2*k , rson = 2*k+1;
            a[lson].element.clear();
            a[rson].element.clear();
            a[lson].element.push_back(0);
            a[rson].element.push_back(0);
            a[lson].lft.clear();
            a[rson].lft.clear();
            a[lson].lft.push_back(0);
            a[rson].lft.push_back(0);
            for(int i = 1; i < a[k].element.size(); i++){
                if(a[k].element[i] <= seq[mid]){
                    a[lson].element.push_back(a[k].element[i]);
                    a[k].lft.push_back(a[k].lft[i-1]+1);
                }else{
                    a[rson].element.push_back(a[k].element[i]);
                    a[k].lft.push_back(a[k].lft[i-1]);
                }
            }
            build(l , mid , 2*k);
            build(mid+1 , r , 2*k+1);
        }
    }
    
    int query(int l , int r , int k , int kth){
        if(a[k].l==a[k].r){
            return a[k].element[1];
        }else{
            int tem = a[k].lft[r]-a[k].lft[l-1];
            if(tem >= kth){
                return query(1+a[k].lft[l-1] , 1+a[k].lft[l-1]+tem-1 , 2*k , kth);
            }else{
                return query(1+(l-1-a[k].lft[l-1]) , (l-1-a[k].lft[l-1])+r-l+1-tem , 2*k+1 , kth-tem);
            }
        }
    }
    
    void initial(){
        a[1].element.clear();
        a[1].element.push_back(0);
        a[1].lft.clear();
        a[1].lft.push_back(0);
        seq.clear();
    }
    
    void readcase(){
        scanf("%d%d" , &n , &m);
        seq.push_back(0);
        for(int i = 0; i < n; i++){
            int num;
            scanf("%d" , &num);
            if(seq[0] >= num) seq[0] = num-1;
            seq.push_back(num);
            a[1].element.push_back(num);
        }
    }
    
    void computing(){
        sort(seq.begin() , seq.end() , cmp);
        build(1 , n , 1);
        int s , t , k;
        while(m--){
            scanf("%d%d%d" , &s , &t , &k);
            printf("%d
    " , query(s , t , 1 , k));
        }
    }
    
    int main(){
        int t;
        scanf("%d" , &t);
        while(t--){
            initial();
            readcase();
            computing();
        }
        return 0;
    }


  • 相关阅读:
    TensorFlow基础篇
    MySql分类
    VISUAL STUDIO 调试
    排序分类
    位分类
    Visio分类
    工作线程AfxBeginThread的使用
    Windows Live Writer配置步骤
    用户界面线程AfxBeginThread的使用
    WIndows 相关知识
  • 原文地址:https://www.cnblogs.com/liguangsunls/p/6851770.html
Copyright © 2011-2022 走看看