KD Tree

news/2025/10/24 13:12:59/文章来源:https://www.cnblogs.com/xihegudi/p/19162999

KD Tree

在第 \(k\) 维上的单次查询复杂度最坏为 \(\mathcal O(n^{1-k^{-1}})\)

struct KDT {constexpr static int N = 1e5 + 10, K = 2;double alpha = 0.725;struct node {int info[K];int mn[K], mx[K];} tr[N];int ls[N], rs[N], siz[N], id[N], d[N];int idx, rt, cur;int ans;KDT() {rt = 0;cur = 0;memset(ls, 0, sizeof ls);memset(rs, 0, sizeof rs);memset(d, 0, sizeof d);}void apply(int p, int son) {if (son) {for (int i = 0; i < K; i++) {tr[p].mn[i] = min(tr[p].mn[i], tr[son].mn[i]);tr[p].mx[i] = max(tr[p].mx[i], tr[son].mx[i]);}siz[p] += siz[son];}}void maintain(int p) {for (int i = 0; i < K; i++) {tr[p].mn[i] = tr[p].info[i];tr[p].mx[i] = tr[p].info[i];}siz[p] = 1;apply(p, ls[p]);apply(p, rs[p]);}int build(int l, int r) {if (l > r) return 0;vector<double> avg(K);for (int i = 0; i < K; i++) {for (int j = l; j <= r; j++) {avg[i] += tr[id[j]].info[i];}avg[i] /= (r - l + 1);}vector<double> var(K);for (int i = 0; i < K; i++) {for (int j = l; j <= r; j++) {var[i] += (tr[id[j]].info[i] - avg[i]) * (tr[id[j]].info[i] - avg[i]);}}int mid = (l + r) / 2;int x = max_element(var.begin(), var.end()) - var.begin();nth_element(id + l, id + mid, id + r + 1, [&](int a, int b) {return tr[a].info[x] < tr[b].info[x];});d[id[mid]] = x;ls[id[mid]] = build(l, mid - 1);rs[id[mid]] = build(mid + 1, r);maintain(id[mid]);return id[mid];}void print(int p) {if (!p) return;print(ls[p]);id[++idx] = p;print(rs[p]);}void rebuild(int &p) {idx = 0;print(p);p = build(1, idx);}bool bad(int p) {return alpha * siz[p] <= max(siz[ls[p]], siz[rs[p]]);}void insert(int &p, int cur) {if (!p) {p = cur;maintain(p);return;}if (tr[p].info[d[p]] > tr[cur].info[d[p]]) insert(ls[p], cur);else insert(rs[p], cur);maintain(p);if (bad(p)) rebuild(p);}void insert(vector<int> &a) {cur++;for (int i = 0; i < K; i++) {tr[cur].info[i] = a[i];}insert(rt, cur);}bool out(int p, vector<int> &a) {for (int i = 0; i < K; i++) {if (a[i] < tr[p].mn[i]) {return true;}}return false;}bool in(int p, vector<int> &a) {for (int i = 0; i < K; i++) {if (a[i] < tr[p].info[i]) {return false;}}return true;}bool all(int p, vector<int> &a) {for (int i = 0; i < K; i++) {if (a[i] < tr[p].mx[i]) {return false;}}return true;}void query(int p, vector<int> &a) {if (!p) return;if (out(p, a)) return;if (all(p, a)) {ans += siz[p];return;}if (in(p, a)) ans++;query(ls[p], a);query(rs[p], a);}int query(vector<int> &a) {ans = 0;query(rt, a);return ans;}
};

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/945203.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

小波矩阵树:高效静态区间第 K 大查询

小波矩阵树:高效静态区间第 K 大查询 手写 bitset 压位,以 \(\mathcal O(N \log N)\) 的时间复杂度和 \(\mathcal O(N + \frac{N \log N}{64})\) 的空间建树后,实现单次 \(\mathcal O(\log N)\) 复杂度的区间第 \(k…

Seata用法

目录AT模式TCCSAGA模式参考资料 AT模式在Springboot启动程序加上,@EnableAutoDataSourceProxy 确保数据源被代理 在全局事务启动的方法上加 @GlobalTransactional 分支事务代码无需做任何处理。TCC 实现类加上 @Local…

Day3多媒体标签——视频与音频

<!DOCTYPE html> <html lang="en"> <head><meta charset="UTF-8"><meta name="viewport" content="width= , initial-scale=1.0"><title&g…

分数运算类

分数运算类 定义了分数的四则运算,如果需要处理浮点数,那么需要将函数中的 gcd 运算替换为 fgcd 。 template<class T> struct Frac {T x, y;Frac() : Frac(0, 1) {}Frac(T x_) : Frac(x_, 1) {}Frac(T x_, T …

坐标压缩与离散化

坐标压缩与离散化 简单版本 sort(alls.begin(), alls.end()); alls.erase(unique(alls.begin(), alls.end()), alls.end()); auto get = [&](int x) {return lower_bound(alls.begin(), alls.end(), x) - alls.beg…

撸一个功能强大的基于语义的图像检索系统

构建了一个支持中英文和图像检索的自定义系统。通过PyQt5开发GUI界面,集成KimiAI实现中文翻译,解决了原生框架中文支持差、界面固定等问题。系统核心功能包括:1)中文文本检索(自动翻译为英文);2)以图搜图(基于…

提交一张 PPT,参与 RTE2025 全球语音智能体云展示

无法亲临 RTE2025 大会?😢 没关系!🎉 我们特别为你的项目提供了一个云展示机会。只需提交一张 PPT,即可参与我们的「全球语音智能体云展示」,与众多领先的语音智能体一同在大会展区屏幕上轮播展示! 🤩项目提…

完整教程:深入解析AppCrawler:开源自动遍历测试工具配置指南

完整教程:深入解析AppCrawler:开源自动遍历测试工具配置指南pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: block !important; font-family: "Con…

解释 EIP-4337

简单来说,EIP-4337 旨在实现“账户抽象”,让智能合约钱包成为用户的默认和主流钱包,从而极大地改善用户体验和安全性。 下面我将从几个方面详细解释 EIP-4337: 1. 核心问题:以太坊的两种账户 在理解 EIP-4337 之前…

数论常见结论及例题

数论常见结论及例题 常见结论 球盒模型(八种) 参考链接。给定 \(n\) 个小球 \(m\) 个盒子。球同,盒不同、不能空隔板法: \(N\) 个小球即一共 \(N-1\) 个空,分成 \(M\) 堆即 \(M-1\) 个隔板,答案为 \(\dbinom{n-1…

材料包含与下载漏洞

pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: block !important; font-family: "Consolas", "Monaco", "Courier New", …

N8N Workflow Collection - 专业级自动化工作流库 - 详解

pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: block !important; font-family: "Consolas", "Monaco", "Courier New", …

完整教程:Elasticsearch面试精讲 Day 23:安全认证与权限控制

完整教程:Elasticsearch面试精讲 Day 23:安全认证与权限控制pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: block !important; font-family: "Con…

Min25 筛

Min25 筛 求解 \(1-N\) 的质数和,其中 \(N \le 10^{10}\) 。 namespace min25{const int N = 1000000 + 10;int prime[N], id1[N], id2[N], flag[N], ncnt, m;LL g[N], sum[N], a[N], T;LL n;LL mod;inline LL ps(LL …

莫比乌斯函数/反演

莫比乌斯函数/反演 莫比乌斯函数定义:\(\displaystyle {\mu(n) = \begin{cases} 1 &n = 1 \\ (-1)^k &n = \prod_{i = 1}^k p_i \text{ 且 } p_i \text{ 互质 } \\ 0 &else \end{cases}}\) 。莫比乌斯函数…

同余方程组、拓展中国剩余定理 excrt

同余方程组、拓展中国剩余定理 excrt 公式:\(x \equiv b_i(\bmod\ a_i)\) ,即 \((x - b_i) \mid a_i\) 。 int n; LL ai[maxn], bi[maxn]; inline int mypow(int n, int k, int p) {int r = 1;for (; k; k >>=…

完整教程:微软2025教育AI报告:教育群体采用AI的比例显著提升

完整教程:微软2025教育AI报告:教育群体采用AI的比例显著提升pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: block !important; font-family: "Con…

康拓展开

康拓展开 正向展开普通解法 将一个字典序排列转换成序号。例如:12345->1,12354->2。 int f[20]; void jie_cheng(int n) { // 打出1-n的阶乘表f[0] = f[1] = 1; // 0的阶乘为1for (int i = 2; i <= n; i++)…

求解连续数字的正约数集合——倍数法

求解连续数字的正约数集合——倍数法 使用规律递推优化,时间复杂度为 \(\mathcal{O}(N\log N)\) ,如果不需要详细的输出集合,则直接将 vector 换为普通数组即可(时间更快) 。 #include <bits/stdc++.h> usi…

git回滚代码

回滚上一次提交是指撤销最近一次的git提交操作。在实际使用中,有两种常见的方法可以实现这个操作: 方法一:使用git revert命令回滚 1. 首先,通过命令`git log`查看提交记录,找到要回滚的提交的hash值。 2. 使用命…