题目链接:https://www.spoj.com/problems/COT/en/
题意: 给你一颗树,问你u,v结点这条路径上第k大是多少。


思路:就是说先将无根树转化为有根树,然后对每一条链建立一个主席树再相减查询


#include <iostream>
#include <cstdio>
#include <stack>
#include <sstream>
#include <vector>
#include <map>
#include <cstring>
#include <deque>
#include <cmath>
#include <iomanip>
#include <queue>
#include <algorithm>
#include <set>
#define mid ((l + r) >> 1)
#define Lson rt << 1, l , mid
#define Rson rt << 1|1, mid + 1, r
#define ms(a,al) memset(a,al,sizeof(a))
#define log2(a) log(a)/log(2)
#define _for(i,a,b) for( int i = (a); i < (b); ++i)
#define _rep(i,a,b) for( int i = (a); i <= (b); ++i)
#define for_(i,a,b) for( int i = (a); i >= (b); -- i)
#define rep_(i,a,b) for( int i = (a); i > (b); -- i)
#define lowbit(x) ((-x) & x)
#define IOS std::ios::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define INF 0x3f3f3f3f
#define hash Hash
#define next Next
#define count Count
#define pb push_back
#define f first
#define s second
using namespace std;
const int N = 1e5+10, mod = 10007;
const double eps = 1e-10;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> PII;
inline LL read()
{LL f=1,s=0;char ch=getchar();while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}while(ch>='0'&&ch<='9')s=(s<<1)+(s<<3)+ch-'0',ch=getchar();return s*f;
}
int n,m;
int a[N];struct node {int to, next;
}e[N << 2];
int head[N], cnt;int f[N][20], depth[N];struct Tree{int l, r, cnt;
}tr[N * 40];
int root[N], idx;
vector<int> ys;inline int getid(int x)
{return lower_bound(ys.begin(),ys.end(),x) - ys.begin() + 1;
}inline void add(int from, int to)
{e[cnt] = {to,head[from]};head[from] = cnt ++;
}inline int build(int l, int r)
{int now = ++ idx;if(l == r) return now;tr[now].l = build(l,mid);tr[now].r = build(mid+1,r);return now;
}inline int insert(int l, int r, int pre, int pos)
{int now = ++ idx;tr[now] = tr[pre];if(l == r){tr[now].cnt ++;return now;}if(pos <= mid)tr[now].l = insert(l,mid,tr[pre].l,pos);else tr[now].r = insert(mid+1,r,tr[pre].r,pos);tr[now].cnt = tr[tr[now].l].cnt + tr[tr[now].r].cnt;return now;
}inline void dfs(int u, int fa, int d)
{depth[u] = d; f[u][0] = fa;//  cout << u << " " << fa << " " << getid(a[u]) << endl;root[u] = insert(1,ys.size(),root[fa],getid(a[u]));for(int i = head[u]; ~i; i = e[i].next){int v = e[i].to;if(v != fa) dfs(v,u,d+1);}
}inline void init()
{root[0] = build(1,ys.size());dfs(1,0,0);for(int j = 0; (1 << (j + 1)) < n; j ++){for(int i = 1; i <= n; i ++)if(f[i][j] == 0) f[i][j + 1] = 0;else f[i][j + 1] = f[f[i][j]][j];}
}inline int LCA(int l, int r)
{if(depth[l] < depth[r]) swap(l,r);int temp = depth[l] - depth[r];for(int i = 0; (1 << i) <= temp; i ++) if((1 << i) & temp) l = f[l][i];if(l == r) return l;for(int i = log2(n); i >= 0; i --)if(f[l][i] != f[r][i]) {l = f[l][i]; r = f[r][i];}return f[l][0];
}inline int query(int l, int r, int ltree, int rtree, int lcatree, int lcafa, int k)
{if(l == r) return l;int Eps = tr[tr[ltree].l].cnt + tr[tr[rtree].l].cnt -  tr[tr[lcatree].l].cnt - tr[tr[lcafa].l].cnt;if(k <= Eps) return query(l,mid,tr[ltree].l,tr[rtree].l,tr[lcatree].l,tr[lcafa].l,k);else return query(mid+1,r,tr[ltree].r,tr[rtree].r,tr[lcatree].r,tr[lcafa].r,k - Eps);
}void debug(int rt, int l, int r)
{cout << tr[tr[rt].l].cnt << " " << tr[tr[rt].r].cnt << endl;cout << endl;if(l == r) return;debug(tr[rt].l,l,mid);debug(tr[rt].r,mid+1,r);
}int main()
{ms(head,-1);n = read(), m = read();_for(i,1,n+1) ys.pb(a[i] = read());sort(ys.begin(),ys.end());ys.erase(unique(ys.begin(),ys.end()),ys.end());_for(i,0,n-1){int l, r;l = read(), r = read();add(l,r);add(r,l);}init();while(m --){int l, r, k;l = read(), r = read(), k = read();int lca = LCA(l,r);printf("%d\n",ys[query(1,ys.size(),root[l],root[r],root[lca],root[f[lca][0]],k) - 1]);}return 0;
}

主席树 ---- LCA(树上第k大)Count on a tree相关推荐

  1. 主席树 || 可持久化线段树 || LCA || BZOJ 2588: Spoj 10628. Count on a tree || Luogu P2633 Count on a tree...

    题面: Count on a tree 题解: 主席树维护每个节点到根节点的权值出现次数,大体和主席树典型做法差不多,对于询问(X,Y),答案要计算ans(X)+ans(Y)-ans(LCA(X,Y) ...

  2. Greedy Sequence(2019南京icpc网络预选赛)主席树求区间小于k的最大值

    题意:给出n个整数,构造s1,s2,s3-sn s1,s2,s3-sns1,s2,s3-sn,si sisi满足五个条件 1.s1[i]=i s1[i]=is1[i]=i 2.对于1<j< ...

  3. 【代码源 Div1 - 108】#464. 数数(主席树,区间比k小的数的个数)HDU4417

    problem solution 主席树查询区间比k小的数的个数 建树之后直接在目标区间的主席树内将 H 作为挡板递归计数. #include<bits/stdc++.h> using n ...

  4. 【洛谷 P2633】 Count on a tree(主席树,树上差分)

    题目链接 思维难度0 实现难度7 建出主席树后用两点的状态减去lca和lca父亲的状态,然后在新树上跑第\(k\)小 #include <cstdio> #include <cstr ...

  5. P3302-[SDOI2013]森林【主席树,LCA,启发式合并】

    正题 题目链接:https://www.luogu.com.cn/problem/P3302 题目大意 nnn个点的一个森林,要求支持以下操作 连接两个不连通的点 询问两个点之间最短路径上第kkk大的 ...

  6. [noip2016]天天爱跑步(主席树+lca)

    恩..在百度的第一页翻了翻,没有用主席树做的,于是打算水一篇blog: 天天爱跑步 首先有一个解题的关键: 将玩家的向上和向下分成两部分: 先定义几个变量,s是起点,t是终点,wi是每个观察员出现的时 ...

  7. P4175 [CTSC2008]网络管理 (动态树上第k大)

    题目链接: P4175 [CTSC2008]网络管理 大致题意 给定一棵有nnn个节点的树, 节点上有权值wiw_iwi​. 有mmm次操作. 0 a b 表示把节点aaa的权值修改为bbb. 即: ...

  8. Just $h$-index HDU - 6278(主席树找区间大于等于k的个数)

    The hh-index of an author is the largest hh where he has at least hh papers with citations not less ...

  9. P2633-Count on a tree【主席树,LCA】

    正题 题目链接:https://www.luogu.com.cn/problem/P2633 题目大意 nnn个点的树,每个点有点权,求u∼vu\sim vu∼v的路径上第kkk小的权值,强制在线. ...

最新文章

  1. AI公司融了1500万美元,却引来LeCun「嘲讽三连」,还要出书调侃CEO?
  2. go get 获取被墙依赖包解决方法
  3. Git学习笔记——分支
  4. 隐私设置错误您的连接不是私密连接_用小米手机的,赶紧将这些保护个人隐私的功能设置起来,安全好用...
  5. C++ 序列化 serialization 如何将类持久化?
  6. plsql删除大量数据_一次oracle大量数据删除经历
  7. 遇到一个Linux系统时间突变问题
  8. JavaScript 中数组 sort() 方法的基本使用
  9. jQuery Mobile中按钮button的data-*选项
  10. SAP FICO面试题
  11. m6A甲基化及预测方法工具总结
  12. 项目管理与SSM框架——Spring
  13. 没噪音免更换滤网?贝昂JY200净化器评测
  14. Google Earth Engine APP(GEE)——用一个选择器选择不同城市的应用
  15. 以一例教你找到【无水印】【高清】【百度图片】的url
  16. dbo sysadmin和db_owner的区别
  17. 关于最新版mumu模拟器(2.2.16)安装xposed框架
  18. 互联网和万维网的区别
  19. 2015中国程序员生存报告
  20. Latex beamer

热门文章

  1. Java基本数据类型转换|字符和字符串
  2. 知识产权一般都具有法定的保护期限,一旦保护期限届满,权利将自行终止,成为社会公众可以自由使用的知识。 (20答案:D
  3. PyTorch中模型的可复现性
  4. 实用技能 | OpenCV算法程序导出 DLL与调用演示
  5. 从技术角度分析推荐系统案例
  6. Sequelize 4.43.0 发布,基于 Nodejs 的异步 ORM 框架
  7. spi nor flash使用汇总
  8. BizTalk 开发小技巧-Custom XSLT复杂mapping的简单实现
  9. python爬虫入门教程--优雅的HTTP库requests(二)
  10. matplotlib中文文档_python绘图库——Matplotlib及Seaborn使用(入门篇1)