博客
关于我
hdu2852
阅读量:727 次
发布时间:2019-03-17

本文共 1490 字,大约阅读时间需要 4 分钟。

经过对代码和解释部分的优化,以下是优化后的内容:

#include 
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;#define scd(n) scanf("%d", &n)#define scdd(a, b) scanf("%d%d", &a, &b)#define lowbit(x) x & (-x)typedef long long LL;typedef pair
PII;const int N = 1e5 + 7;const int INF = 0x3f3f3f3f;int a[N];int tr[N];int m, k;void add(int x, int c) { for (int i = x; i <= N; i += lowbit(i)) { tr[i] += c; }}LL sum(int x) { LL res = 0; for (int i = x; i > 0; i -= lowbit(i)) { res += tr[i]; } return res;}void solve() { memset(tr, 0, sizeof(tr)); memset(a, 0, sizeof(a)); while (m--) { int p, e; scd(p); if (p == 0) { scd(e); a[e]++; add(e, 1); } else if (p == 1) { scd(e); if (!a[e]) { puts("No Element!"); } else { a[e]--; add(e, -1); } } else { int a_val, k_val; scdd(a_val, k_val); LL s = sum(a_val); int left = 1, right = N; while (left < right) { int mid = left + (right - left) / 2; if (sum(mid) >= s + k_val) { right = mid; } else { left = mid + 1; } } if (sum(left) >= s + k_val) { printf("%d\n", left); } else { puts("Not Found!"); } } }}int main() { // int t; // scd(t); // while (cin >> t) { solve(); // } return 0;}

解释部分:

本程序实现了三个主要操作:

  • 插入:当一个新元素被插入时,我们将该元素的值加一,并在树状数组中更新对应位置的计数。

  • 删除:当一个元素被删除时,我们将该元素的值减一,并在树状数组中更新对应位置的计数。

  • 查找:要查找容器中大于给定值$a$的第$k$个元素,我们可以通过二分查找来实现。具体方法是使用树状数组来快速计算前缀和,并找到满足条件的最小值。

  • 树状数组是一种高效的数据结构,支持快速的前缀和查询和更新操作。这个程序充分利用了树状数组的优势,保证了每个操作的时间复杂度较低,适用于大规模数据的处理。

    转载地址:http://hnfez.baihongyu.com/

    你可能感兴趣的文章
    MySQL 多表联合查询:UNION 和 JOIN 分析
    查看>>
    MySQL 大数据量快速插入方法和语句优化
    查看>>
    mysql 如何给SQL添加索引
    查看>>
    mysql 字段区分大小写
    查看>>
    mysql 字段合并问题(group_concat)
    查看>>
    mysql 字段类型类型
    查看>>
    MySQL 字符串截取函数,字段截取,字符串截取
    查看>>
    MySQL 存储引擎
    查看>>
    mysql 存储过程 注入_mysql 视图 事务 存储过程 SQL注入
    查看>>
    MySQL 存储过程参数:in、out、inout
    查看>>
    mysql 存储过程每隔一段时间执行一次
    查看>>
    mysql 存在update不存在insert
    查看>>
    Mysql 学习总结(86)—— Mysql 的 JSON 数据类型正确使用姿势
    查看>>
    Mysql 学习总结(87)—— Mysql 执行计划(Explain)再总结
    查看>>
    Mysql 学习总结(88)—— Mysql 官方为什么不推荐用雪花 id 和 uuid 做 MySQL 主键
    查看>>
    Mysql 学习总结(89)—— Mysql 库表容量统计
    查看>>
    mysql 实现主从复制/主从同步
    查看>>
    mysql 审核_审核MySQL数据库上的登录
    查看>>
    mysql 导入 sql 文件时 ERROR 1046 (3D000) no database selected 错误的解决
    查看>>
    mysql 导入导出大文件
    查看>>