Count on a tree
SPOJ - COT
You are given a tree with N nodes. The tree nodes are numbered from 1 to N. Each node has an integer weight.
We will ask you to perform the following operation:
- u v k : ask for the kth minimum weight on the path from node u to node v
Input
In the first line there are two integers N and M. (N, M <= 100000)
In the second line there are N integers. The ith integer denotes the weight of the ith node.
In the next N-1 lines, each line contains two integers u v, which describes an edge (u, v).
In the next M lines, each line contains three integers u v k, which means an operation asking for the kth minimum weight on the path from node u to node v.
Output
For each operation, print its result.
Example
Input: 8 5 105 2 9 3 8 5 7 7 1 2 1 3 1 4 3 5 3 6 3 7 4 8 2 5 1 2 5 2 2 5 3 2 5 4 7 8 2
Output: 2 8 9 105 7
Source: SPOJ - COT
[kuangbin]主席树]
My Solution: 题意:给出一个树和树上每个点的权值,给出m个询问(u,v,k),询问在树上从点u到点v所构成的路径上权值第k小的点的权值。 树上主席树+LCA+任意路径问题 主席树维护的一个前缀和,而前缀和不一定要出现在一个线性表上(或者说树型可以线性话),即对于每个从根到点u的路径是一个线性序列,可以把这个序列建成主席树。
利用这个前缀和,我们可以解决一些树上任意路径的问题,比如在线询问[u,v]点对的距离——dist[u]+dist[v]-2*dist[LCA(u,v)]。
类似的,我们可以利用主席树来解决树上任意路径的问题。 DFS遍历整棵树,然后在每个节点上建立一棵线段树,某一棵线段树的“前一版本”是位于该节点父亲节点fa的线段树。那么对于询问[a,b],答案就是root[u]+root[v]-root[LCA(u,v)]-root[fa[LCA(u,v)]]上的第k小。 时间复杂度 O(nlogn)
空间复杂度 O(nlogn)
#include <iostream> #include <cstdio> #include <vector> #include <algorithm> using namespace std; typedef long long LL; const int MAXN = 1e5 + 8; vector<int> sons[MAXN]; //LCA int father[MAXN][22], depth[MAXN]; //!LCA 倍增算法 可以动态加减叶子节点, 因为预处理的时候每个叶子是独立的 inline void prepare(int n){ int i, j; for(j = 1; j <= 20; j++){ for(i = 1; i <= n; i++){ father[i][j] = father[father[i][j-1]][j-1]; } } } inline int LCA(int u, int v) { if (depth[u] < depth[v]) swap(u, v); int dc = depth[u] - depth[v]; int i; for(i = 0; i <= 20; i++){ if((1<<i) & dc) u = father[u][i]; } if(u == v) return u; for(i = 20; i >= 0; i--){ if (father[u][i] != father[v][i]){ u = father[u][i]; v = father[v][i]; } } u = father[u][0]; return u; } //Chairman_Tree int tot, size; int a[MAXN], b[MAXN], root[20*MAXN], ls[20*MAXN], rs[20*MAXN], sum[20*MAXN]; inline void _build(int &o, int l, int r){ o = ++ tot; sum[o] = 0; if(l == r) return; int mid = (l + r) >> 1; _build(ls[o], l, mid); _build(rs[o], mid + 1, r); } inline void update(int &o, int l, int r, int last, int p){ o = ++ tot; ls[o] = ls[last]; rs[o] = rs[last]; sum[o] = sum[last] + 1; if(l == r) return; int mid = (l + r) >> 1; if(p <= mid) update(ls[o], l, mid, ls[last], p); else update(rs[o], mid + 1, r, rs[last], p); } inline int _query(int u, int v, int lca, int flca, int l, int r, int k){ if(l == r) return l; int mid = (l + r) >> 1; int cnt = sum[ls[u]] + sum[ls[v]] - sum[ls[lca]] - sum[ls[flca]]; if(k <= cnt) return _query(ls[u], ls[v], ls[lca], ls[flca], l, mid, k); else return _query(rs[u], rs[v], rs[lca], rs[flca], mid + 1, r, k - cnt); } // inline void dfs(int u, int fa){// int sz = sons[u].size(), i, v; //cout << u <<" "<< a[u] << endl; update(root[u], 1, size, root[fa], a[u]); for(i = 0; i < sz; i++){ v = sons[u][i]; if(v == fa) continue; father[v][0] = u; depth[v] = depth[u] + 1; dfs(v, u); } } int main() { #ifdef LOCAL freopen("c.txt", "r", stdin); //freopen("c.out", "w", stdout); int T = 1; while(T--){ #endif // LOCAL ios::sync_with_stdio(false); cin.tie(0); int n, m, i, u, v, x, ind, lca; cin >> n >> m; for(i = 1; i <= n; i++){ cin >> a[i]; b[i] = a[i]; } for(i = 1; i < n; i++){ cin >> u >> v; sons[u].push_back(v); sons[v].push_back(u); } sort(b + 1, b + n + 1); size = unique(b + 1, b + n + 1) - (b + 1); for(i = 1; i <= n; i++){ //cout << a[i] << " "; a[i] = lower_bound(b + 1, b + size + 1, a[i]) - b; //It is based on 1~n.. //cout << a[i] << "\n"; } tot = 0; _build(root[0], 1, size); dfs(1, 0); prepare(n); while(m--){ cin >> u >> v >> x; lca = LCA(u, v); //cout << u << " " << v << " " << lca << endl; ind = _query(root[u], root[v], root[lca], root[father[lca][0]], 1, size, x); cout << b[ind] << "\n"; } #ifdef LOCAL cout << endl; } #endif // LOCAL return 0; }
------from ProLights
Thank you!