BZOJ 4196 软件包管理器

树链剖分

建树之后,安装软件就是让跟节点到安装的节点路径所有点权+1,卸载软件就是让一个节点和他的子数-1
要求变化数量的话直接求和相减就行啦(绝对值)
注意一点,一开始的lazyatag应该是-1,因为0代表pushdown所有节点应该变成0,1同理。

#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
#define full(a, b) memset(a, b, sizeof a)
using namespace std;
typedef long long ll;
inline int lowbit(int x){ return x & (-x); }
inline int read(){
    int X = 0, w = 0; char ch = 0;
    while(!isdigit(ch)) { w |= ch == '-'; ch = getchar(); }
    while(isdigit(ch)) X = (X << 3) + (X << 1) + (ch ^ 48), ch = getchar();
    return w ? -X : X;
}
inline int gcd(int a, int b){ return a % b ? gcd(b, a % b) : b; }
inline int lcm(int a, int b){ return a / gcd(a, b) * b; }
template<typename T>
inline T max(T x, T y, T z){ return max(max(x, y), z); }
template<typename T>
inline T min(T x, T y, T z){ return min(min(x, y), z); }
template<typename A, typename B, typename C>
inline A fpow(A x, B p, C lyd){
    A ans = 1;
    for(; p; p >>= 1, x = 1LL * x * x % lyd)if(p & 1)ans = 1LL * x * ans % lyd;
    return ans;
}
const int N = 100005;
int n, cnt, dfn, head[N], size[N], depth[N], son[N], p[N], w[N], id[N], top[N];
int tree[N<<2], lazy[N<<2];
struct Edge { int v, next; } edge[N<<2];

void addEdge(int a, int b){
    edge[cnt].v = b, edge[cnt].next = head[a], head[a] = cnt ++;
}

void dfs1(int s, int fa){
    depth[s] = depth[fa] + 1;
    p[s] = fa;
    size[s] = 1;
    int child = -1;
    for(int i = head[s]; i != -1; i = edge[i].next){
        int u = edge[i].v;
        if(u == fa) continue;
        dfs1(u, s);
        size[s] += size[u];
        if(size[u] > child) child = size[u], son[s] = u;
    }
}

void dfs2(int s, int tp){
    id[s] = ++dfn;
    w[id[s]] = 0;
    top[s] = tp;
    if(son[s] != -1) dfs2(son[s], tp);
    for(int i = head[s]; i != -1; i = edge[i].next){
        int u = edge[i].v;
        if(u == p[s] || u == son[s]) continue;
        dfs2(u, u);
    }
}

void push_up(int rt){
    tree[rt] = tree[rt << 1] + tree[rt << 1 | 1];
}

void push_down(int rt, int l, int r){
    if(lazy[rt] != -1){
        int lson = rt << 1, rson = rt << 1 | 1, mid = (l + r) >> 1;
        lazy[lson] = lazy[rson] = lazy[rt];
        tree[lson] = lazy[rt] * (mid - l + 1);
        tree[rson] = lazy[rt] * (r - mid);
        lazy[rt] = -1;
    }
}

void buildTree(int rt, int l, int r){
    if(l == r){
        tree[rt] = w[l];
        return;
    }
    int mid = (l + r) >> 1;
    buildTree(rt << 1, l, mid);
    buildTree(rt << 1 | 1, mid + 1, r);
    push_up(rt);
}

void modify(int rt, int l, int r, int modifyL, int modifyR, int e){
    if(l == modifyL && r == modifyR){
        lazy[rt] = e;
        tree[rt] = (r - l + 1) * e;
        return;
    }
    push_down(rt, l, r);
    int mid = (l + r) >> 1;
    if(modifyL > mid) modify(rt << 1 | 1, mid + 1, r, modifyL, modifyR, e);
    else if(modifyR <= mid) modify(rt << 1, l, mid, modifyL, modifyR, e);
    else{
        modify(rt << 1, l, mid, modifyL, mid, e);
        modify(rt << 1 | 1, mid + 1, r, mid + 1, modifyR, e);
    }
    push_up(rt);
}

int query(int rt, int l, int r, int queryL, int queryR){
    if(l == queryL && r == queryR){
        return tree[rt];
    }
    push_down(rt, l, r);
    int mid = (l + r) >> 1;
    if(queryL > mid) return query(rt << 1 | 1, mid + 1, r, queryL, queryR);
    else if(queryR <= mid) return query(rt << 1, l, mid, queryL, queryR);
    else{
        return query(rt << 1, l, mid, queryL, mid) +
               query(rt << 1 | 1, mid + 1, r, mid + 1, queryR);
    }
}

void treeModify(int x, int y, int e){
    while(top[x] != top[y]){
        if(depth[top[x]] < depth[top[y]]) swap(x, y);
        modify(1, 1, n, id[top[x]], id[x], e);
        x = p[top[x]];
    }
    if(depth[x] > depth[y]) swap(x, y);
    modify(1, 1, n, id[x], id[y], e);
}

void sonModify(int x, int e){
    modify(1, 1, n, id[x], id[x] + size[x] - 1, e);
}

int main(){

    full(head, -1), full(lazy, -1), full(son, -1);
    n = read();
    for(int i = 2; i <= n; i ++){
        int u = read();
        addEdge(u + 1, i), addEdge(i, u + 1);
    }
    dfs1(1, 0), dfs2(1, 1);
    buildTree(1, 1, n);
    int q = read();
    while(q --){
        char opt[20]; scanf("%s", opt);
        int x = read(), a = query(1, 1, n, 1, n);
        x ++;
        if(opt[0] == 'i'){
            treeModify(1, x, 1);
            int b = query(1, 1, n, 1, n);
            printf("%d\n", abs(a - b));
        }
        else if(opt[0] == 'u'){
            sonModify(x, 0);
            int b = query(1, 1, n, 1, n);
            printf("%d\n", abs(a - b));
        }
    }
    return 0;
}
posted @ 2019-04-07 22:06  清楚少女ひなこ  阅读(206)  评论(0编辑  收藏  举报