HDU2665 Kth number 【归并树】

Kth number

Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 5425    Accepted Submission(s): 1760

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

Source

HDU男生专场公开赛——赶在女生之前先过节(From
WHU)

Recommend

zty   |   We have carefully selected several similar problems for you:  2660 2662 2667 2663 2661

跟POJ2104一样。

#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <vector>

#define maxn 100005
#define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
using namespace std;

vector<int> T[maxn << 2];
int N, Q;

void build(int l, int r, int rt) {
    if(l == r) {
        int val;
        scanf("%d", &val);
        T[rt].clear();
        T[rt].push_back(val);
        return;
    }

    int mid = (l + r) >> 1;

    build(lson);
    build(rson);

    T[rt].resize(r - l + 1); // Attention
    merge(T[rt<<1].begin(), T[rt<<1].end(), T[rt<<1|1].begin(), T[rt<<1|1].end(), T[rt].begin());
}

int query(int L, int R, int val, int l, int r, int rt) {
    if(L == l && R == r) {
        return upper_bound(T[rt].begin(), T[rt].end(), val) - T[rt].begin();
    }

    int mid = (l + r) >> 1;

    if(R <= mid) return query(L, R, val, lson);
    else if(L > mid) return query(L, R, val, rson);
    return query(L, mid, val, lson) + query(mid + 1, R, val, rson);
}

int main() {
    int a, b, c, k, left, right, mid, t;
    scanf("%d", &t);
    while(t--) {
        scanf("%d%d", &N, &Q);
        build(1, N, 1);
        while(Q--) {
            scanf("%d%d%d", &a, &b, &k);
            left = -1; right = N - 1;
            while(right - left > 1) { // binary search
                mid = (left + right) >> 1;
                c = query(a, b, T[1][mid], 1, N, 1);
                if(c >= k) right = mid;
                else left = mid;
            }
            printf("%d\n", T[1][right]);
        }
    }
    return 0;
}
时间: 2024-11-08 17:44:58

HDU2665 Kth number 【归并树】的相关文章

poj 2104 K-th Number(划分树模板)

划分树模板题,敲上模板就ok了. #include<algorithm> #include<iostream> #include<cstring> #include<vector> #include<cstdio> #include<cmath> #include<queue> #include<stack> #include<map> #include<set> #define MP

K-th Number 线段树(归并树)+二分查找

K-th Number 题意:给定一个包含n个不同数的数列a1, a2, ..., an 和m个三元组表示的查询.对于每个查询(i, j, k), 输出ai, ai+1, ... ,aj的升序排列中第k个数 . 题解:用线段树,每个节点维护一个区间并且保证内部升序,对于每次查询x,返回该区间小于x的数的个数.就这样不断二分,直到找到x为止. 线段树(归并树)+二分查找 1 #include <iostream> 2 #include <cstdio> 3 #include <

POJ2104 K-th Number[主席树]

K-th Number Time Limit: 20000MS   Memory Limit: 65536K Total Submissions: 51440   Accepted: 17594 Case Time Limit: 2000MS Description You are working for Macrohard company in data structures department. After failing your previous task about key inse

[POJ2104/HDU2665]Kth Number-主席树-可持久化线段树

Problem Kth Number Solution 裸的主席树,模板题.但是求k大的时候需要非常注意,很多容易写错的地方.卡了好久.写到最后还给我来个卡空间. 具体做法参见主席树论文<可持久化数据结构研究>. AC Code #include "cstdio" #include "iostream" #include "cstring" #include "algorithm" using namespace

hdoj 2665 Kth number主席树裸

Kth number Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 9417    Accepted Submission(s): 2938 Problem Description Give you a sequence and ask you the kth big number of a inteval. Input The fi

POJ 2104 K-th Number (线段树)

K-th Number Time Limit: 20000MS   Memory Limit: 65536K Total Submissions: 46589   Accepted: 15553 Case Time Limit: 2000MS Description You are working for Macrohard company in data structures department. After failing your previous task about key inse

poj2104 K-th Number 主席树入门;

题目链接:K-th Number 题解:我们先把数组离散离散化一下,然后先不考虑L,R的区间的关系,我们有一个棵线段树sum[]保存的是第几大到第几大出现的个数,这样我们想要询问这颗线段数的第k大是多少可以在log(n)次下就找到,但是区间的不同,一颗线段树是解决不了的,那我们如何得到L,R区间的sum数组呢?.我们可以建N棵线段树,第一棵树是空树,然后第一个数过来我们再建一课线段树在原来树的基础上,加上这个数对sum数组的贡献,这样从第一个到第N个建N棵线段树建好,我们可以发现sum[]有前缀

poj 2104 K-th Number 主席树

K-th Number Time Limit: 20000MS   Memory Limit: 65536K Total Submissions: 39737   Accepted: 12955 Case Time Limit: 2000MS Description You are working for Macrohard company in data structures department. After failing your previous task about key inse

【POJ2104】K-th Number——主席树

早上刷NOIP的题刷到有点烦就想学点新东西,然后.....一个早上就这样过去了QAQ.虽然主席树不是NOIP考点,但是...或许我能活到省选呢?(美好的幻想) 题目链接 题目的大意就是给定一个长度为n的区间,给出m个询问,每次询问一个区间[l,r]中第k小的树. 主席树(一种可持久化线段树)的入门题. 推荐一发学习资料:戳这里 感觉人家讲得很仔细了我也没什么讲的必要了...... 总算是学了一种可持久化树了,好像也没想象中那么难?这道题的重点在query函数方面,建议自己在纸上模拟一下建树和查询