Codeforces edu 6. E New Year Tree 图论 线段树

题目

题目链接:http://codeforces.com/contest/620/problem/E

题目来源:Educational Codeforces Round 6

简要题意:给定一棵树,可以给节点子树染色,可以询问一个节点子树有多少不同颜色。

题解

首先颜色可以用二进制来表示, 60 这个范围很明显。

显然直接给你一棵树是无法直接利用线段树来维护的。

实际上只需要对树dfs记录下起始和结束时间以之为基础维护线段树

写了第一个线段树想起这题就来做了,利用区间更新和求和即可

对于要染色节点的区间染色,对询问节点的区间询问

本来是写了build函数的,后来看其他人有没写的,转换成更新来做的,也尝试了下,区别不大。

求多少位有 1 <script type="math/tex" id="MathJax-Element-2">1</script>可以使用g++内置函数__builtin_popcount对于LL,后面加ll即可。

代码

#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <stack>
#include <queue>
#include <string>
#include <vector>
#include <set>
#include <map>

#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define sz(x) ((int)(x).size())
#define fi first
#define se second
using namespace std;
typedef long long LL;
typedef vector<int> VI;
typedef pair<int,int> PII;
LL powmod(LL a,LL b, LL MOD) {LL res=1;a%=MOD;for(;b;b>>=1){if(b&1)res=res*a%MOD;a=a*a%MOD;}return res;}
// head
const int N = 4e5+5;
const int M = N<<1;

// gragh part
struct Edge {
    int to, nxt;
    Edge(int to, int nxt) : to(to), nxt(nxt){}
    Edge() {}
};

int head[N];
Edge e[M];

void addEdge(int from, int to, int cnt) {
    e[cnt] = Edge(to, head[from]);
    head[from] = cnt;
}

void init(int n) {
    for (int i = 1; i <= n; i++) head[i] = -1;
}

PII lr[N];

int time = 0;
void dfs(int x, int fa) {
    lr[x].fi = ++time;
    for (int i = head[x]; ~i; i = e[i].nxt) {
        if (e[i].to == fa) continue;
        dfs(e[i].to, x);
    }
    lr[x].se = time;
}

// data structure
struct SegmentTree {
#define lsonPara lson, L, MID
#define rsonPara rson, MID+1, R
#define MID ((L+R)>>1)
#define lson (rt<<1)
#define rson (rt<<1|1)

    static const int TN = N<<2;

    LL tree[TN];
    LL lazy[TN];

    void pushUp(int rt) {
        tree[rt] = tree[lson] | tree[rson];
    }

    void pushDown(int rt, int L, int R) {
        if (lazy[rt] > 0) {
            tree[lson] = tree[rson] = lazy[rt];
            lazy[lson] = lazy[rson] = lazy[rt];
            lazy[rt] = 0;
        }
    }

    void updateInterval(int rt, int L, int R, int l, int r, LL v) {
        if (r < L || l > R || l > r) return;
        if (L == l && R == r) {
            tree[rt] = lazy[rt] = v;
            return;
        }

        pushDown(rt, L, R);
        updateInterval(lsonPara, l, min(MID, r), v);
        updateInterval(rsonPara, max(l, MID+1), r, v);
        pushUp(rt);
    }

    LL query(int rt, int L, int R, int l, int r) {
        if (r < L || l > R || l > r) return 0;
        if (L == l && R == r) return tree[rt];

        pushDown(rt, L, R);
        return query(lsonPara, l, min(MID, r)) | query(rsonPara, max(l, MID+1), r);
    }
};

SegmentTree st;

int a[N];
int main()
{
    int n, m, u, v, c, q;
    scanf("%d%d", &n, &m);
    for (int i = 1; i <= n; i++) {
        scanf("%d", a+i);
    }

    init(n);
    int cnt = 0;
    for (int i = 1; i < n; i++) {
        scanf("%d%d", &u, &v);
        addEdge(u, v, cnt++);
        addEdge(v, u, cnt++);
    }

    dfs(1, -1);
    for (int i = 1; i <= n; i++) {
        st.updateInterval(1, 1, n, lr[i].fi, lr[i].fi, (1LL<<a[i]));
    }

    for (int i = 0; i < m; i++) {
        scanf("%d%d", &q, &v);
        if (q == 1) {
            scanf("%d", &c);
            st.updateInterval(1, 1, n, lr[v].fi, lr[v].se, (1LL<<c));
        } else {
            printf("%d\n", __builtin_popcountll(st.query(1, 1, n, lr[v].fi, lr[v].se)));
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值