Codeforces 620E New Year Tree【DFS序+线段树区间染色+二进制思维+位运算】

本文介绍了一种利用线段树和DFS序处理树上颜色更新及查询的问题,通过二进制思想实现颜色的快速统计。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

The New Year holidays are over, but Resha doesn’t want to throw away the New Year tree. He invited his best friends Kerim and Gural to help him to redecorate the New Year tree.
The New Year tree is an undirected tree with n vertices and root in the vertex 1.
You should process the queries of the two types:
Change the colours of all vertices in the subtree of the vertex v to the colour c.
Find the number of different colours in the subtree of the vertex v.
Input
The first line contains two integers n, m (1 ≤ n, m ≤ 4·105) — the number of vertices in the tree and the number of the queries.
The second line contains n integers ci (1 ≤ ci ≤ 60) — the colour of the i-th vertex.
Each of the next n - 1 lines contains two integers xj, yj (1 ≤ xj, yj ≤ n) — the vertices of the j-th edge. It is guaranteed that you are given correct undirected tree.
The last m lines contains the description of the queries. Each description starts with the integer tk (1 ≤ tk ≤ 2) — the type of the k-th query. For the queries of the first type then follows two integers vk, ck (1 ≤ vk ≤ n, 1 ≤ ck ≤ 60) — the number of the vertex whose subtree will be recoloured with the colour ck. For the queries of the second type then follows integer vk (1 ≤ vk ≤ n) — the number of the vertex for which subtree you should find the number of different colours.
Output
For each query of the second type print the integer a — the number of different colours in the subtree of the vertex given in the query.
Each of the numbers should be printed on a separate line in order of query appearing in the input.
Examples
input
7 10
1 1 1 1 1 1 1
1 2
1 3
1 4
3 5
3 6
3 7
1 3 2
2 1
1 4 3
2 1
1 2 5
2 1
1 6 4
2 1
2 2
2 3
output
2
3
4
5
1
2

题意:
给你n个点,每个点都有一个初始颜色,然后搭建成一棵根节点为1的树,下面m个操作,操作共两种:1 x y 把以x节点为根节点的子树全染成y色,2 x 查询以x节点为根节点的子树颜色总数.
分析:
这道题关键是二进制的思维,把60种颜色用二进制枚举出来然后进行或运算,最后统计二进制中1的个数就是颜色总数。其次就是,DFS序预处理注意建树,用线段树维护区间染色,复杂度:O(410560log[4105])O(5108)O(4∗105∗60∗log[4∗105])≈O(5∗108) .

#include <cstdio>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cmath>
#include <vector>
using namespace std;
typedef long long LL;

const LL MAXN = 4e5 + 5;
LL col[MAXN << 2], lazy[MAXN << 2];
LL in[MAXN], out[MAXN], vis[MAXN], num[MAXN];
vector<LL> G[MAXN];
LL time = 0;

void dfs(LL x, LL fa) {
    in[x] = ++time;
    num[time] = x;//把线性结构记录下来
    for(LL i = 0; i < G[x].size(); i++) {
        LL cnt = G[x][i];
        if(cnt == fa) continue;
        dfs(cnt, x);
    }
    out[x] = time;
}

void pushup(LL root) {
    col[root] = col[root << 1] | col[root << 1 | 1];
}

void pushdown(LL root) {
    if(lazy[root]) {
        lazy[root << 1] = lazy[root];
        lazy[root << 1 | 1] = lazy[root];
        col[root << 1] = lazy[root];
        col[root << 1 | 1] = lazy[root];
        lazy[root] = 0;
    }
}

void build(LL root, LL L, LL R) {
    if(L == R) {
        col[root] = vis[num[L]]; //建树时注意这一步,因为经过了DFS序的预处理
        return ;
    }
    LL mid = (L + R) >> 1;
    build(root << 1, L, mid);
    build(root << 1 | 1, mid + 1, R);
    pushup(root);
}

void updata(LL root, LL L, LL R, LL l, LL r, LL c) {
    if(l <= L && R <= r) {
        col[root] = c;
        lazy[root] = c;
        return ;
    }
    pushdown(root);
    LL mid = (L + R) >> 1;
    if(l <= mid) updata(root << 1, L, mid, l, r, c);
    if(r > mid) updata(root << 1 | 1, mid + 1, R, l, r, c);
    pushup(root);
}

LL query(LL root, LL L, LL R, LL l, LL r) {
    if(l <= L && R <= r) {
        return col[root];
    }
    pushdown(root);
    LL ans = 0;
    LL mid = (L + R) >> 1;
    if(l <= mid) ans |= query(root << 1, L, mid, l, r);
    if(r > mid) ans |= query(root << 1 | 1, mid + 1, R, l, r);
    return ans;
}

LL judge(LL x) {
    LL ans = 0;
    while(x) {
        if(x % 2 == 1) ans++;
        x /= 2;
    }
    return ans;
}

int main() {
    LL n, m;
    memset(col, 0, sizeof(col));
    memset(lazy, 0, sizeof(lazy));
    scanf("%lld %lld", &n, &m);
    for(LL i = 1; i <= n; i++) {
        LL c;
        scanf("%lld", &c);
        vis[i] = 1LL << (c - 1); //注意longlong中1LL
    }
    for(LL i = 1; i < n; i++) {
        LL x, y;
        scanf("%lld %lld", &x, &y);
        G[x].push_back(y);
        G[y].push_back(x);
    }
    dfs(1, -1);
    build(1, 1, n); //后续建树
    // for(LL i = 1; i <= n; i++) {
    //     printf("%lld -> %lld\n", i, in[i]);
    // }
    while(m--) {
        LL op, x, c;
        scanf("%lld", &op);
        if(op == 1) {
            scanf("%lld %lld", &x, &c);
            LL L = in[x];
            LL R = out[x];
            c = 1LL << (c - 1);
            updata(1, 1, n, L, R, c);
        }
        else {
            scanf("%lld", &x);
            LL L = in[x];
            LL R = out[x];
            printf("%lld\n", judge(query(1, 1, n, L, R)));
        }
    }
    return 0;
}

/*这组数据卡1LL,确实不容忽视
10 10
39 50 50 7 39 7 46 7 39 7
10 7
7 3
3 5
3 4
6 4
1 4
1 8
8 2
2 9
2 8
1 6 50
2 4
2 6
1 7 39
1 3 39
2 9
1 1 15
2 7
1 10 7
*/
当前提供的引用内容并未提及关于Codeforces比赛M1的具体时间安排[^1]。然而,通常情况下,Codeforces的比赛时间会在其官方网站上提前公布,并提供基于不同时区的转换工具以便参赛者了解具体开赛时刻。 对于Codeforces上的赛事而言,如果一场名为M1的比赛被计划举行,则它的原始时间一般按照UTC(协调世界时)设定。为了得知该场比赛在UTC+8时区的确切开始时间,可以遵循以下逻辑: - 前往Codeforces官网并定位至对应比赛页面。 - 查看比赛所标注的标准UTC起始时间。 - 将此标准时间加上8小时来获取对应的北京时间(即UTC+8)。 由于目前缺乏具体的官方公告链接或者确切日期作为依据,无法直接给出Codeforces M1比赛于UTC+8下的实际发生时段。建议定期访问Codeforces平台查看最新动态更新以及确认最终版程表信息。 ```python from datetime import timedelta, datetime def convert_utc_to_bj(utc_time_str): utc_format = "%Y-%m-%dT%H:%M:%SZ" bj_offset = timedelta(hours=8) try: # 解析UTC时间为datetime对象 utc_datetime = datetime.strptime(utc_time_str, utc_format) # 转换为北京时区时间 beijing_time = utc_datetime + bj_offset return beijing_time.strftime("%Y-%m-%d %H:%M:%S") except ValueError as e: return f"错误:{e}" # 示例输入假设某场Codeforces比赛定于特定UTC时间 example_utc_start = "2024-12-05T17:35:00Z" converted_time = convert_utc_to_bj(example_utc_start) print(f"Codeforces比赛在北京时间下将是:{converted_time}") ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值