树链剖分 入门题(洛谷)

最近学习了树链剖分。
发现这个东西其实并不是很难,而是因为代码量比较长,在码的过程中容易出错。
但不过其中还是用套路可循的,如果要学习树链剖分必须要知道DFS序和线段树。
这里就不详细讲了,只给出一些入门题,供个人参考。
https://www.luogu.com.cn/problem/P3384
这是一个模板题
首先树链剖分有两个dfs其中一个dfs是处理轻重儿子的,另外一个dfs是维护重链上的信息的,以及子树上的信息,因为优先处理重链的dfs序,因此链上信息可以区间处理,同时子树本身就可以用dfs序变成区间处理。同时处理树链上的信息,我觉得就是重链的合并。

#include "bits/stdc++.h"

using namespace std;
inline int read() {
    int x = 0;
    bool f = 1;
    char c = getchar();
    for (; !isdigit(c); c = getchar()) if (c == '-') f = 0;
    for (; isdigit(c); c = getchar()) x = (x << 3) + (x << 1) + c - '0';
    if (f) return x;
    return 0 - x;
}
typedef long long ll;
const int maxn = 200000 + 10;
const ll mod = 1000000000 + 7;
struct edge {
    int v, next;
} ed[maxn << 1];
struct node {
    int l, r, rs, ls, sum, lazy;
} t[maxn << 2];

int rt, n, m, r, p, val[maxn], cnt, head[maxn], top[maxn], id[maxn];
int f[maxn], dep[maxn], sz[maxn], son[maxn], rk[maxn];

void add_edge(int u, int v) {
    ++cnt;
    ed[cnt].v = v;
    ed[cnt].next = head[u];
    head[u] = cnt;
}
void dfs1(int u, int fa, int deep) {
    f[u] = fa, dep[u] = deep, sz[u] = 1;
    for (int i = head[u]; i; i = ed[i].next) {
        int v = ed[i].v;
        if (v == fa) continue;
        dfs1(v, u, deep + 1);
        sz[u] += sz[v];
        if (sz[v] > sz[son[u]]) son[u] = v;
    }
}
void dfs2(int u, int t) {
    top[u] = t, id[u] = ++cnt, rk[cnt] = u;
    if (!son[u]) return;
    dfs2(son[u], t);
    for (int i = head[u]; i; i = ed[i].next) {
        int v = ed[i].v;
        if (v != son[u] && v != f[u]) dfs2(v, v);
    }
}
#define lson t[o].ls
#define rson t[o].rs
void pushup(int o) {
    t[o].sum = (t[lson].sum + t[rson].sum) % p;
}
void build(int l, int r, int o) {
    t[o].l = l, t[o].r = r;
    if (l == r) {
        t[o].sum = val[rk[l]];
        return;
    }
    int mid = l + r >> 1;
    lson = ++cnt, rson = ++cnt;
    build(l, mid, lson);
    build(mid + 1, r, rson);
    pushup(o);
}
#define len(o) (t[o].r-t[o].l+1)

void pushdown(int o) {
    if (t[o].lazy) {
        int lz = t[o].lazy;
        t[lson].lazy = (t[lson].lazy + lz) % p;
        t[rson].lazy = (t[rson].lazy + lz) % p;
        t[lson].sum = (t[lson].sum + len(lson) * lz % p) % p;
        t[rson].sum = (t[rson].sum + len(rson) * lz % p) % p;
        t[o].lazy = 0;
    }
}
void update(int l, int r, int c, int o) {
    if (t[o].l >= l && t[o].r <= r) {
        t[o].lazy = (t[o].lazy + c) % p;
        t[o].sum = (t[o].sum + c * len(o) % p) % p;
        return;
    }
    pushdown(o);
    int mid = (t[o].l + t[o].r) >> 1;
    if (l <= mid) update(l, r, c, lson);
    if (r > mid) update(l, r, c, rson);
    pushup(o);
}
int query(int l, int r, int o) {
    if (t[o].l >= l && t[o].r <= r) return t[o].sum;
    pushdown(o);
    int mid = (t[o].l + t[o].r) >> 1;
    int ret = 0;
    if (l <= mid) ret += query(l, r, lson);
    if (r > mid) ret += query(l, r, rson);
    return ret % p;
}
void modify(int x, int y, int c) {
    while (top[x] != top[y]) {
        if (dep[top[x]] < dep[top[y]])
            swap(x, y);
        update(id[top[x]], id[x], c, rt);
        x = f[top[x]];
    }
    if (id[x] > id[y]) swap(x, y);
    update(id[x], id[y], c, rt);
}
int sum(int x, int y) {
    int ret = 0;
    while (top[x] != top[y]) {
        if (dep[top[x]] < dep[top[y]])
            swap(x, y);
        ret = (ret + query(id[top[x]], id[x], rt)) % p;
        x = f[top[x]];
    }
    if (id[x] > id[y]) swap(x, y);
    return (ret + query(id[x], id[y], rt)) % p;
}

int main() {
    scanf("%d%d%d%d", &n, &m, &r, &p);
    for (int i = 1; i <= n; i++) {
        scanf("%d", &val[i]);
    }
    int x, y;
    for (int i = 1; i < n; i++) {
        scanf("%d%d", &x, &y);
        add_edge(x, y), add_edge(y, x);
    }
    cnt = 0;
    dfs1(r, 0, 0);
    dfs2(r, r);
    cnt = 0;
    build(1, n, rt = cnt);
    int op, k;
    for (int i = 1; i <= m; i++) {
        scanf("%d", &op);
        if (op == 1) {
            scanf("%d%d%d", &x, &y, &k);
            modify(x, y, k);
        } else if (op == 2) {
            scanf("%d%d", &x, &y);
            printf("%d\n", sum(x, y));
        } else if (op == 3) {
            scanf("%d%d", &x, &y);
            update(id[x], id[x] + sz[x] - 1, y, rt);
        } else {
            scanf("%d", &x);
            printf("%d\n", query(id[x], id[x] + sz[x] - 1, rt));
        }
    }
    return 0;
}

https://www.luogu.com.cn/problem/P2146
P2146 [NOI2015]软件包管理器
这道题也不难,知道线段树维护什么,线段树应维护一个区间有多个没有安装和安装了多少个,安装一个就是询问结点到根有多少个没有安装。卸载一个就是询问子树安装了多少个。

#include "bits/stdc++.h"

using namespace std;
inline int read() {
    int x = 0;
    bool f = 1;
    char c = getchar();
    for (; !isdigit(c); c = getchar()) if (c == '-') f = 0;
    for (; isdigit(c); c = getchar()) x = (x << 3) + (x << 1) + c - '0';
    if (f) return x;
    return 0 - x;
}
typedef long long ll;
const int maxn = 200000 + 10;
const ll mod = 1000000000 + 7;
struct edge {
    int v, next;
} ed[maxn << 1];
struct node {
    int l, r, rs, ls, sum, lazy;
} a[maxn << 2];

int rt, n, m, val[maxn], cnt, head[maxn], top[maxn], id[maxn];
int f[maxn], dep[maxn], sz[maxn], son[maxn], rk[maxn];

void add_edge(int u, int v) {
    ++cnt;
    ed[cnt].v = v;
    ed[cnt].next = head[u];
    head[u] = cnt;
}
void dfs1(int u, int fa, int deep) {
    sz[u] = 1, f[u] = fa, dep[u] = deep;
    for (int i = head[u]; i; i = ed[i].next) {
        int v = ed[i].v;
        if (v == fa) continue;
        dfs1(v, u, deep + 1);
        sz[u] += sz[v];
        if (sz[v] > sz[son[u]])
            son[u] = v;
    }
}
void dfs2(int u, int t) {
    top[u] = t, id[u] = ++cnt, rk[cnt] = u;
    if (!son[u]) return;
    dfs2(son[u], t);
    for (int i = head[u]; i; i = ed[i].next) {
        int v = ed[i].v;
        if (v != son[u] && v != f[u])
            dfs2(v, v);
    }
}
#define lson a[o].ls
#define rson a[o].rs
#define len(o) (a[o].r-a[o].l+1)

void pushup(int o) {
    a[o].sum = a[lson].sum + a[rson].sum;
}
void build(int l, int r, int o) {
    a[o].l = l, a[o].r = r;
    a[o].lazy = -1;
    if (l == r) return;
    int mid = l + r >> 1;
    lson = ++cnt, rson = ++cnt;
    build(l, mid, lson);
    build(mid + 1, r, rson);
}
void pushdown(int o) {
    if (a[o].lazy != -1) {
        a[lson].lazy = a[rson].lazy = a[o].lazy;
        a[lson].sum = len(lson) * a[o].lazy;
        a[rson].sum = len(rson) * a[o].lazy;
        a[o].lazy = -1;
    }
}
void update(int l, int r, int o, int f) {
    if (a[o].l >= l && a[o].r <= r) {
        a[o].lazy = f;
        a[o].sum = f * len(o);
        return;
    }
    pushdown(o);
    int mid = a[o].l + a[o].r >> 1;
    if (l <= mid) update(l, r, lson, f);
    if (r > mid) update(l, r, rson, f);
    pushup(o);
}
int query(int l, int r, int o) {
    if (a[o].l >= l && a[o].r <= r) {
        return len(o) - a[o].sum;
    }
    pushdown(o);
    int mid = a[o].l + a[o].r >> 1, ret = 0;
    if (l <= mid) ret += query(l, r, lson);
    if (r > mid) ret += query(l, r, rson);
    return ret;
}
int sum(int x, int y) {
    int ret = 0;
    while (top[x] != top[y]) {
        if (dep[top[x]] < dep[top[y]])
            swap(x, y);
        ret += query(id[top[x]], id[x], rt);
        x = f[top[x]];
    }
    if (id[x] > id[y]) swap(x, y);
    return ret + query(id[x], id[y], rt);
}
void modify(int x, int y, int c) {
    while (top[x] != top[y]) {
        if (dep[top[x]] < dep[top[y]])
            swap(x, y);
        update(id[top[x]], id[x], rt, c);
        x = f[top[x]];
    }
    if (id[x] > id[y]) swap(x, y);
    update(id[x], id[y], rt, c);
}
int main() {
//    scanf("%d", &n);
    n = read();
    int x;
    for (int i = 1; i < n; i++) {
//        scanf("%d", &x);
        x = read();
        add_edge(x + 1, i + 1);
        add_edge(i + 1, x + 1);
    }
    cnt = 0;
    dfs1(1, 0, 0);
    dfs2(1, 1);
    cnt = 0;
    build(1, n, rt = cnt);
//    scanf("%d", &m);
    m = read();
//    string tmp;
    char tmp[11];
    for (int i = 1; i <= m; i++) {
        scanf("%s", tmp);
        x = read();
        if (tmp[0] == 'i') {
            x++;
            printf("%d\n", sum(1, x));
            modify(1, x, 1);
        } else {
            x++;
            printf("%d\n", sz[x] - query(id[x], id[x] + sz[x] - 1, rt));
            update(id[x], id[x] + sz[x] - 1, rt, 0);
        }
    }
    return 0;
}

https://www.luogu.com.cn/problem/P2590
P2590 [ZJOI2008]树的统计
这道题也是一个模板题,线段树维护最大值和区间和就可以了。代码就不贴了,只需要稍微改一改上面的代码。
https://www.luogu.com.cn/problem/P3258
P3258 [JLOI2014]松鼠的新家
这道题也是一个模板题,每条链加上1,然后询问每一个点就可以了。
https://www.luogu.com.cn/problem/P3178
P3178 [HAOI2015]树上操作
这道题可能还没有模板题难。
https://www.luogu.com.cn/problem/P2486
P2486 [SDOI2011]染色
这道题思路不难想到,但不过你不仅仅要考虑线段树中合并时可能颜色相同,你还有考虑链合并时颜色 相同。

#include "bits/stdc++.h"

using namespace std;
inline int read() {
    int x = 0;
    bool f = 1;
    char c = getchar();
    for (; !isdigit(c); c = getchar()) if (c == '-') f = 0;
    for (; isdigit(c); c = getchar()) x = (x << 3) + (x << 1) + c - '0';
    if (f) return x;
    return 0 - x;
}
typedef long long ll;
const int maxn = 100000 + 10;
const ll mod = 1000000000 + 7;
struct edge {
    int v, next;
} ed[maxn << 1];
struct node {
    int l, r, rs, ls;
    int lc, rc, sum, lazy;
} a[maxn << 2];

int rt, n, m, val[maxn], cnt, head[maxn], top[maxn], id[maxn];
int f[maxn], dep[maxn], sz[maxn], son[maxn], rk[maxn];

void add_edge(int u, int v) {
    ++cnt;
    ed[cnt].v = v;
    ed[cnt].next = head[u];
    head[u] = cnt;
}
void dfs1(int u, int fa, int deep) {
    sz[u] = 1, f[u] = fa, dep[u] = deep;
    for (int i = head[u]; i; i = ed[i].next) {
        int v = ed[i].v;
        if (v == fa) continue;
        dfs1(v, u, deep + 1);
        sz[u] += sz[v];
        if (sz[v] > sz[son[u]]) son[u] = v;
    }
}
void dfs2(int u, int t) {
    top[u] = t, id[u] = ++cnt, rk[cnt] = u;
    if (!son[u]) return;
    dfs2(son[u], t);
    for (int i = head[u]; i; i = ed[i].next) {
        int v = ed[i].v;
        if (v != f[u] && v != son[u]) dfs2(v, v);
    }
}

#define lson a[o].ls
#define rson a[o].rs
#define len(o) (a[o].r-a[o].l+1)

void pushup(int o) {
    a[o].sum = a[lson].sum + a[rson].sum;
    if (a[lson].rc == a[rson].lc) a[o].sum--;
    a[o].lc = a[lson].lc;
    a[o].rc = a[rson].rc;
}
void build(int l, int r, int o) {
    a[o].l = l, a[o].r = r, a[o].lazy = 0;
    if (l == r) {
        a[o].lc = a[o].rc = val[rk[l]];
        a[o].sum = 1;
        return;
    }
    int mid = l + r >> 1;
    a[o].ls = ++cnt, a[o].rs = ++cnt;
    build(l, mid, lson);
    build(mid + 1, r, rson);
    pushup(o);
}

void up(int o, int lz) {
    a[o].lazy = lz;
    a[o].lc = a[o].rc = lz;
    a[o].sum = 1;
}

void pushdown(int o) {
    if (a[o].lazy) {
        up(lson, a[o].lazy);
        up(rson, a[o].lazy);
        a[o].lazy = 0;
    }
}
void update(int l, int r, int o, int c) {
    if (a[o].l >= l && a[o].r <= r) {
        up(o, c);
        return;
    }
    pushdown(o);
    int mid = a[o].l + a[o].r >> 1;
    if (l <= mid) update(l, r, lson, c);
    if (r > mid) update(l, r, rson, c);
    pushup(o);
}
int query(int l, int r, int o) {
    if (a[o].l == l && a[o].r == r) {
        return a[o].sum;
    }
    pushdown(o);
    int mid = a[o].l + a[o].r >> 1, ret = 0;
    if (r <= mid) ret = query(l, r, lson);
    else if (l > mid) ret = query(l, r, rson);
    else {
        ret = query(l, mid, lson) + query(mid + 1, r, rson);
        if (a[lson].rc == a[rson].lc) ret--;
    }
    return ret;
}
int cquery(int o, int p) {
    if (a[o].l == a[o].r) return a[o].lc;
    pushdown(o);
    int mid = a[o].l + a[o].r >> 1;
    if (p <= mid) return cquery(lson, p);
    else return cquery(rson, p);
}
void modify(int x, int y, int c) {
    while (top[x] != top[y]) {
        if (dep[top[x]] < dep[top[y]])
            swap(x, y);
        update(id[top[x]], id[x], rt, c);
        x = f[top[x]];
    }
    if (id[x] > id[y]) swap(x, y);
    update(id[x], id[y], rt, c);
}
int sum(int x, int y) {
    int ret = 0, last1 = -1, last2 = -1;
    int LC = 0, RC = 0;
    while (top[x] != top[y]) {
        if (dep[top[x]] < dep[top[y]])
            swap(x, y), swap(last1, last2);
        ret += query(id[top[x]], id[x], rt);
        LC = cquery(rt, id[top[x]]);
        RC = cquery(rt, id[x]);
        if (RC == last1) ret--;
        last1 = LC;
        x = f[top[x]];
    }
    if (id[x] > id[y]) swap(x, y), swap(last1, last2);
    ret += query(id[x], id[y], rt);
    LC = cquery(rt, id[x]);
    RC = cquery(rt, id[y]);
    if (LC == last1) ret--;
    if (RC == last2) ret--;
    return ret;
}

int main() {
    n = read(), m = read();
    for (int i = 1; i <= n; i++) {
        val[i] = read();
    }
    int x, y, z;
    for (int i = 1; i < n; i++) {
        x = read(), y = read();
        add_edge(x, y);
        add_edge(y, x);
    }
    cnt = 0;
    dfs1(1, 0, 0);
    dfs2(1, 1);
    cnt = 0;
    build(1, n, rt = cnt);
    char op[10];
    while (m--) {
        scanf("%s", op);
        x = read(), y = read();
        if (op[0] == 'C') {
            z = read();
            modify(x, y, z);
        } else {
            printf("%d\n", sum(x, y));
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值