Kth number(HDU2665)

题目

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

翻译一下啦~:
给你一个序列,然后问你第k个大数。

输入
第一行是测试用例的数量。对于每个测试用例,第一行包含两个整数n和m(n,m <= 100000),表示序列中的整数数和quaere数。第二行包含n个整数,描述序列。以下m行中的每一行包含三个整数s,t,k。[s,t]表示间隔,k表示区间[s,t]中的第k个大数

输出
对于每个测试用例,输出m行。 每行包含第k个大数字。

思路:一道很经典的主席树入门啦,就是求区间第k大,这里我讲的不是很清楚,可以看看这位大佬讲的 浅谈主席树,然后谈谈我的看法

主席树,首先每个点存的是这个区间的节点数量,一棵树是由很多条链构成,我们每次插入一个新的节点,只需要修改一条链就可以了,这样大大减少了空间的同时也在更新这棵树,然后我们查询的时候,是利用每个数据所在的根节点,比较不同区间的节点数量得到结果~
(因为第一次没有用结构体写树,一开始十分生疏~)

代码如下:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
const int mx=2e6+10;
int ls[mx],rs[mx],rt[mx],sum[mx];//分别代表着左儿子,右儿子,数据所在链,区间节点数量
int a[mx],b[mx],tot;//分别为输入的数据,离散化操作之后的,以及总结点数量

void update(int &now,int pre
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值