当前位置: 首页 > news >正文

坐标压缩与离散化

坐标压缩与离散化

简单版本

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.begin();
};

封装

template <typename T> struct Compress_ {int n, shift = 0; // shift 用于标记下标偏移量vector<T> alls;Compress_() {}Compress_(auto in) : alls(in) {init();}void add(T x) {alls.emplace_back(x);}template <typename... Args> void add(T x, Args... args) {add(x), add(args...);}void init() {alls.emplace_back(numeric_limits<T>::max());sort(alls.begin(), alls.end());alls.erase(unique(alls.begin(), alls.end()), alls.end());this->n = alls.size();}int size() {return n;}int operator[](T x) { // 返回 x 元素的新下标return upper_bound(alls.begin(), alls.end(), x) - alls.begin() + shift;}T Get(int x) { // 根据新下标返回原来元素assert(x - shift < n);return x - shift < n ? alls[x - shift] : -1;}bool count(T x) { // 查找元素 x 是否存在return binary_search(alls.begin(), alls.end(), x);}friend auto &operator<< (ostream &o, const auto &j) {cout << "{";for (auto it : j.alls) {o << it << " ";}return o << "}";}
};
using Compress = Compress_<int>;
http://www.jsqmd.com/news/21206/

相关文章:

  • 撸一个功能强大的基于语义的图像检索系统
  • 提交一张 PPT,参与 RTE2025 全球语音智能体云展示
  • 完整教程:深入解析AppCrawler:开源自动遍历测试工具配置指南
  • 解释 EIP-4337
  • 数论常见结论及例题
  • 材料包含与下载漏洞
  • N8N Workflow Collection - 专业级自动化工作流库 - 详解
  • 完整教程:Elasticsearch面试精讲 Day 23:安全认证与权限控制
  • Min25 筛
  • 莫比乌斯函数/反演
  • 同余方程组、拓展中国剩余定理 excrt
  • 完整教程:微软2025教育AI报告:教育群体采用AI的比例显著提升
  • 求解连续数字的正约数集合——倍数法
  • 扩展欧几里得 exgcd
  • 离散对数 bsgs 与 exbsgs
  • 防爆模乘
  • 欧拉筛(线性筛)
  • 常见数列
  • 20232314 2025-2026-1 《网络与系统攻防技术》实验三实验报告
  • 【LTDC】LTDC 简介
  • Markdown数学公式 - -一叶知秋
  • 分类器案例 - -一叶知秋
  • 最大流
  • 最小割树 Gomory-Hu Tree
  • 最小割
  • 费用流
  • 图论常见结论及例题
  • 查询GPIO状态值(步骤)
  • 最长路(topsort+DP算法)
  • 最短路径树(SPT问题)