Danil and a Part-time Job (dfs序 + 线段树)

传送门

Description
给你一棵树, 每个节点都是一盏灯,有开和关的状态,有两种操作:
1、查询以u为根节点的子树,有多少个节点(包括根节点)亮着。
2、更改以u为根节点的子树,亮->暗,暗->亮(包括根节点)亮着。
Solution
先用dfs序将树处理成线段树可以维护的状态, 然后线段树维护即可。

push_up里面没有写push_down,调了半天。。。

时间复杂度:求dfs序O(n),维护线段树O(logn)
Code

#include <bits/stdc++.h>

using namespace std;
const int N = 2e5 + 10;

int n, m;
int idx, in[N], out[N];
vector<int> g[N];

void dfs(int u, int fa) {
    in[u] = ++idx;
    for(int i = 0; i < g[u].size(); ++i) {
        int v = g[u][i];
        if(v == fa) continue;
        dfs(v, u);
    }
    //cout << "debug\n";
    out[u] = idx;
}

struct node {
    int l, r, lazy, sum;
};

struct segment_tree {
    node t[N << 2];

    void push_up(int root) {
        int ch = root << 1;
        push_down(ch);
        push_down(ch + 1);
        t[root].sum = t[ch].sum + t[ch + 1].sum;
    }

    void push_down(int root) {
        if(t[root].lazy != 0) {
            // cal_lazy
            t[root].sum = (t[root].r - t[root].l + 1) - t[root].sum;
            // union_lazy
            if(t[root].l != t[root].r) {
                int ch = root << 1;
                t[ch].lazy ^= t[root].lazy;
                t[ch + 1].lazy ^= t[root].lazy;
            }
            // init_lazy
            t[root].lazy = 0;
        }
    }

    void build(int root, int l, int r) {
        t[root].l = l;
        t[root].r = r;
        if(l != r) {
            int ch = root << 1;
            int mid = l + r >> 1;
            build(ch, l, mid);
            build(ch + 1, mid + 1, r);
        } else {
            t[root].lazy = t[root].sum = 0;
        }
    }

    void update(int root, int l, int r) {
        push_down(root);
        if(t[root].l == l && t[root].r == r) {
            //cout << "root" << root << "\n";
            t[root].lazy ^= 1;
            return;
        }
        int ch = root << 1;
        int mid = t[root].l + t[root].r >> 1;
        if(r <= mid) update(ch, l,  r);
        else if(l > mid) update(ch + 1, l, r);
        else update(ch, l, mid), update(ch + 1, mid + 1, r);
        push_up(root);
    }

    int query(int root, int l, int r) {
        push_down(root);
        if(t[root].l >= l && t[root].r <= r) {
            return t[root].sum;
        }
        int ch = root << 1;
        int mid = t[root].l + t[root].r >> 1;
        if(r <= mid) return query(ch, l, r);
        else if(l > mid) return query(ch + 1, l, r);
        else return query(ch, l, mid) + query(ch + 1, mid + 1, r);
    }

} st;

signed main() {
    ios::sync_with_stdio(false), cin.tie(0);
    int x;
    cin >> n;
    for(int i = 2; i <= n; ++i) {
        cin >> x;
        g[i].push_back(x), g[x].push_back(i);
    }

    dfs(1, -1);
    st.build(1, 1, n);
    //cout << "finsh\n";
    for(int i = 1; i <= n; ++i) {
        cin >> x;

        if(x) st.update(1, in[i], in[i]);
        //cout << "finshdd\n";
    }


    cin >> m;
    string op;
    while(m--) {
        cin >> op >> x;
        if(op == "get") {
            cout << st.query(1, in[x], out[x]) << "\n";
        } else {
            st.update(1, in[x], out[x]);
        }
    }

    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值