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小。
如何理解主席树?主席树可以当做保存了每次修改后的状态的线段树。由于每次添加一个数只沿着一条路径修改节点,所以只会添加logn个新节点,其他节点则指向旧节点以节省空间,所以建树的时间复杂度和空间复杂度都是nlogn,10^5级别的数据logn是17,所以大约要为节点开20*10^5的空间。
节点中储存的是该节点对应的数值区间内,已经插入了几个数,有点类似桶排序。
假设主席树从状态A更新到B,即一组数据从a[A]……a[B]依次插入,同一空间位置的不同时间状态下的节点则表示了这两个状态(区间)之间新增的数。
二分查找第k小,则是判断值在[1,mid]之间的数的个数是否>=k,若是,则从左子树继续二分,若不是,那左子树只有前x小的数,剩下的k-x个数自然要从右子树找。最后找的叶节点就是要找的数。
主席树的结构很好理解,难点还是在于理解找数的原理,线段树储存的是该数列区间的最值,而主席树用类似桶排序的思想,储存的是数值区间的数的个数。
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
using namespace std;
const int MAXN = 100010;
const int M = MAXN * 30;
int x,n,q,m,tot;
int a[MAXN], t[MAXN];
int T[M], lson[M], rson[M], c[M];
void Init_hash()
{
for(int i = 1; i <= n;i++)
t[i] = a[i];
sort(t+1,t+1+n);
m = unique(t+1,t+1+n)-t-1;
}
int build(int l,int r)
{
int root = tot++;
c[root] = 0;
if(l != r)
{
int mid = (l+r)>>1;
lson[root] = build(l,mid);
rson[root] = build(mid+1,r);
}
return root;
//建立一个空树,横坐标l到r是1到m,节点储存的值是该区间内值的个数,即该状态下值是[l,r]内的数有几个
}
int has(int x)
{
return lower_bound(t+1,t+1+m,x) - t;
}
int update(int root,int pos,int val)
{
int newroot = tot++, tmp = newroot;
c[newroot] = c[root] + val;
int l = 1, r = m;
while(l < r)
{
int mid = (l+r)>>1;
if(pos <= mid)
{
lson[newroot] = tot++; rson[newroot] = rson[root];
newroot = lson[newroot]; root = lson[root];
r = mid;
}
else
{
rson[newroot] = tot++; lson[newroot] = lson[root];
newroot = rson[newroot]; root = rson[root];
l = mid+1;
}
c[newroot] = c[root] + val;
}
return tmp;
//从根节点向下更新,相当于每次插入新建一个新状态的线段树,只不过没有改动的部分指向原线段树
}
int query(int left_root,int right_root,int k)
{
int l = 1, r = m;
while( l < r)
{
int mid = (l+r)>>1;
if(c[lson[left_root]]-c[lson[right_root]] >= k )
{
r = mid;
left_root = lson[left_root];
right_root = lson[right_root];
}
else
{
l = mid + 1;
k -= c[lson[left_root]] - c[lson[right_root]];
left_root = rson[left_root];
right_root = rson[right_root];
}
}
return l;
//假设从A,A+1,...,B依次插入数,则状态B时的总个数-状态A时的总个数就是该区间的长度,节点的值发生变动就意味着某个值a[i]插入
//要求第L个数到第R个数之间的第k大,是对数列的值a[L],a[L+1],...,a[R]进行二分查找
//左子树或右子树上,新状态比旧状态增加的个数若大于k,则说明该数的值是在这个区间上
}
int main()
{
//freopen("in.txt","r",stdin);
scanf("%d",&x);
while(x--)
{
scanf("%d%d",&n,&q);
tot = 0;
for(int i = 1;i <= n;i++)
scanf("%d",&a[i]);
Init_hash();
T[n+1] = build(1,m);
for(int i = n;i ;i--)
{
int pos = has(a[i]);
T[i] = update(T[i+1],pos,1);
//从右往左插入数列中的值
}
while(q--)
{
int l,r,k;
scanf("%d%d%d",&l,&r,&k);
printf("%d\n",t[query(T[l],T[r+1],k)]);
}
}
return 0;
}