题目链接:https://www.spoj.com/problems/COT/en/

题目:

题意:

  给你一棵有n个节点的树,求节点u到节点v这条链上的第k大。

思路:

  我们首先用dfs进行建题目给的树,然后在dfs时进行主席树的update操作。众所周知,主席树采用的是前缀和思想,区间第k大是与前一个树添加新的线段树,而树上第k大则是与父亲节点添加新的线段树,因而在此思想上此题的答案为sum[u] + sum[v] - sum[lca(u,v)] - sum[fa[lca(u,v)]。求第k大操作和区间第k大一样,就不描述了~

代码实现如下:

 #include <set>
#include <map>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <bitset>
#include <cstdio>
#include <string>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std; typedef long long LL;
typedef pair<LL, LL> pLL;
typedef pair<LL, int> pli;
typedef pair<int, LL> pil;;
typedef pair<int, int> pii;
typedef unsigned long long uLL; #define lson rt<<1
#define rson rt<<1|1
#define lowbit(x) x&(-x)
#define name2str(name) (#name)
#define bug printf("*********\n");
#define debug(x) cout<<#x"=["<<x<<"]" <<endl;
#define FIN freopen("/home/dillonh/CLionProjects//in.txt", "r", stdin);
#define FOUT freopen("D://code//out.txt", "w", stdout);
#define IO ios::sync_with_stdio(false),cin.tie(0); const double eps = 1e-;
const int mod = ;
const int maxn = + ;
const double pi = acos(-);
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3fLL; int n, q, tot, cnt, x, y, k, len;
int head[maxn], root[maxn];
int a[maxn], deep[maxn], fa[maxn][];
vector<int> v; struct edge {
int v, next;
}ed[maxn<<]; void addedge(int u, int v) {
ed[tot].v = v;
ed[tot].next = head[u];
head[u] = tot++;
ed[tot].v = u;
ed[tot].next = head[v];
head[v] = tot++;
} struct node {
int l, r, sum;
}tree[maxn*]; int getid(int x) {
return lower_bound(v.begin(), v.end(), x) - v.begin() + ;
} void update(int l, int r, int& x, int y, int pos) {
tree[++cnt] = tree[y], tree[cnt].sum++, x = cnt;
if(l == r) return;
int mid = (l + r) >> ;
if(mid >= pos) update(l, mid, tree[x].l, tree[y].l, pos);
else update(mid + , r, tree[x].r, tree[y].r, pos);
} int query(int l, int r, int x, int y, int p, int pp, int k) {
if(l == r) return l;
int mid = (l + r) >> ;
int sum = tree[tree[x].l].sum + tree[tree[y].l].sum - tree[tree[p].l].sum - tree[tree[pp].l].sum;
if(sum >= k) return query(l, mid, tree[x].l, tree[y].l, tree[p].l, tree[pp].l, k);
else return query(mid + , r, tree[x].r, tree[y].r, tree[p].r, tree[pp].r, k - sum);
} void dfs(int u, int d, int p) {
deep[u] = d;
fa[u][] = p;
update(, len, root[u], root[p], getid(a[u]));
for(int i = head[u]; ~i; i = ed[i].next) {
int v = ed[i].v;
if(v != p) {
dfs(v, d + , u);
}
}
} void lca() {
for(int i = ; i <= n; i++) {
for(int j = ; ( << j) <= n; j++) {
fa[i][j] = -;
}
}
for(int j = ; ( << j) <= n; j++) {
for(int i = ; i <= n; i++) {
if(fa[i][j-] != -) {
fa[i][j] = fa[fa[i][j-]][j-];
}
}
}
} int cal(int u, int v) {
if(deep[u] < deep[v]) swap(u, v);
int k;
for(k = ; ( << ( + k)) <= deep[u]; k++);
for(int i = k; i >= ; i--) {
if(deep[u] - ( << i) >= deep[v]) {
u = fa[u][i];
}
}
if(u == v) return u;
for(int i = k; i >= ; i--) {
if(fa[u][i] != - && fa[u][i] != fa[v][i]) {
u = fa[u][i];
v = fa[v][i];
}
}
return fa[u][];
} int main() {
#ifndef ONLINE_JUDGE
FIN;
#endif
scanf("%d%d", &n, &q);
memset(head, -, sizeof(head));
for(int i = ; i <= n; i++) scanf("%d", &a[i]), v.push_back(a[i]);
sort(v.begin(), v.end());
v.erase(unique(v.begin(), v.end()), v.end());
len = v.size();
for(int i = ; i < n; i++) scanf("%d%d", &x, &y), addedge(x, y);
dfs(, , );
lca();
while(q--) {
scanf("%d%d%d", &x, &y, &k);
int p = cal(x, y);
printf("%d\n", v[query(, len, root[x], root[y], root[p], root[fa[p][]], k)-]);
}
return ;
}

Count on a tree(SPOJ COT + 树上第k大 + 主席树 + LCA)的更多相关文章

  1. 计蒜客 38229.Distance on the tree-1.树链剖分(边权)+可持久化线段树(区间小于等于k的数的个数)+离散化+离线处理 or 2.树上第k大(主席树)+二分+离散化+在线查询 (The Preliminary Contest for ICPC China Nanchang National Invitational 南昌邀请赛网络赛)

    Distance on the tree DSM(Data Structure Master) once learned about tree when he was preparing for NO ...

  2. 🔺Count on a tree SPOJ - COT (无能为力。。。)

    https://cn.vjudge.net/problem/SPOJ-COT 插上 大佬的代码 和 我的...以后再看吧... Count on a tree 大佬:http://www.cnblog ...

  3. Count on a tree SPOJ - COT (主席树,LCA)

    You are given a tree with N nodes. The tree nodes are numbered from 1 to N. Each node has an integer ...

  4. 【SPOJ】Count On A Tree II(树上莫队)

    [SPOJ]Count On A Tree II(树上莫队) 题面 洛谷 Vjudge 洛谷上有翻译啦 题解 如果不在树上就是一个很裸很裸的莫队 现在在树上,就是一个很裸很裸的树上莫队啦. #incl ...

  5. Count on a tree SPOJ 10628 主席树+LCA(树链剖分实现)(两种存图方式)

    Count on a tree SPOJ 10628 主席树+LCA(树链剖分实现)(两种存图方式) 题外话,这是我第40篇随笔,纪念一下.<( ̄︶ ̄)↗[GO!] 题意 是说有棵树,每个节点上 ...

  6. HDU 4729 An Easy Problem for Elfness (主席树,树上第K大)

    转载请注明出处,谢谢http://blog.csdn.net/ACM_cxlove?viewmode=contents    by---cxlove 题意:给出一个带边权的图.对于每一个询问(S , ...

  7. 树上第k大联通块

    题意:求树上第k大联通块 n,k<=1e5 考虑转化为k短路的形式. 也就是要建出一张图是的这条图上每一条S到T的路径都能代表一个联通块. 点分治建图 递归下去,假定每个子树的所有联通块中都可以 ...

  8. SPOJ 10628 COT - Count on a tree(在树上建立主席树)(LCA)

    COT - Count on a tree #tree You are given a tree with N nodes.The tree nodes are numbered from 1 to ...

  9. SPOJ COT Count on a tree(树上主席树 + LCA 求点第k小)题解

    题意:n个点的树,每个点有权值,问你u~v路径第k小的点的权值是? 思路: 树上主席树就是每个点建一棵权值线段树,具体看JQ博客,LCA用倍增logn求出,具体原理看这里 树上主席树我每个点的存的是点 ...

随机推荐

  1. 软工实践团队展示——WorldElite

    软工实践团队展示--WorldElite 本次作业链接 团队成员 031602636许舒玲(组长) 031602237吴杰婷 031602634吴志鸿 081600107傅滨 031602220雷博浩 ...

  2. PAT 甲级 1005 Spell It Right

    https://pintia.cn/problem-sets/994805342720868352/problems/994805519074574336 Given a non-negative i ...

  3. IIS部署时failed to execute url 解决方法

    web.config中增加如下节点: <system.webServer>  <validation validateIntegratedModeConfiguration=&quo ...

  4. MVC、MVP、MVVM 模式

    一.前言 做客户端开发.前端开发对MVC.MVP.MVVM这些名词不了解也应该大致听过,都是为了解决图形界面应用程序复杂性管理问题而产生的应用架构模式.网上很多文章关于这方面的讨论比较杂乱,各种MV* ...

  5. 【设计模式】C++中的单例模式

    讲解来自:http://blog.chinaunix.net/xmlrpc.php?r=blog/article&id=4281275&uid=26611383 由于使用了POSIX函 ...

  6. lxs1314 is not in the sudoers file. This incident will be reported.

    虚拟机下面  普通用户用sudo执行命令时报"xxx is not in the sudoers file.This incident will be reported"错误,解决 ...

  7. Vue 定时执行函数

    var app = new Vue({ el: '#app', data: { count: , }, filters: { }, mounted: function () { this.$nextT ...

  8. CSUOJ 1141——第四届河南省程序设计大赛

    题目的意思是给你一个机器人,初始的时候在某一个给定的路灯位置,机器人要把路边所有的路灯关掉,每个路灯都有一个距离和一个功率,求要把所有的路灯关掉最小的最终能耗是多少? 题目是一个很明显的区间DP.可以 ...

  9. BZOJ 1190 梦幻岛宝珠(分组01背包)

    跑了7000ms... 这是个体积和价值都超大的背包.但是体积保证为a*2^b的(a<=10,b<=30)形式.且n<=100. 于是可以想到按b来分组.这样的话每组最多为a*n*2 ...

  10. bzoj2969 矩形粉刷 概率期望

    此题在bzoj是权限题,,,所以放另一个oj的链接 题解: 因为期望线性可加,所以可以对每个方格单独考虑贡献.每个方格的贡献就为至少被粉刷过一次的概率×1(每个格子的最大贡献就是1...)每个方格至少 ...