Bzoj2588 Count on a tree

离散化 主席树
每个点记录它到根的路径上的点
查询时u,v,lca,fa[lca]组合起来计算即可

# include <bits/stdc++.h>
# define IL inline
# define RG register
# define Fill(a, b) memset(a, b, sizeof(a))
using namespace std;
typedef long long ll;
const int _(2e5 + 10), __(2e6 + 10);

IL ll Read(){
    RG char c = getchar(); RG ll x = 0, z = 1;
    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 n, m, len, w[_], fst[_], nxt[_], to[_], cnt, size[_], fa[_], son[_], dfn[_], deep[_], id[_], top[_];
int ls[__], rs[__], sz[__], rt[__], num;

IL void Add(RG int u, RG int v){  to[cnt] = v; nxt[cnt] = fst[u]; fst[u] = cnt++;  }

IL void Build(RG int &x, RG int l, RG int r){
    x = ++num; if(l == r) return;
    RG int mid = (l + r) >> 1;
    Build(ls[x], l, mid); Build(rs[x], mid + 1, r);
}

IL void Modify(RG int &x, RG int l, RG int r, RG int val){
    sz[++num] = sz[x]; ls[num] = ls[x]; rs[num] = rs[x];
    sz[x = num]++;
    if(l == r) return;
    RG int mid = (l + r) >> 1;
    if(val <= mid) Modify(ls[x], l, mid, val);
    else Modify(rs[x], mid + 1, r, val);
}

IL int Query(RG int a, RG int b, RG int c, RG int d, RG int l, RG int r, RG int k){
    if(l == r) return l;
    RG int sum = sz[ls[a]] + sz[ls[b]] - sz[ls[c]] - sz[ls[d]], mid = (l + r) >> 1;
    if(sum >= k) return Query(ls[a], ls[b], ls[c], ls[d], l, mid, k);
    else return Query(rs[a], rs[b], rs[c], rs[d], mid + 1, r, k - sum);
}

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;
        deep[to[e]] = deep[u] + 1; fa[to[e]] = u;
        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){
    top[u] = Top; dfn[u] = ++cnt; rt[u] = rt[fa[u]];
    Modify(rt[u], 1, len, id[u]);
    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 GetLCA(RG int u, RG int v){
    while(top[u] != top[v]){
        if(deep[top[u]] < deep[top[v]]) swap(u, v);
        u = fa[top[u]];
    }
    return deep[u] < deep[v] ? u : v;
}

int main(RG int argc, RG char* argv[]){
    n = Read(); m = Read();
    for(RG int i = 1; i <= n; i++) id[i] = w[i] = Read(), fst[i] = -1;
    sort(w + 1, w + n + 1); len = unique(w + 1, w + n + 1) - w - 1;
    for(RG int i = 1; i <= n; i++) id[i] = lower_bound(w + 1, w + len + 1, id[i]) - w;
    for(RG int i = 1, x, y; i < n; i++) x = Read(), y = Read(), Add(x, y), Add(y, x);
    Build(rt[0], 1, len);
    Dfs1(1); cnt = 0; Dfs2(1, 1);
    for(RG int i = 1, ans = 0; i <= m; i++){
        RG int u = Read() ^ ans, v = Read(), k = Read();
        RG int lca = GetLCA(u, v);
        ans = w[Query(rt[u], rt[v], rt[lca], rt[fa[lca]], 1, len, k)];
        printf("%d\n", ans);
    }
    return 0;
}
posted @ 2017-12-23 11:22  Cyhlnj  阅读(108)  评论(0编辑  收藏  举报