区间第K小(可持久化线段树)

Posted jpphy0

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了区间第K小(可持久化线段树)相关的知识,希望对你有一定的参考价值。

问题

  • 给定一个序列 a 1 , a 2 , ⋯ , a n a_1,a_2,\\cdots,a_n a1a2an m m m 次操作,每次给定 l , r , k l,r,k lrk,问 a l , a l + 1 , ⋯ , a r a_l,a_{l+1},\\cdots ,a_r alal+1ar中第 k k k 小的值.
    • 1 ≤ a i ≤ n 1 \\leq a_i \\leq n 1ain
    • 1 ≤ n , m ≤ 100000 1\\leq n,m \\leq 100000 1nm100000
    • 1 ≤ l ≤ r ≤ n , 1 ≤ k ≤ r − l + 1 1\\leq l \\leq r\\leq n,1\\leq k \\leq r-l+1 1lrn1krl+1

分析

  • → \\rightarrow 时间
  • n棵线段树维护各个时刻的各个数字的出现次数
  • 增量持久化策略优化线段树

代码

一般形式

#include<bits/stdc++.h>
using namespace std;
const int MXN = 1e5+5;
int N, M, a[MXN], b[MXN], c[MXN], tot, ver[MXN];
struct TreeNode{
	int l, r, sum; // l、r:子树;sum:出现次数
}tree[MXN<<5];
void pushup(int root){
	TreeNode &rt = tree[root];
	TreeNode &ls = tree[rt.l], &rs = tree[rt.r];
	rt.sum = ls.sum + rs.sum;
}
int build(int l, int r){
	int root = ++tot;
	tree[root].sum = 0;
	if(l == r) return root;
	int mid = (l+r)>>1;
	tree[root].l = build(l, mid); // 左子树
	tree[root].r = build(mid+1, r); // 右子树
	return root;
}
int change(int node, int l, int r, int pos){ // node:待更新的子树
	int root = ++tot;
	TreeNode &rt = tree[root];
	rt = tree[node]; // 复制
	if(l == r){
		rt.sum += 1;
		return root;
	}
	int mid = (l+r)>>1;
	if(pos <= mid) rt.l = change(rt.l, l, mid, pos);
	else rt.r = change(rt.r, mid+1, r, pos);
	pushup(root); // 上推更新
	return root;
}
int query(int pre, int lst, int l, int r, int k){ 
	if(l == r) return r;
	TreeNode &pn = tree[pre], &ln = tree[lst];
	int x = tree[ln.l].sum - tree[pn.l].sum;
	int mid = (l+r)>>1;
	if(x >= k) return query(pn.l, ln.l, l, mid, k);
	else return query(pn.r, ln.r, mid+1, r, k-x);
}
int main(){
	int t, L, R, K;
	scanf("%d", &t);	
	while(t--){
		memset(b, 0, sizeof b), tot = 0;
		scanf("%d%d", &N, &M);        
		for(int i = 1; i <= N; ++i) scanf("%d", a+i), b[a[i]] = 1;
		for(int i = 2; i <= N; ++i) b[i] += b[i-1];
		for(int pc=0, i = 1; i <= N; ++i) if(b[i] > b[i-1]) c[++pc] = i;
		ver[0] = build(1, b[N]);
		for(int i = 1; i <= N; ++i)
			ver[i] = change(ver[i-1], 1, b[N], b[a[i]]);
		while(M--){
			scanf("%d%d%d", &L, &R, &K);
			printf("%d\\n", c[query(ver[L-1], ver[R], 1, b[N], K)]);
		}
	}
    return 0;
}

简化形式

#include<bits/stdc++.h>
using namespace std;
const int MXN = 1e5+5;
int N, M, tot, ver[MXN];
struct TreeNode{
	int l, r, sum;
}tree[MXN*20];
int build(int l, int r){ // 初始树
	int root = ++tot;
	tree[root].sum = 0;	
	tree[root].l = root;
	tree[root].r = root;
	return root;
}
int change(int node, int l, int r, int pos){
	int root = ++tot;
	TreeNode &rt = tree[root];
	rt = tree[node], ++rt.sum;
	if(l == r) return root;
	int mid = (l+r)>>1;
	if(pos <= mid) rt.l = change(rt.l, l, mid, pos);
	else rt.r = change(rt.r, mid+1, r, pos);
	return root;
}
int query(int pre, int lst, int l, int r, int k){
	if(l == r) return r;
	TreeNode &pn = tree[pre], &ln = tree[lst];
	int x = tree[ln.l].sum - tree[pn.l].sum;
	int mid = (l+r)>>1;
	if(x >= k) return query(pn.l, ln.l, l, mid, k);
	else return query(pn.r, ln.r, mid+1, r, k-x);
}
int main(){
	int t, x, L, R, K;
	scanf("%d", &t);	
	while(t--){
		tot = 0;
		scanf("%d%d", &N, &M);
		ver[0] = build(1, N);       
		for(int i = 1; i <= N; ++i){
			scanf("%d", &x);		
			ver[i] = change(ver[i-1], 1, N, x);
		}
		while(M--){
			scanf("%d%d%d", &L, &R, &K);
			printf("%d\\n", query(ver[L-1], ver[R], 1, N, K));
		}
	}
    return 0;
}

以上是关于区间第K小(可持久化线段树)的主要内容,如果未能解决你的问题,请参考以下文章

HDU 2665.Kth number-无修改区间第K小-可持久化线段树(主席树)模板

主席树(区间第k小)

可持久化

luogu P3834 模板可持久化线段树 1(主席树)| 静态第k小问题^&

9018:2208可持久化线段树2

poj2104 求区间第k大 可持久化线段树