• Luogu 4216 [SCOI2015]情报传递


    BZOJ 4448。

    写起来很愉悦的题。

    按照时间可持久化线段树,修改就在原来的地方加$1$即可,查询就直接询问$root_1 - root_{now - c - 1}$中相应的个数。

    主席树维护树链剖分即可。

    时间复杂度$O(nlog^2n)$。

    Code:

    #include <cstdio>
    #include <cstring>
    using namespace std;
    
    const int N = 2e5 + 5;
    
    int n, rt, qn, tot = 0, head[N];
    int dfsc = 0, id[N], siz[N], fa[N], top[N], son[N], dep[N];
    
    struct Edge {
        int to, nxt;
    } e[N << 1];
    
    inline void add(int from, int to) {
        e[++tot].to = to;
        e[tot].nxt = head[from];
        head[from] = tot;
    }
    
    inline void read(int &X) {
        X = 0; char ch = 0; int op = 1;
        for(; ch > '9' || ch < '0'; ch = getchar())
            if(ch == '-') op = -1;
        for(; ch >= '0' && ch <= '9'; ch = getchar())
            X = (X << 3) + (X << 1) + ch - 48;
        X *= op;
    }
    
    inline void swap(int &x, int &y) {
        int t = x; x = y; y = t;
    }
     
    void dfs1(int x, int fat, int depth) {
        fa[x] = fat, dep[x] = depth, siz[x] = 1;
        int maxson = -1;
        for(int i = head[x]; i; i = e[i].nxt) {
            int y = e[i].to;
            if(y == fat) continue;
            dfs1(y, x, depth + 1);
    
            siz[x] += siz[y];
            if(siz[y] > maxson) {
                son[x] = y;
                maxson = siz[y];
            }
        }
    }
    
    void dfs2(int x, int topf) {
        top[x] = topf, id[x] = ++dfsc;
        if(!son[x]) return;
        dfs2(son[x], topf);
        for(int i = head[x]; i; i = e[i].nxt) {
            int y = e[i].to;
            if(y == fa[x] || y == son[x]) continue;
            dfs2(y, y);
        }
    }
    
    namespace PSegT {
        struct Node {
            int lc, rc, sum;
        } s[N * 40];
    
        int root[N], nodeCnt = 0;
    
        #define lc(p) s[p].lc
        #define rc(p) s[p].rc
        #define mid ((l + r) >> 1)
        #define sum(p) s[p].sum
    
        void ins(int &p, int l, int r, int x, int pre) {
            s[p = ++nodeCnt] = s[pre];
            ++sum(p);
            if(l == r) return;
    
            if(x <= mid) ins(lc(p), l, mid, x, lc(pre));
            else ins(rc(p), mid + 1, r, x, rc(pre));
        }
    
        int query(int r1, int r2, int l, int r, int x, int y) {
            if(x <= l && y >= r) return sum(r2) - sum(r1);
    
            int res = 0;
            if(x <= mid) res += query(lc(r1), lc(r2), l, mid, x, y);
            if(y > mid) res += query(rc(r1), rc(r2), mid + 1, r, x, y);
    
            return res;
        }
    
    } using namespace PSegT;
    
    inline void solve(int r) {
        int x, y, c, res = 0;
        read(x), read(y), read(c);
        r -= c + 1;
        int px = x, py = y;
    
        for(; top[x] != top[y]; ) {
            if(dep[top[x]] < dep[top[y]]) swap(x, y);
            if(r > 0) res += query(root[0], root[r], 1, n, id[top[x]], id[x]);
            x = fa[top[x]];
        }
    
        if(dep[x] > dep[y]) swap(x, y);
        if(r > 0) res += query(root[0], root[r], 1, n, id[x], id[y]);
    
        printf("%d %d
    ", dep[px] + dep[py] - 2 * dep[x] + 1, res);
    }
    
    int main() {
        read(n);
        for(int fat, i = 1; i <= n; i++) {
            read(fat);
            if(!fat) rt = i;
            else add(i, fat), add(fat, i);
        }
        dfs1(rt, 0, 1), dfs2(rt, rt);
    
        read(qn);
        for(int op, i = 1; i <= qn; i++) {
            root[i] = root[i - 1];
            read(op);
            if(op == 2) {
                int x; read(x);
                ins(root[i], 1, n, id[x], root[i]);
            } else solve(i);
        }
        
        return 0;
    }
    View Code
  • 相关阅读:
    Andrew Ng机器学习week4(Neural Networks: Representation)编程习题
    linux系统下crontab 配置启动定时任务
    在python中配置tornado服务
    在python中Flask配置服务
    ltp的使用
    查看文件内容- 删除某个运行程序的所有进程-nohup后台执行程序
    机器学习和深度学习资料整理
    配置linux服务器和pycharm的连接
    计算 24 点是一种扑克牌益智游戏,随机抽出 4 张扑克牌,通过加 (+) ,减 (-) ,乘 ( * ), 除 (/) 四种运算法则计算得到整数 24 ,本问题中,扑克牌通过如下字符或者字符串表示,其中,小写 joker 表示小王,大写 JOKER 表示大王:
    机器人的运动范围 剑指offer66题
  • 原文地址:https://www.cnblogs.com/CzxingcHen/p/9812558.html
Copyright © 2020-2023  润新知