cf375D. Tree and Queries

Posted Jozky86

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了cf375D. Tree and Queries相关的知识,希望对你有一定的参考价值。

cf375D. Tree and Queries

题意:

给你一颗有根树,每个点都有一个颜色,有m次询问,问以u为根的子树中,相同颜色数量超过k的有多少种颜色?

题解:

这个题做法很多,有莫队分块,这里用的dsu
就是对于一个子树内的颜色进行记录,为了方便我们后面的数量统计,对于一个子树中所有颜色出现次数,我们用树状数组来维护。比如颜色1已经出现了2两次,又出现了一次,那么我们就在树状数组中将2的位置减1,将位置3加1,树状数组维护的是颜色出现的数量。这样对于出现次数大于等于k的颜色种类,我们直接query(n)-query(l-1)即可,注意k有可能回比n大,所以要注意特判。
还有树状数组的维护尽可能在dfs过程中,不要单独再循环维护,不然常数太大

代码:

// Problem: D. Tree and Queries
// Contest: Codeforces - Codeforces Round #221 (Div. 1)
// URL: https://codeforces.com/contest/375/problem/D
// Memory Limit: 256 MB
// Time Limit: 1000 ms
// Data:2021-09-03 12:55:52
// By Jozky

#include <bits/stdc++.h>
#include <unordered_map>
#define debug(a, b) printf("%s = %d\\n", a, b);
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> PII;
clock_t startTime, endTime;
//Fe~Jozky
const ll INF_ll= 1e18;
const int INF_int= 0x3f3f3f3f;
void read(){};
template <typename _Tp, typename... _Tps> void read(_Tp& x, _Tps&... Ar)
{
    x= 0;
    char c= getchar();
    bool flag= 0;
    while (c < '0' || c > '9')
        flag|= (c == '-'), c= getchar();
    while (c >= '0' && c <= '9')
        x= (x << 3) + (x << 1) + (c ^ 48), c= getchar();
    if (flag)
        x= -x;
    read(Ar...);
}
template <typename T> inline void write(T x)
{
    if (x < 0) {
        x= ~(x - 1);
        putchar('-');
    }
    if (x > 9)
        write(x / 10);
    putchar(x % 10 + '0');
}
void rd_test()
{
#ifdef LOCAL
    startTime= clock();
    freopen("in.txt", "r", stdin);
#endif
}
void Time_test()
{
#ifdef LOCAL
    endTime= clock();
    printf("\\nRun Time:%lfs\\n", (double)(endTime - startTime) / CLOCKS_PER_SEC);
#endif
}
const int maxn= 2e5 + 9;
int col[maxn];
vector<int> vec[maxn];
int siz[maxn];
int son[maxn], Son;
int ans[maxn];
void dfs(int u, int fa)
{
    siz[u]= 1;
    for (auto v : vec[u]) {
        if (v == fa)
            continue;
        dfs(v, u);
        siz[u]+= siz[v];
        if (siz[v] > siz[son[u]])
            son[u]= v;
    }
}
int va[maxn];

vector<PII> q[maxn];
int f[maxn];
int num[maxn];
int n, m;
int lowbits(int x)
{
    return x & (-x);
}
void update(int pos, int val)
{
    for (int i= pos; i <= n; i+= lowbits(i)) {
        va[i]+= val;
    }
}
int query(int pos)
{
    int sum= 0;
    for (int i= pos; i; i-= lowbits(i)) {
        sum+= va[i];
    }
    return sum;
}
int query(int l, int r)
{
    if (l > r)
        return 0;
    return query(r) - query(l - 1);
}
void cal(int u, int fa, int val)
{
    if (num[col[u]] != 0)
        update(num[col[u]], -1);

    num[col[u]]+= val;

    if (num[col[u]] != 0)
        update(num[col[u]], 1);
    for (auto v : vec[u]) {
        if (v == fa || v == Son)
            continue;
        cal(v, u, val);
    }
}
void dfs2(int u, int fa, int keep)
{
    for (auto v : vec[u]) {
        if (v == fa || v == son[u])
            continue;
        dfs2(v, u, 0);
    }
    if (son[u]) {
        dfs2(son[u], u, 1);
        Son= son[u];
    }
    cal(u, fa, 1);
    for (auto it : q[u]) {
        int k= it.first;
        int id= it.second;
        ans[id]= query(k, n);
    }
    Son= 0;
    if (!keep) {
        cal(u, fa, -1);
    }
}
int main()
{
    //rd_test();

    read(n, m);
    for (int i= 1; i <= n; i++) {
        read(col[i]);
    }
    for (int i= 1; i < n; i++) {
        int u, v;
        read(u, v);
        vec[u].push_back(v);
        vec[v].push_back(u);
    }
    dfs(1, 0);
    for (int i= 1; i <= m; i++) {
        int v, k;
        read(v, k);
        q[v].push_back({k, i});
        // read(q[i].v,q[i].k);
    }
    dfs2(1, 0, 0);
    for (int i= 1; i <= m; i++)
        printf("%d\\n", ans[i]);
    //Time_test();
}

以上是关于cf375D. Tree and Queries的主要内容,如果未能解决你的问题,请参考以下文章

cf375D. Tree and Queries(树上启发式合并+线段树)

CF375D Tree and Queries

题解 CF375D Tree and Queries

题解 CF375D Tree and Queries

CodeForces 375D. Tree and Queries树上启发式合并

Tree and Queries CodeForces - 375D 树上莫队