zoukankan      html  css  js  c++  java
  • [SPOJ 10628]Count on a tree

    Description

    题库链接

    求不带修改的树上路径第 (K) 小。 (N) 个节点 (M) 组询问。

    (1leq N,Mleq 100000)

    Solution

    主席树维护树上前缀和。分离一段路径的技巧: (val_u+val_v-val_{lca_{u,v}}-val_{fa_{lca_{u,v}}}) 。其余的就是 (K-th~number) 了。

    Code

    //It is made by Awson on 2018.2.28
    #include <bits/stdc++.h>
    #define LL long long
    #define dob complex<double>
    #define Abs(a) ((a) < 0 ? (-(a)) : (a))
    #define Max(a, b) ((a) > (b) ? (a) : (b))
    #define Min(a, b) ((a) < (b) ? (a) : (b))
    #define Swap(a, b) ((a) ^= (b), (b) ^= (a), (a) ^= (b))
    #define writeln(x) (write(x), putchar('
    '))
    #define lowbit(x) ((x)&(-(x)))
    using namespace std;
    const int N = 100000;
    void read(int &x) {
        char ch; bool flag = 0;
        for (ch = getchar(); !isdigit(ch) && ((flag |= (ch == '-')) || 1); ch = getchar());
        for (x = 0; isdigit(ch); x = (x<<1)+(x<<3)+ch-48, ch = getchar());
        x *= 1-2*flag;
    }
    void print(int x) {if (x > 9) print(x/10); putchar(x%10+48); }
    void write(int x) {if (x < 0) putchar('-'); print(Abs(x)); }
    
    int a[N+5], n, m, b[N+5], u, v, last, tot, k;
    struct tt {int to, next; }edge[(N<<1)+5];
    int path[N+5], Top, dep[N+5], fa[N+5], top[N+5], size[N+5], son[N+5];
    struct Segment_tree {
        int root[N+5], key[N*40+5], ch[N*40+5][2], pos;
        int cpynode(int o) {++pos, ch[pos][0] = ch[o][0], ch[pos][1] = ch[o][1], key[pos] = key[o]; return pos; }
        void insert(int &o, int l, int r, int loc) {
        o = cpynode(o); ++key[o];
        if (l == r) return; int mid = (l+r)>>1;
        if (loc <= mid) insert(ch[o][0], l, mid, loc); else insert(ch[o][1], mid+1, r, loc);
        }
        int query(int a, int b, int c, int d, int l, int r, int k) {
        if (l == r) return l; int mid = (l+r)>>1;
        int tmp = key[ch[a][0]]+key[ch[b][0]]-key[ch[c][0]]-key[ch[d][0]];
        if (tmp >= k) return query(ch[a][0], ch[b][0], ch[c][0], ch[d][0], l, mid, k);
        else return query(ch[a][1], ch[b][1], ch[c][1], ch[d][1], mid+1, r, k-tmp);
        }
    }T;
    
    void dfs1(int o, int father, int depth) {
        T.root[o] = T.root[father]; T.insert(T.root[o], 1, tot, lower_bound(b+1, b+tot+1, a[o])-b);
        size[o] = 1, dep[o] = depth, fa[o] = father;
        for (int i = path[o]; i; i = edge[i].next)
        if (edge[i].to != father) {
            dfs1(edge[i].to, o, depth+1);
            size[o] += size[edge[i].to];
            if (size[edge[i].to] > size[son[o]]) son[o] = edge[i].to;
        }
    }
    void dfs2(int o, int tp) {
        top[o] = tp;
        if (son[o]) dfs2(son[o], tp);
        for (int i = path[o]; i; i = edge[i].next)
        if (edge[i].to != fa[o] && edge[i].to != son[o]) dfs2(edge[i].to, edge[i].to);
    }
    int get_lca(int x, int y) {
        while (top[x] != top[y]) {
        if (dep[top[x]] < dep[top[y]]) Swap(x, y);
        x = fa[top[x]];
        }
        return dep[x] > dep[y] ? y : x;
    }
    void add(int u, int v) {edge[++Top].to = v, edge[Top].next = path[u], path[u] = Top; }
    void work() {
        read(n), read(m); for (int i = 1; i <= n; i++) read(a[i]), b[i] = a[i];
        for (int i = 1; i < n; i++) read(u), read(v), add(u, v), add(v, u);
        sort(b+1, b+n+1); tot = unique(b+1, b+n+1)-b-1;
        dfs1(1, 0, 1); dfs2(1, 1);
        for (int i = 1; i <= m; i++) {
        read(u), read(v); u ^= last; read(k); int lca = get_lca(u, v);
        write(last = b[T.query(T.root[u], T.root[v], T.root[lca], T.root[fa[lca]], 1, tot, k)]);
        if (i != m) putchar('
    ');
        }
    }
    int main() {
        work(); return 0;
    }
  • 相关阅读:
    ELK 一些截图
    AD域
    NPOI
    搭建harbor
    【【【【日常问题记录】】】】
    golang yaml配置文件解析
    golang操作mysql使用总结
    【转】mysql优化步骤
    【转】Mysql事务,并发问题,锁机制
    golang curl
  • 原文地址:https://www.cnblogs.com/NaVi-Awson/p/8483936.html
Copyright © 2011-2022 走看看