bzoj4771 七彩树 dfs序+主席树+树链的并

题目传送门

https://lydsy.com/JudgeOnline/problem.php?id=4771

题解

一道不错的树链并的基础练习题。

如果不是树,而是一个数组的话,对于给定区间内的不同颜色数,我们可以维护一个 \(pre_{i, j}\) 表示前 \(i\) 个最后一个 \(j\) 出现的位置。那么答案就是 \(pre_{r, j} \geq l\) 的 \(j\) 的个数,用主席树维护,第一维 \(i\),第二维 \(pre_i, j\),维护的是数量就可以了。(当然这个问题还有别的方法,比如维护 \(nxt_i\) 之类的)

所以类似的,对于一个点 \(x\),维护 \(pre_{x, i}\) 表示从 \(1\) 到 \(x\) 的链上,满足条件的颜色必须 \(pre_{y, i} \in subtree_x\),最后 \(i\) 颜色最后一次出现的位置,同样第一维 \(i\),第二维 \(pre_i, j\),维护的是数量。

但是这个做法显然有问题。如果 \(x\) 的子树中第 \(d\) 层的点数不止一个,那么会有很多重复的。

发现每一个颜色的点都会对从 \(1\) 到自己的链上的点产生贡献,但是一种颜色只能产生一种贡献。所以考虑进行树链的并来维护就可以了。这样做的第一维就是层数而不是编号了。



时间复杂度 \(O(n\log n)\)。

这道题不卡常但是因为我滥用 memset 导致当做卡常题调了半天。

线段树的点数极限很大,但是跑不满,所以不要直接把整个线段树 memset 一遍。


#include<bits/stdc++.h>

#define fec(i, x, y) (int i = head[x], y = g[i].to; i; i = g[i].ne, y = g[i].to)
#define dbg(...) fprintf(stderr, __VA_ARGS__)
#define File(x) freopen(#x".in", "r", stdin), freopen(#x".out", "w", stdout)
#define fi first
#define se second
#define pb push_back

template<typename A, typename B> inline char smax(A &a, const B &b) {return a < b ? a = b , 1 : 0;}
template<typename A, typename B> inline char smin(A &a, const B &b) {return b < a ? a = b , 1 : 0;}

typedef long long ll; typedef unsigned long long ull; typedef std::pair<int, int> pii;

template<typename I> inline void read(I &x) {
    int f = 0, c;
    while (!isdigit(c = getchar())) c == '-' ? f = 1 : 0;
    x = c & 15;
    while (isdigit(c = getchar())) x = (x << 1) + (x << 3) + (c & 15);
    f ? x = -x : 0;
}

const int N = 1e5 + 7;

int n, m, nod, dfc, cnt;
int c[N], rt[N], q[N];
int dep[N], f[N], siz[N], son[N], dfn[N], pre[N], top[N];

struct Node { int lc, rc, val; } t[N * 70];
inline void ins(int &o, int L, int R, int x, int k) {
    ++cnt;
    t[++nod] = t[o], t[o = nod].val += k;
    if (L == R) return;
    int M = (L + R) >> 1;
    if (x <= M) ins(t[o].lc, L, M, x, k);
    else ins(t[o].rc, M + 1, R, x, k);
}
inline int qsum(int o, int L, int R, int l, int r) {
    ++cnt;
    if (!o) return 0;
    if (l <= L && R <= r) return t[o].val;
    int M = (L + R) >> 1;
    if (r <= M) return qsum(t[o].lc, L, M, l, r);
    if (l > M) return qsum(t[o].rc, M + 1, R, l, r);
    return qsum(t[o].lc, L, M, l, r) + qsum(t[o].rc, M + 1, R, l, r);
}

inline void qadd(int &o, int x, int k) { /*dbg("x = %d, k = %d\n", x, k);*/ ins(o, 1, n, dfn[x], k); }
inline int qval(int o, int x) { return qsum(o, 1, n, dfn[x], dfn[x] + siz[x] - 1); }

struct Edge { int to, ne; } g[N << 1]; int head[N], tot;
inline void addedge(int x, int y) { g[++tot].to = y, g[tot].ne = head[x], head[x] = tot; }
inline void adde(int x, int y) { addedge(x, y), addedge(y, x); }

inline void dfs1(int x, int fa = 0) {
    dep[x] = dep[fa] + 1, f[x] = fa, siz[x] = 1;
    for fec(i, x, y) if (y != fa) dfs1(y, x), siz[x] += siz[y], siz[y] > siz[son[x]] && (son[x] = y);
}
inline void dfs2(int x, int pa) {
    top[x] = pa, dfn[x] = ++dfc, pre[dfc] = x;
    if (!son[x]) return; dfs2(son[x], pa);
    for fec(i, x, y) if (y != f[x] && y != son[x]) dfs2(y, y);
}
inline int lca(int x, int y) {
    ++cnt;
    while (top[x] != top[y]) dep[top[x]] > dep[top[y]] ? x = f[top[x]] : y = f[top[y]];
    return dep[x] < dep[y] ? x : y;
}

struct cmp {
    inline bool operator () (const int &x, const int &y) {++cnt; return dfn[x] < dfn[y]; }
};
std::set<int, cmp> s[N];

inline void ins(std::set<int, cmp> &s, int dep, int x) {
    std::set<int, cmp>::iterator p = s.lower_bound(x);
    int y = 0, z = 0;
    qadd(rt[dep], x, 1);
    if (p != s.end()) y = *p, qadd(rt[dep], lca(x, y), -1);
    if (p != s.begin()) z = *--p, qadd(rt[dep], lca(x, z), -1);
    if (y && z) qadd(rt[dep], lca(y, z), 1);
    // dbg("ins: dep = %d, x = %d, y = %d, z = %d, qry = %d, rt[dep] = %d\n", dep, x, y, z, qval(rt[dep], x), rt[dep]);
    s.insert(x);
}

inline void bfs() {
    int hd = 0, tl = 1;
    q[tl] = 1;
    while (hd < tl) {
        int x = q[++hd];
        for fec(i, x, y) if (y != f[x]) q[++tl] = y;
    }
}
inline void build() {
    for (int i = 1; i <= n; ++i) {
        int x = q[i];
        if (dep[x] != dep[q[i - 1]]) rt[dep[x]] = rt[dep[x] - 1];
        ins(s[c[x]], dep[x], x);
    }
}

inline void work() {
    dfs1(1), dfs2(1, 1);
    bfs();
    build();
    int la = 0;
    while (m--) {
        int x, d;
        read(x), read(d);
        x ^= la, d ^= la;
        // dbg("x = %d, d = %d\n", x, d);
        d += dep[x], smin(d, dep[q[n]]);
        printf("%d\n", la = qval(rt[d], x));
        // la = qval(rt[d], x);
    }
    memset(t, 0, sizeof(Node) * (nod + 1));
}

inline void cls() {
    tot = nod = dfc = 0;
    memset(head, 0, sizeof(head));
    memset(son, 0, sizeof(son));
    for (int i = 1; i <= n; ++i) s[i].clear();
}

inline void init() {
    read(n), read(m);
    cls();
    for (int i = 1; i <= n; ++i) read(c[i]);
    for (int i = 2, x; i <= n; ++i) read(x), addedge(x, i);
}

int main() {
#ifdef hzhkk
    freopen("hkk.in", "r", stdin);
#endif
    int T;
    read(T);
    while (T--) {
        init();
        work();
    }
    fclose(stdin), fclose(stdout);
    return 0;
}

原文地址:https://www.cnblogs.com/hankeke/p/bzoj4771.html

时间: 2024-08-04 04:22:47

bzoj4771 七彩树 dfs序+主席树+树链的并的相关文章

【bzoj1803】Spoj1487 Query on a tree III DFS序+主席树

题目描述 You are given a node-labeled rooted tree with n nodes. Define the query (x, k): Find the node whose label is k-th largest in the subtree of the node x. Assume no two nodes have the same labels. 输入 The first line contains one integer n (1 <= n <

51 nod 1681 公共祖先 (主席树+dfs序)

1681 公共祖先 基准时间限制:1 秒 空间限制:131072 KB 分值: 80 难度:5级算法题 有一个庞大的家族,共n人.已知这n个人的祖辈关系正好形成树形结构(即父亲向儿子连边). 在另一个未知的平行宇宙,这n人的祖辈关系仍然是树形结构,但他们相互之间的关系却完全不同了,原来的祖先可能变成了后代,后代变成的同辈…… 两个人的亲密度定义为在这两个平行宇宙有多少人一直是他们的公共祖先. 整个家族的亲密度定义为任意两个人亲密度的总和. Input 第一行一个数n(1<=n<=100000)

【BZOJ1803】Spoj1487 Query on a tree III 主席树+DFS序

[BZOJ1803]Spoj1487 Query on a tree III Description You are given a node-labeled rooted tree with n nodes. Define the query (x, k): Find the node whose label is k-th largest in the subtree of the node x. Assume no two nodes have the same labels. Input

Tsinsen A1505. 树(张闻涛) 倍增LCA,可持久化线段树,DFS序

题目:http://www.tsinsen.com/A1505 A1505. 树(张闻涛) 时间限制:1.0s   内存限制:512.0MB 总提交次数:196   AC次数:65   平均分:58.62 将本题分享到: 查看未格式化的试题   提交   试题讨论 试题来源 2013中国国家集训队第二次作业 问题描述 给定一棵N个节点的树,每个点有一个权值,有M个询问(a,b,c)若a 为1,回答b到c路径上的最小权值,若a为2,回答b到c路径上的最大权值,若a为3,回答b到c路径上的所有权值的

Codeforces 384E 线段树+dfs序

题目链接:点击打开链接 题意: 给定n个点,m个询问的无向树(1为根) 下面n个数表示每个点的权值 下面n-1行给出树 操作1:x点权值+v, x的第 i & 1 的儿子-v, 第 !(i&1) 的儿子+v 操作2:询问x点权值 dfs把树转成序列 根据深度把点分成2组 分别用线段树维护.. 然后Y一下 #include<stdio.h> #include<string.h> #include<iostream> #include<algorith

线段树+dfs序(Apple Tree )(Assign the task )

线段树+dfs序 给定一棵n个节点的树,m次查询,每次查询需要求出某个节点深度为h的所有子节点. 作为预处理,首先将树的所有节点按深度保存起来,每个深度的所有节点用一个线性结构保存,每个深度的节点相对顺序要和前序遍历一致. 然后从树的根节点进行dfs,对于每个节点记录两个信息,一个是dfs进入该节点的时间戳in[id],另一个是dfs离开该节点的时间戳out[id]. 最后对于每次查询,求节点v在深度h的所有子节点,只需将深度为h并且dfs进入时间戳在in[v]和out[v]之间的所有节点都求出

J - Assign the task HDU - 3974 (线段树 + dfs序)

题意:给一颗树,两种操作,查询 i 结点的颜色,和将i结点和它的子树都染成另一种颜色 题解:dfs序构建线段树,对于x和其子树染色就是 l[x] 和 r[x]; dfs序线段树板子 #include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #include<algorithm> #include<cmath> #include<queue>

BZOJ 2809: [Apio2012]dispatching [主席树 DFS序]

传送门 题意:查询树上根节点值*子树中权值和$\le m$的最大数量 最大值是多少 求$DFS$序,然后变成区间中和$\le m$最多有几个元素,建主席树,然后权值线段树上二分就行了 $WA$:又把边表开小了..... 好吧我$zz$了有根树加无向边干什么.... #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; #de

bzoj 3772 精神污染 主席树+dfs序

精神污染 Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 637  Solved: 177[Submit][Status][Discuss] Description 兵库县位于日本列岛的中央位置,北临日本海,南面濑户内海直通太平洋,中央部位是森林和山地,与拥有关西机场的大阪府比邻而居,是关西地区面积最大的县,是集经济和文化于一体的一大地区,是日本西部门户,海陆空交通设施发达.濑户内海沿岸气候温暖,多晴天,有日本少见的贸易良港神户港所在的神户市和曾是豪