zoukankan      html  css  js  c++  java
  • Luogu3242:[HNOI2015]接水果

    题面

    Luogu3242

    Sol

    考虑每个盘子怎样才能接到一个水果
    分两种情况:
    盘子的(x, y)在一条链上,那么水果的两点就要在这条链之外
    不在的话,水果的两点就分别在盘子的两点的子树中
    记录下每个点的(dfs)序,和这棵子树结束的(dfs)
    发现上述讨论就是相当于求水果这个点((x, y))包含它的二维矩阵(盘子)有多少个
    扫描线+树链剖分+树状数组+整体二分
    矩阵的样子看代码

    # 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 _(1e5 + 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, ed[_];
    int fst[_], nxt[_], to[_], cnt, n;
    
    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]);
        ed[u] = Index;
    }
    
    IL int LCA(RG int u, RG int v, RG int &qs){
        while(top[u] ^ top[v]){
            if(deep[top[u]] > deep[top[v]]) swap(u, v);
            qs = top[v]; v = fa[top[v]];
        }
        if(deep[u] > deep[v]) swap(u, v);
        if(u != v) qs = son[u];
        return u;
    }
    
    int c[_];
    
    IL void Modify(RG int x, RG int d){
        for(; x <= n; x += x & -x) c[x] += d;
    }
    
    IL int Query(RG int x){
        RG int ret = 0;
        for(; x; x -= x & -x) ret += c[x];
        return ret;
    }
    
    int m, ans[_], Q, tot;
    struct Matrix{
        int x1, x2, y1, y2, k;
        IL bool operator <(RG Matrix B) const{
            return k < B.k;
        }
    } p[_];
    struct Option{
        int x, l, r, d;
        IL bool operator <(RG Option B) const{
            return x < B.x;
        }
    } opt[_];
    struct Data{
        int id, x, y, k;
        IL bool operator <(RG Data B) const{
            return x < B.x;
        }
    } 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) ans[q[i].id] = p[l].k;
            return;
        }
        RG int mid = (l + r) >> 1, t0 = 0, t1 = 0, t2 = 0;
        for(RG int i = l; i <= mid; ++i){
            opt[++t0] = (Option){p[i].x1, p[i].y1, p[i].y2, 1};
            opt[++t0] = (Option){p[i].x2 + 1, p[i].y1, p[i].y2, -1};
        }
        sort(opt + 1, opt + t0 + 1); RG int tt = 1;
        for(RG int i = L; i <= R; ++i){
            for(; tt <= t0 && opt[tt].x <= q[i].x; ++tt)
                Modify(opt[tt].l, opt[tt].d), Modify(opt[tt].r + 1, -opt[tt].d);
            RG int s = Query(q[i].y);
            if(q[i].k <= s) q1[++t1] = q[i];
            else q[i].k -= s, q2[++t2] = q[i];
        }
        for(; tt <= t0; ++tt) Modify(opt[tt].l, opt[tt].d), Modify(opt[tt].r + 1, -opt[tt].d);
        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[]){
        n = Input(); m = Input(); Q = Input(); Fill(fst, -1);
        for(RG int i = 1; i < n; ++i){
        	RG int u = Input(), v = Input();
        	Add(u, v); Add(v, u);
        }
        Dfs1(1); Dfs2(1, 1);
        for(RG int i = 1; i <= m; ++i){
            RG int x = Input(), y = Input(), k = Input();
            if(dfn[x] > dfn[y]) swap(x, y);
            RG int qs, lca = LCA(x, y, qs);
            if(x == lca){
                if(ed[qs] < n) p[++tot] = (Matrix){dfn[y], ed[y], ed[qs] + 1, n, k};
                if(dfn[qs] > 1) p[++tot] = (Matrix){1, dfn[qs] - 1, dfn[y], ed[y], k};
            }
            else p[++tot] = (Matrix){dfn[x], ed[x], dfn[y], ed[y], k};
        }
        sort(p + 1, p + tot + 1);
        for(RG int i = 1; i <= Q; ++i){
            RG int x = Input(), y = Input(), k = Input();
            if(dfn[x] > dfn[y]) swap(x, y);
            q[i] = (Data){i, dfn[x], dfn[y], k};
        }
        sort(q + 1, q + Q + 1);
        Solve(1, tot, 1, Q);
        for(RG int i = 1; i <= Q; ++i) printf("%d
    ", ans[i]);
        return 0;
    }
    
  • 相关阅读:
    HDU 1009 FatMouse' Trade
    HDU 2602 (简单的01背包) Bone Collector
    LA 3902 Network
    HDU 4513 吉哥系列故事——完美队形II
    LA 4794 Sharing Chocolate
    POJ (Manacher) Palindrome
    HDU 3294 (Manacher) Girls' research
    HDU 3068 (Manacher) 最长回文
    Tyvj 1085 派对
    Tyvj 1030 乳草的入侵
  • 原文地址:https://www.cnblogs.com/cjoieryl/p/8423714.html
Copyright © 2011-2022 走看看