「luogu2633」Count on a tree
「luogu2633」Count on a tree
传送门
树上主席树板子。
每个节点的根从其父节点更新得到,查询的时候差分一下就好了。
参考代码:
#include <algorithm>
#include <cstdio>
#define rg register
#define file(x) freopen(x".in", "r", stdin), freopen(x".out", "w", stdout)
using namespace std;
template < class T > inline void read(T& s) {
s = 0; int f = 0; char c = getchar();
while ('0' > c || c > '9') f |= c == '-', c = getchar();
while ('0' <= c && c <= '9') s = s * 10 + c - 48, c = getchar();
s = f ? -s : s;
}
const int _ = 1e5 + 5;
int tot, head[_], nxt[_ << 1], ver[_ << 1];
inline void Add_edge(int u, int v)
{ nxt[++tot] = head[u], head[u] = tot, ver[tot] = v; }
int n, q, a[_], X[_];
int dep[_], siz[_], son[_], fa[_], top[_];
int tt, rt[_], lc[_ << 5], rc[_ << 5], cnt[_ << 5];
inline void update(int& p, int q, int v, int l = 1, int r = X[0]) {
p = ++tt, lc[p] = lc[q], rc[p] = rc[q], cnt[p] = cnt[q] + 1;
if (l == r) return ;
int mid = (l + r) >> 1;
if (v <= mid) update(lc[p], lc[q], v, l, mid);
else update(rc[p], rc[q], v, mid + 1, r);
}
inline int query(int u, int v, int lca, int flca, int k, int l = 1, int r = X[0]) {
if (l == r) return l;
int mid = (l + r) >> 1;
int num = cnt[lc[u]] + cnt[lc[v]] - cnt[lc[lca]] - cnt[lc[flca]];
if (num >= k) return query(lc[u], lc[v], lc[lca], lc[flca], k, l, mid);
else return query(rc[u], rc[v], rc[lca], rc[flca], k - num, mid + 1, r);
}
inline void dfs(int u, int f) {
dep[u] = dep[f] + 1, siz[u] = 1, fa[u] = f;
update(rt[u], rt[f], a[u]);
for (rg int i = head[u]; i; i = nxt[i]) {
int v = ver[i]; if (v == f) continue ;
dfs(v, u), siz[u] += siz[v];
if (siz[son[u]] < siz[v]) son[u] = v;
}
}
inline void dfs(int u, int f, int topf) {
top[u] = topf;
if (son[u]) dfs(son[u], u, topf);
for (rg int i = head[u]; i; i = nxt[i]) {
int v = ver[i]; if (v == f || v == son[u]) continue ;
dfs(v, u, v);
}
}
inline int LCA(int x, int y) {
int fx = top[x], fy = top[y];
while (fx != fy) {
if (dep[fx] < dep[fy]) swap(fx, fy), swap(x, y);
x = fa[fx], fx = top[x];
}
return dep[x] < dep[y] ? x : y;
}
int main() {
read(n), read(q);
for (rg int i = 1; i <= n; ++i) read(a[i]), X[i] = a[i];
sort(X + 1, X + n + 1);
X[0] = unique(X + 1, X + n + 1) - X - 1;
for (rg int i = 1; i <= n; ++i) a[i] = lower_bound(X + 1, X + X[0] + 1, a[i]) - X;
for (rg int u, v, i = 1; i < n; ++i)
read(u), read(v), Add_edge(u, v), Add_edge(v, u);
dfs(1, 0), dfs(1, 0, 1);
for (rg int ans = 0, u, v, k, lca; q--; ) {
read(u), u ^= ans, read(v), read(k), lca = LCA(u, v);
ans = X[query(rt[u], rt[v], rt[lca], rt[fa[lca]], k)], printf("%d\n", ans);
}
return 0;
}
「luogu2633」Count on a tree的更多相关文章
- 「SPOJ10707」Count on a tree II
「SPOJ10707」Count on a tree II 传送门 树上莫队板子题. 锻炼基础,没什么好说的. 参考代码: #include <algorithm> #include &l ...
- 「SPOJ1487」Query on a tree III
「SPOJ1487」Query on a tree III 传送门 把树的 \(\text{dfs}\) 序抠出来,子树的节点的编号位于一段连续区间,然后直接上建主席树区间第 \(k\) 大即可. 参 ...
- 「SP10628 COT - Count on a tree」
主席树的综合运用题. 前置芝士 可持久化线段树:其实就是主席树了. LCA:最近公共祖先,本题需要在\(\log_2N\)及以内的时间复杂度内解决这个问题. 具体做法 主席树维护每个点到根节点这一条链 ...
- 「CF741D」Arpa’s letter-marked tree and Mehrdad’s Dokhtar-kosh paths
传送门 Luogu 解题思路 考虑把22个字符状压下来,易知合法情况就是状态中之多有一个1,这个可以暴力一点判断23次. 然后后就是 dsu on the tree 了. 细节注意事项 咕咕咕 参考代 ...
- 「CF375E」Red and Black Tree「树形DP」
题意 给定一个结点颜色红或黑的树,问最少进行多少次交换黑.红结点使得每个红结点离最近的黑结点距离\(\leq x\). \(1\leq n \leq 500, 1 \leq x \leq 10^9\) ...
- LoibreOJ 2042. 「CQOI2016」不同的最小割 最小割树 Gomory-Hu tree
2042. 「CQOI2016」不同的最小割 内存限制:256 MiB时间限制:1000 ms标准输入输出 题目类型:传统评测方式:文本比较 上传者: 匿名 提交提交记录统计讨论测试数据 题目描述 ...
- 「BZOJ2654」tree
「BZOJ2654」tree 最小生成树+二分答案. 最开始并没有觉得可以二分答案,因为答案并不单调啊. 其实根据题意,白边的数目肯定大于need条,而最小生成树的白边数并不等于need(废话),可以 ...
- 「AGC035C」 Skolem XOR Tree
「AGC035C」 Skolem XOR Tree 感觉有那么一点点上道了? 首先对于一个 \(n\),若 \(n\equiv 3 \pmod 4\),我们很快能够构造出一个合法解如 \(n,n-1, ...
- 「AGC010F」 Tree Game
「AGC010F」 Tree Game 传送门 切了一个 AGC 的题,很有精神. 于是决定纪念一下. 首先如果任意一个人在点 \(u\),他肯定不会向点权大于等于 \(a_u\) 的点走的,因为此时 ...
随机推荐
- 吴裕雄 python 机器学习——模型选择分类问题性能度量
import numpy as np import matplotlib.pyplot as plt from sklearn.svm import SVC from sklearn.datasets ...
- tensorflow之tensorboard
参考https://www.cnblogs.com/felixwang2/p/9184344.html 边学习,边练习 # https://www.cnblogs.com/felixwang2/p/9 ...
- Laravel Vuejs 实战:开发知乎 (6)发布问题
1.view部分: 安装一个扩展包:Laravel-UEditor composer require "overtrue/laravel-ueditor:~1.0" 配置 添加下面 ...
- 12306 selenium 模拟登录
# 下面是12306 实现的模拟登陆 # 解码 应用超级鹰,注册用户,左侧栏软件ID进去,开启一个新软件,拿到软件ID # 下面测试都在jupyter里面实现 # 超级鹰类 cell import r ...
- 【前端之BOM和DOM】
" 目录 #. window对象介绍 #. window子对象 1. 浏览器对象 navigator 2. 屏幕对象 screen 3. 历史 history 4. 地址(URL) loc ...
- Vue-设置默认路由选中
需求分析: 一个导航组件,需要其中一个是选中状态,并且样式呈现高亮,选中的导航对应的页面也需要展示出来. 功能实现: router-link内置有一个选中状态,当处于当前路由时,会给 router-l ...
- Unix套接字接口
简介 套接字是操作系统中用于网络通信的重要结构,它是建立在网络体系结构的传输层,用于主机之间数据的发送和接收,像web中使用的http协议便是建立在socket之上的.这一节主要讨论网络套接字. 套接 ...
- Javascript——(1)
1.Javascript有两种解释表示形式:1)在html的<header>中写<script><script/>,另一种是将另一个文件保存为xxx.js文档,然后 ...
- 例题3_1 TeX中的引号(TeX Quotes,UVa 272)
在TeX中,左双引号是“``”,右双引号是“''”.输入一篇包含双引号的文章,你的任务是把它转换成TeX的格式. 样例输入: "To be or not to be,"quoth ...
- map或者对象转换
map或者对象转换为具有相同字段的对象 List<Example> errorCodeExcelBeanList = JSONObject.parseArray(((JSONObject) ...