SPOJ
来源:互联网 发布:java关键字do的作用 编辑:程序博客网 时间:2024/06/05 19:41
Count on a tree
SPOJ - COTYou 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 5105 2 9 3 8 5 7 71 21 31 43 53 63 74 82 5 12 5 22 5 32 5 47 8 2
Output:2891057
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];//LCAint 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_Treeint 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!
- SPOJ
- SPOJ
- SPOJ
- SPOJ
- SPOJ
- SPOJ
- SPOJ
- SPOJ
- SPOJ
- SPOJ
- SPOJ
- SPOJ
- SPOJ
- SPOJ
- SPOJ
- SPOJ
- SPOJ
- SPOJ
- python scikit-learn机器学习相关库
- 想提高用户访问的响应速度和成功率还不赶快学习CDN
- windows 下重置 mysql 的 root 密码
- 一款手机制作软件类工具的简单分析
- socket简单java模拟
- SPOJ
- [Android开发] Android Studio问题以及解决记录
- pandas方法积累
- 【SSH】安装JBoss Tools及eclipse从数据库逆向生成hibernate实体类和映射
- 【1105】字符串大小比较
- 史上最简单的 SpringCloud 教程 | 终章
- 配置一台装tomcat的linux服务器(阿里云为例)
- 人工智能资料下载地址分享
- TNS-01189: The listener could not authenticate the user