博客
关于我
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/

    你可能感兴趣的文章
    Nacos2.X 源码分析:为订阅方推送、服务健康检查、集群数据同步、grpc客户端服务端初始化
    查看>>
    Nacos2.X 配置中心源码分析:客户端如何拉取配置、服务端配置发布客户端监听机制
    查看>>
    Nacos2.X源码分析:服务注册、服务发现流程
    查看>>
    NacosClient客户端搭建,微服务注册进nacos
    查看>>
    Nacos中使用ribbon
    查看>>
    Nacos使用OpenFeign
    查看>>
    Nacos使用Ribbon
    查看>>
    Nacos做注册中心使用
    查看>>
    Nacos做配置中心使用
    查看>>
    Nacos入门过程的坑--获取不到配置的值
    查看>>
    Nacos原理
    查看>>
    Nacos发布0.5.0版本,轻松玩转动态 DNS 服务
    查看>>
    Nacos启动异常
    查看>>
    Nacos命名空间配置_每个人用各自自己的命名空间---SpringCloud Alibaba_若依微服务框架改造---工作笔记001
    查看>>
    Nacos和Zookeeper对比
    查看>>
    Nacos在双击startup.cmd启动时提示:Unable to start embedded Tomcat
    查看>>
    Nacos基础版 从入门到精通
    查看>>
    Nacos如何实现Raft算法与Raft协议原理详解
    查看>>
    Nacos安装教程(非常详细)从零基础入门到精通,看完这一篇就够了
    查看>>
    Nacos实战攻略:从入门到精通,全面掌握服务治理与配置管理!(上)
    查看>>