博客
关于我
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第一天~mysql基础【主要是DDL、DML、DQL语句,以及重点掌握存存引擎、查询(模糊查询)】
    查看>>
    mysql第二天~mysql基础【查询排序、分页查询、多表查询、数据备份与恢复等】
    查看>>
    MySQL简介和安装
    查看>>
    MySQL简单查询
    查看>>
    MySQL管理利器 MySQL Utilities 安装
    查看>>
    MySQL篇(管理工具)
    查看>>
    mysql类型转换函数convert与cast的用法
    查看>>
    mysql系列一
    查看>>
    MySQL系列之数据类型(Date&Time)
    查看>>
    MySQL系列之数据类型(Date&Time)
    查看>>
    Mysql系列之锁机制
    查看>>
    Mysql系列九:使用zookeeper管理远程Mycat配置文件、Mycat监控、Mycat数据迁移(扩容)...
    查看>>
    MySql系列:[4200][1140]In aggregated query without GROUP BY, expression #2 of SELECT list contains nona
    查看>>
    Mysql索引
    查看>>
    mysql索引
    查看>>
    mysql索引
    查看>>
    Mysql索引,索引的优化,如何避免索引失效案例
    查看>>
    Mysql索引、命令重点介绍
    查看>>
    mysql索引、索引优化(这一篇包括所有)
    查看>>
    Mysql索引一篇就够了
    查看>>