zoukankan      html  css  js  c++  java
  • 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
    ", abs(a - b));
            }
            else if(opt[0] == 'u'){
                sonModify(x, 0);
                int b = query(1, 1, n, 1, n);
                printf("%d
    ", abs(a - b));
            }
        }
        return 0;
    }
    
  • 相关阅读:
    笔记:Oracle SQL 高级编程 第2章 SQL 执行
    python 中的 filter, lambda, map, reduce 内置函数
    笔记:Oracle SQL 高级编程 第1章 SQL 核心
    java大文件读写操作,java nio 之MappedByteBuffer,高效文件/内存映射
    使用JDBC进行批处理
    程序员面试、算法研究、编程艺术、红黑树、数据挖掘5大系列集锦
    教你如何迅速秒杀掉:99%的海量数据处理面试题
    十道海量数据处理面试题与十个方法大总结
    《Java 7 并发编程指南》学习概要 (7) 定制并发类
    HashMap多线程并发问题分析
  • 原文地址:https://www.cnblogs.com/onionQAQ/p/10667493.html
Copyright © 2011-2022 走看看