题目
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