主席树——区间第K大

Kth number

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

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


T个样例
n个数 m个询问
(n个数)
左 右 k

#include<iostream>
#include<string>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<math.h>
using namespace std;
const int MAXNODE = 100000+5;
int tre[MAXNODE * 25];
int lc[MAXNODE*25], rc[MAXNODE*25], root[MAXNODE];
int tot,Time;
int ans;
int a[MAXNODE], b[MAXNODE];
void init(void)
{
    tot = 0;
    Time = 0;
}
void build(int t, int l, int r)
{
    tre[t] = 0;
    if (l == r)
    {
        return;
    }
    int mid = l + r >> 1;
    build(lc[t] = ++tot, l, mid);
    build(rc[t] = ++tot, mid + 1, r);
}
void update(int last, int cur, int l, int r, int x)
{
    tre[cur] = tre[last];
    lc[cur] = lc[last];
    rc[cur] = rc[last];
    if (l == r)
    {
        tre[cur]++;
        return;
    }
    int mid = l + r >> 1;
    if (x <= mid)
    {
        update(lc[last], lc[cur] = ++tot, l, mid, x);
    }
    else
    {
        update(rc[last], rc[cur] = ++tot, mid + 1, r, x);
    }
    tre[cur] = tre[lc[cur]] + tre[rc[cur]];
}
void query(int last, int cur, int l, int r, int k)
{
    if (l == r)
    {
        ans = r;
        return;
    }
    int mid = l + r >> 1;
    int d = tre[lc[cur]] - tre[lc[last]];
    if (k<=d)
    {
        query(lc[last], lc[cur], l, mid, k);
    }
    else
    {
        query(rc[last], rc[cur], mid + 1, r, k - d);
    }
}
int main()
{
    int T;
    scanf("%d", &T);
    while (T--)
    {
        int n, m;
        init();
        scanf("%d%d", &n, &m);
        build(root[Time] = ++tot, 1, n);
        int x, y, t;
        for (int i = 0; i < n; i++)
        {
            scanf("%d", &a[i]);
            b[i] = a[i];
        }
        sort(b, b + n);
        int N = unique(b, b + n) - b;
        for (int i = 0; i < n; i++)
        {
            int j = lower_bound(b, b + N, a[i]) - b;
            root[++Time] = ++tot;
            update(root[Time - 1], root[Time], 1, n, j+1);
        }
        for (int i = 0; i < m; i++)
        {
            scanf("%d%d%d", &x, &y, &t);
            ans = 0;
            query(root[x - 1], root[y], 1, n, t);
            ans = b[ans-1];
            printf("%d\n", ans);
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值