Loading

洛谷 P3919 【模板】可持久化线段树 1(可持久化数组)

传送门

Version 1: 可持久化线段树

#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using p = pair<int, int>;
const double pi(acos(-1));
const int inf(0x3f3f3f3f);
const int mod(1e9 + 7);
const int maxn(1e6 + 10);
int cnt, a[maxn], root[maxn];

struct node {
    int val, l, r;
} tree[maxn * 40];

template<typename T = int>
inline const T read()
{
    T x = 0, f = 1;
    char ch = getchar();
    while (ch < '0' || ch > '9') {
        if (ch == '-') f = -1;
        ch = getchar();
    }
    while (ch >= '0' && ch <= '9') {
        x = (x << 3) + (x << 1) + ch - '0';
        ch = getchar();
    }
    return x * f;
}

template<typename T>
inline void write(T x, bool ln)
{
    if (x < 0) {
        putchar('-');
        x = -x;
    }
    if (x > 9) write(x / 10, false);
    putchar(x % 10 + '0');
    if (ln) putchar(10);
}

void build(int l, int r, int& cur)
{
    cur = ++cnt;
    if (l == r) {
        tree[cur].val = a[l];
        return;
    }
    int mid = (l + r) >> 1;
    build(l, mid, tree[cur].l);
    build(mid + 1, r, tree[cur].r);
}

void update(int l, int r, int pos, int val, int pre, int& cur)
{
    tree[cur = ++cnt] = tree[pre];
    if (l == r) {
        tree[cur].val = val;
        return;
    }
    int mid = (l + r) >> 1;
    if (pos <= mid) {
        update(l, mid, pos, val, tree[pre].l, tree[cur].l);
    } else {
        update(mid + 1, r, pos, val, tree[pre].r, tree[cur].r);
    }
}

int query(int l, int r, int pos, int cur)
{
    if (l == r) {
        return tree[cur].val;
    }
    int mid = (l + r) >> 1;
    if (pos <= mid) {
        return query(l, mid, pos, tree[cur].l);
    }
    return query(mid + 1, r, pos, tree[cur].r);
}

int main()
{
#ifdef ONLINE_JUDGE
#else
    freopen("input.txt", "r", stdin);
#endif
    ios::sync_with_stdio(false);
    int n = read(), m = read();
    for (int i = 1; i <= n; ++i) {
        a[i] = read();
    }
    build(1, n, root[0]);
    for (int i = 1; i <= m; ++i) {
        int v = read(), op = read(), pos = read();
        if (op == 1) {
            int val = read();
            update(1, n, pos, val, root[v], root[i]);
        } else {
            write(query(1, n, pos, root[i] = root[v]), true);
        }
    }
    return 0;
}

Version 2: pb_ds rope

旧闻平板电视大名,块状链表 rope 可以以 O(1) 的时间复杂度拷贝构造(害怕

然鹅还是被洛谷的毒瘤数据卡掉了 ==

#include <bits/stdc++.h>
#include <ext/rope>

using namespace std;
using namespace __gnu_cxx;
const int maxn(1e6 + 10);
rope<int> rp[maxn];

int main()
{
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
#endif
    ios::sync_with_stdio(false);
    int n, m;
    cin >> n >> m;
    rp[0].append(0);
    for (int i = 1; i <= n; ++i) {
        int x;
        cin >> x;
        rp[0].append(x);
    }
    for (int i = 1; i <= m; ++i) {
        int v, op, pos, val;
        cin >> v >> op >> pos;
        rp[i] = rp[v];
        if (op == 1) {
            cin >> val;
            rp[i].replace(pos, val);
        } else {
            cout << rp[i][pos] << endl;
        }
    }
    return 0;
}
posted @ 2020-10-16 16:51  SDUWH_2U  阅读(114)  评论(0编辑  收藏  举报