可持续化线段树(主席树)几种用法总结

1.权值主席树

Acwing255. 第K小数icon-default.png?t=M666https://www.acwing.com/problem/content/257/

最经典的主席树用法,使用主席树来避免建立多个具有重复结点的权值线段树,直接通过不同版本间的信息维护出区间的最大最小值。

#include <iostream>
#include<cstring>
#include<algorithm>
#include<sstream>
#include<cmath>
#include<queue>
#include<bitset>
#include<vector>
#include<map>
#include<unordered_map>
#define int long long
#define endl '\n'
#define lowbit(x) (x) &(-x)
#define mh(x) memset(x, -1, sizeof h)
#define debug(x) cerr << #x << "=" << x << endl;
#define brk exit(0);
using namespace std;
void TLE(){ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);}
const int N = 2e5 + 10;
const int M = 2 * N;
const int mod = 998244353;
const double esp = 1e-6;
const double pi = acos(-1);
typedef pair<int, int> PII;
typedef long long ll;
int n, m;
int a[N];
int root[N];
struct node
{
    int l, r;
    int cnt;
}t[N*40];
int idx;
vector<int> alls;
int id(int x)
{
    return lower_bound(alls.begin(), alls.end(), x) - alls.begin();
}
int build(int l,int r)
{
    int q = ++idx;
    if (l==r)
        return q;
    int mid = l + r >> 1;
    t[q].l = build(l, mid), t[q].r = build(mid + 1, r);
    return q;
}
int insert(int p,int l,int r,int x)
{
    int q = ++idx;
    t[q] = t[p];
    if(l==r)
    {
        t[q].cnt++;
        return q;
    }
    int mid = l + r >> 1;
    if(x<=mid)
        t[q].l = insert(t[p].l, l, mid, x);
    else
        t[q].r = insert(t[p].r, mid + 1, r, x);
    t[q].cnt = t[t[q].l].cnt + t[t[q].r].cnt;
    return q;
}
int query(int q,int p,int l,int r,int k)
{
    if(l==r)
        return l;
    int cnt = t[t[q].l].cnt - t[t[p].l].cnt;
    int mid = l + r >> 1;
    if(cnt>=k)
        return query(t[q].l, t[p].l, l, mid, k);
    else
        return query(t[q].r, t[p].r, mid + 1, r, k-cnt);
}
signed main()
{
    scanf("%lld%lld", &n, &m);
    for (int i = 1; i <= n;i++)
    {
        scanf("%lld", &a[i]);
        alls.push_back(a[i]);
    }
    sort(alls.begin(), alls.end());
    alls.erase(unique(alls.begin(), alls.end()), alls.end());
    root[0] = build(0, alls.size()-1);
    for (int i = 1; i <= n;i++)
        root[i] = insert(root[i - 1], 0, alls.size()-1, id(a[i]));
    while(m--)
    {
        int l, r, k;
        scanf("%lld%lld%lld", &l, &r, &k);
        printf("%lld\n", alls[query(root[r], root[l - 1], 0, alls.size()-1, k)]);
    }
}

2.单个历史版本修改主席树

P3919 【模板】可持久化线段树 1(可持久化数组)icon-default.png?t=M666https://www.luogu.com.cn/problem/P3919

#include <iostream>
#include <cstring>
#include <algorithm>
#include <sstream>
#include <cmath>
#include <queue>
#include <bitset>
#include <vector>
#include <map>
#include <unordered_map>
//#define int long long
#define endl '\n'
#define lowbit(x) (x) & (-x)
#define mh(x) memset(x, -1, sizeof h)
#define debug(x) cerr << #x << "=" << x << endl;
#define brk exit(0);
using namespace std;
void TLE() { ios::sync_with_stdio(false), cin.tie(0), cout.tie(0); }
const int N = 2e6 + 10;
const int M = 2 * N;
const int mod = 998244353;
const double esp = 1e-6;
const double pi = acos(-1);
typedef pair<int, int> PII;
typedef long long ll;
int n, m, idx;
int a[N];
int root[N];
struct node
{
    int l, r;
    int val;
} t[N * 20];
int build(int l, int r)
{
    int q = ++idx;
    if (l == r)
    {
        t[q].val = a[l];
        return q;
    }
    int mid = l + r >> 1;
    t[q].l = build(l, mid), t[q].r = build(mid + 1, r);
    return q;
}
int modify(int p, int l, int r, int k, int x)
{
    int q = ++idx;
    t[q] = t[p];
    if (l == r)
    {
        t[q].val = x;
        return q;
    }
    int mid = l + r >> 1;
    if (k <= mid)
        t[q].l = modify(t[p].l, l, mid, k, x);
    else
        t[q].r = modify(t[p].r, mid + 1, r, k, x);
    return q;
}

int query(int p, int l, int r, int k)
{
    if (l == r)
    {
        return t[p].val;
    }
    int mid = l + r >> 1;
    if (k <= mid)
        return query(t[p].l, l, mid, k);
    else
        return query(t[p].r, mid + 1, r, k);
}

int main()
{
    scanf("%d%d", &n, &m);
    for (int i = 1; i <= n; i++)
    {
        scanf("%d", &a[i]);
    }
    root[0] = build(1, n);
    int cnt = 0;
    while (m--)
    {
        int v, op, loc, val;
        scanf("%d%d%d", &v, &op, &loc);
        if (op == 1)
        {
            scanf("%d", &val);
            root[++cnt] = modify(root[v], 1, n, loc, val);
        }
        else
        {
            root[++cnt] = root[v];
            printf("%d\n",query(root[v], 1, n, loc));
        }
    }
}

3.这里记录一种不用初始建树的建树方法

P1383 高级打字机icon-default.png?t=M666https://www.luogu.com.cn/problem/P1383

这种方法很类似与普通线段树的开点方法,不过要对每一个版本的线段树都要维护一个代表当前版本线段树已存元素个数的len值,在树上进行二分开点。

#include <iostream>
#include <cstring>
#include <algorithm>
#include <sstream>
#include <cmath>
#include <queue>
#include <bitset>
#include <vector>
#include <map>
#include <unordered_map>
#define int long long
#define endl '\n'
#define lowbit(x) (x) & (-x)
#define mh(x) memset(x, -1, sizeof h)
#define debug(x) cerr << #x << "=" << x << endl;
#define brk exit(0);
using namespace std;
void TLE() { ios::sync_with_stdio(false), cin.tie(0), cout.tie(0); }
const int N = 2e5 + 10;
const int M = 2 * N;
const int mod = 998244353;
const double esp = 1e-6;
const double pi = acos(-1);
typedef pair<int, int> PII;
typedef long long ll;
int root[N], idx;
int len[N];
struct node
{
    int l, r;
    char v;
} t[N * 20];
int insert(int p, int l, int r, int f, char v)
{
    int q = ++idx;
    t[q] = t[p];
    if (l == r)
    {
        t[q].v = v;
        return q;
    }
    int mid = l + r >> 1;
    if (f <= mid)
        t[q].l = insert(t[p].l, l, mid, f, v);
    else
        t[q].r = insert(t[p].r, mid + 1, r, f, v);
    return q;
}

char query(int p, int l, int r, int f)
{
    if (l == r)
        return t[p].v;
    int mid = l + r >> 1;
    if (f <= mid)
        return query(t[p].l, l, mid, f);
    else
        return query(t[p].r, mid + 1, r, f);
}

signed main()
{
    TLE();
    int n;
    cin >> n;
    int cnt = 0;
    for (int i = 1; i <= n; i++)
    {
        char op[2], c[2];
        int x;
        cin >> op;
        if (*op == 'T')
        {
            cin >> c;
            ++cnt;
            len[cnt] = len[cnt - 1] + 1;
            root[cnt] = insert(root[cnt - 1], 1, n, len[cnt], *c);
        }
        else if (*op == 'Q')
        {
            cin >> x;
            cout << query(root[cnt], 1, n, x) << endl;
        }
        else if (*op == 'U')
        {
            cin >> x;
            ++cnt;
            root[cnt] = root[cnt - x - 1]; 
            len[cnt] = len[cnt - x - 1];
        }
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值