基本模板
可持久化线段树就是包含历史状态的线段树;我们需要用动态开点的方式来记录左右儿子。
如果当前左右儿子不发生变化,我们就直接使用上一个历史版本的左右儿子;否则新建节点即可。
#include <cmath>
#include <cstdio>
#include <vector>
#include <cstring>
#include <limits.h>
#include <iostream>
#include <algorithm>
using namespace std;
int n,m,t;
int cnt = 0;
int c[20000];
int root[200000];
struct SegmentTree {
int lc,rc,ans;
} a[10000000];
inline int read(void)
{
int s = 0, w = 1;char c = getchar();
while (c<'0' || c>'9') {if (c == '-') w = -1; c = getchar();}
while (c>='0' && c<='9') s = s*10+c-48,c = getchar();
return s*w;
}
void build(int &root,int l,int r)
{
root = ++cnt;
if (l == r) {a[root].ans = c[l]; return;}
int mid = l+r >> 1;
if (l <= mid) build(a[root].lc,l,mid);
if (r > mid) build(a[root].rc,mid+1,r);
a[root].ans = max(a[a[root].lc].ans,a[a[root].rc].ans);
return;
}
void change(int &root,int pre,int p,int v,int l,int r)
{
root = ++cnt;
a[root] = a[pre];
if (l == r) {a[root].ans = v; return;}
int mid = l+r >> 1;
if (p <= mid) change(a[root].lc,a[pre].lc,p,v,l,mid);
if (p > mid) change(a[root].rc,a[pre].rc,p,v,mid+1,r);
a[root].ans = max(a[a[root].lc].ans,a[a[root].rc].ans);
return;
}
int ask(int root,int askL,int askR,int l,int r)
{
if (askL <= l && askR >= r) return a[root].ans;
int mid = l+r >> 1, ans = 0;
if (askL <= mid) ans = max(ans,ask(a[root].lc,askL,askR,l,mid));
if (askR > mid) ans = max(ans,ask(a[root].rc,askL,askR,mid+1,r));
return ans;
}
int main(void)
{
freopen("longterm_segtree.in","r",stdin);
freopen("longterm_segtree.out","w",stdout);
n = read(), m = read(), t = 0;
for (int i=1;i<=n;++i) c[i] = read();
build(root[++t],1,n);
while (m --)
{
int opt = read(), k = read(), x = read(), y = read();
if (opt == 0) printf("%d\n", ask(root[k],x,y,1,n));
if (opt == 1) change(root[++t],root[k],x,y,1,n);
}
return 0;
}
主席树:区间第k小
建立 n n n棵线段树,第i棵线段树的数字是 1 − i 1-i 1−i,而且需要可持久化来记录历史版本。
对于 [ l , r ] [l,r] [l,r]的询问,其实可以转化为第 r r r棵线段树 − - −第 l l l棵线段树的第 k k k大,我们就可以递归来查找区间第 k k k小。
去过左区间 > k >k >k,就在左区间查找;否则就在右区间查找 k − s i z e [ l ] k-size[l] k−size[l]小即可。
#include <cmath>
#include <cstdio>
#include <vector>
#include <cstring>
#include <limits.h>
#include <iostream>
#include <algorithm>
using namespace std;
int n,m;
struct Tree
{
int ans,lc,rc;
} a[20000000];
struct node
{
int id,val,num;
friend bool operator < (node p1,node p2) {
return p1.val < p2.val;
}
} c[1000000];
int cnt = 0;
int Ans[200000];
int root[200000];
bool cmp(node p1,node p2) {
return p1.id < p2.id;
}
inline int read(void)
{
int s = 0, w = 1;char c = getchar();
while (c<'0' || c>'9') {if (c == '-') w = -1; c = getchar();}
while (c>='0' && c<='9') s = s*10+c-48,c = getchar();
return s*w;
}
void build(int &root,int pre,int l,int r,int pos)
{
root = ++cnt;
a[root].lc = a[pre].lc;
a[root].rc = a[pre].rc;
a[root].ans = a[pre].ans+1;
if (l == r) return;
int mid = l+r >> 1;
if (pos <= mid) build(a[root].lc,a[pre].lc,l,mid,pos);
if (pos > mid) build(a[root].rc,a[pre].rc,mid+1,r,pos);
return;
}
int ask(int root1,int root2,int l,int r,int k)
{
if (l == r) return l;
int mid = l+r >> 1;
int sum = a[a[root1].lc].ans-a[a[root2].lc].ans;//计算左子树的个数
if (sum >= k) return ask(a[root1].lc,a[root2].lc,l,mid,k);
if (sum < k) return ask(a[root1].rc,a[root2].rc,mid+1,r,k-sum);
}
int main(void)
{
freopen("kth.in","r",stdin);
freopen("kth.out","w",stdout);
n = read(), m = read();
for (int i=1;i<=n;++i)
c[i].val = read(), c[i].id = i;
sort(c+1, c+n+1);
for (int i=1;i<=n;++i)
{
if (c[i].val ^ c[i-1].val || i == 1) c[i].num = ++cnt;
else c[i].num = c[i-1].num;
Ans[c[i].num] = c[i].val;
}
sort(c+1, c+n+1, cmp);
for (int i=1;i<=n;++i) build(root[i],root[i-1],1,n,c[i].num);
for (int i=1;i<=m;++i)
{
int l = read(), r = read(), k = read();
printf("%d\n", Ans[ask(root[r],root[l-1],1,n,k)]);
}
return 0;
}