zoukankan      html  css  js  c++  java
  • Luogu4175:[CTSC2008]网络管理Network

    题面

    Luogu4175:[CTSC2008]网络管理Network

    Sol

    路径第(k)
    无解直接判断就好了
    然后整体二分,加上树链剖分+树状数组统计

    # include <bits/stdc++.h>
    # define RG register
    # define IL inline
    # define Fill(a, b) memset(a, b, sizeof(a))
    using namespace std;
    typedef long long ll;
    const int _(2e5 + 5);
    
    IL ll Input(){
        RG ll x = 0, z = 1; RG char c = getchar();
        for(; c < '0' || c > '9'; c = getchar()) z = c == '-' ? -1 : 1;
        for(; c >= '0' && c <= '9'; c = getchar()) x = (x << 1) + (x << 3) + (c ^ 48);
        return x * z;
    }
    
    int size[_], fa[_], deep[_], top[_], son[_], dfn[_], Index;
    int fst[_], nxt[_], to[_], cnt, n, c[_];
    
    IL void Modify(RG int x, RG int v){
        for(; x <= n; x += x & -x) c[x] += v;
    }
    
    IL int Query(RG int x){
        RG int ret = 0;
        for(; x; x -= x & -x) ret += c[x];
        return ret;
    }
    
    IL void Add(RG int u, RG int v){
        nxt[cnt] = fst[u]; to[cnt] = v; fst[u] = cnt++;
    }
    
    IL void Dfs1(RG int u){
        size[u] = 1;
        for(RG int e = fst[u]; e != -1; e = nxt[e]){
            if(size[to[e]]) continue;
            fa[to[e]] = u; deep[to[e]] = deep[u] + 1;
            Dfs1(to[e]);
            size[u] += size[to[e]];
            if(size[to[e]] > size[son[u]]) son[u] = to[e];
        }
    }
    
    IL void Dfs2(RG int u, RG int Top){
        dfn[u] = ++Index; top[u] = Top;
        if(son[u]) Dfs2(son[u], Top);
        for(RG int e = fst[u]; e != -1; e = nxt[e])
            if(!dfn[to[e]]) Dfs2(to[e], to[e]);
    }
    
    IL int LCA(RG int u, RG int v){
        while(top[u] ^ top[v]){
            if(deep[top[u]] > deep[top[v]]) swap(u, v);
            v = fa[top[v]];
        }
        return deep[u] > deep[v] ? v : u;
    }
    
    IL int Calc(RG int u, RG int v){
        RG int ret = 0;
        while(top[u] ^ top[v]){
            if(deep[top[u]] > deep[top[v]]) swap(u, v);
            ret += Query(dfn[v]) - Query(dfn[top[v]] - 1);
            v = fa[top[v]];
        }
        if(deep[u] > deep[v]) swap(u, v);
        ret += Query(dfn[v]) - Query(dfn[u] - 1);
        return ret;
    }
    
    int m, tot, ans[_], Q, val[_];
    struct Data{
        int id, x, y, k, op;
    } q[_], q1[_], q2[_];
    
    IL void Solve(RG int l, RG int r, RG int L, RG int R){
        if(L > R) return;
        if(l == r){
            for(RG int i = L; i <= R; ++i)
                if(q[i].id && ans[q[i].id] != -1) ans[q[i].id] = l;
            return;
        }
        RG int mid = (l + r) >> 1, t1 = 0, t2 = 0;
        for(RG int i = L; i <= R; ++i)
            if(q[i].id){
                RG int s = Calc(q[i].x, q[i].y);
                if(s >= q[i].k) q2[++t2] = q[i];
                else q[i].k -= s, q1[++t1] = q[i];
            }
            else{
                if(q[i].y > mid) q2[++t2] = q[i], Modify(dfn[q[i].x], q[i].op);
                else q1[++t1] = q[i];
            }
        for(RG int i = L; i <= R; ++i)
            if(!q[i].id && q[i].y > mid) Modify(dfn[q[i].x], -q[i].op);
        for(RG int i = L, j = 1; j <= t1; ++i, ++j) q[i] = q1[j];
        for(RG int i = L + t1, j = 1; j <= t2; ++i, ++j) q[i] = q2[j];
        Solve(l, mid, L, L + t1 - 1); Solve(mid + 1, r, L + t1, R);
    }
    
    int main(RG int argc, RG char* argv[]){
        tot = n = Input(); m = Input(); RG int mx = 0;
        for(RG int i = 1; i <= n; ++i){
        	q[i].x = i, q[i].y = val[i] = Input(), q[i].op = 1;
            fst[i] = -1; mx = max(mx, val[i]);
        }
        for(RG int i = 1, x, y; i < n; ++i)
            x = Input(), y = Input(), Add(x, y), Add(y, x);
        Dfs1(1); Dfs2(1, 1);
        for(RG int i = 1; i <= m; ++i){
            RG int tk = Input(), tx = Input(), ty = Input();
            if(tk){
                RG int lca = LCA(tx, ty); ++Q;
                RG int len = deep[tx] + deep[ty] - 2 * deep[lca] + 1;
                if(tk > len) ans[Q] = -1;
                q[++tot] = (Data){Q, tx, ty, tk, 0};
            }
            else{
                q[++tot] = (Data){0, tx, val[tx], 0, -1};
                q[++tot] = (Data){0, tx, val[tx] = ty, 0, 1};
                mx = max(mx, ty);
            }
        }
        Solve(0, mx, 1, tot);
        for(RG int i = 1; i <= Q; ++i)
            if(ans[i] == -1) puts("invalid request!");
            else printf("%d
    ", ans[i]);
        return 0;
    }
    
  • 相关阅读:
    postgres 错误duplicate key value violates unique constraint 解决方案
    Golang包管理工具之govendor的使用
    《算法竞赛进阶指南》0x26广搜变形 HDOJ3085 双向BFS
    《算法竞赛进阶指南》0x26广搜变形 POJ3635
    《算法竞赛进阶指南》0x26广搜变形 电路维修 01最短路问题
    《算法竞赛进阶指南》0x25广度优先搜索 推箱子游戏 双重BFS
    《算法竞赛进阶指南》0x25广度优先搜索 多源floodfill
    《算法竞赛进阶指南》0x25广度优先搜索 POJ3322 Bloxorz I
    NETCORE
    VUE- 异步等待方法嵌套
  • 原文地址:https://www.cnblogs.com/cjoieryl/p/8423587.html
Copyright © 2011-2022 走看看