Multiset 题解
考虑求排名怎么做。
一种空间比较优秀的做法就是树状数组加二分找排名,即值域树状数组上二分。
然后删除即在树状数组上将数的值
复杂度应该是
#pragma GCC optimize("-Ofast,inline,fast-math")
#pragma GCC target("avx,sse,sse2,sse3,sse4")
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>
using namespace std;
const int N = 1e6 + 5;
inline int read()
{
char ch(getchar());
int x = 0, o = 1;
while ((ch < '0' || ch > '9') && ch != '-') ch = getchar();
while (ch == '-') o = -o, ch = getchar();
while (ch >= '0' and ch <= '9')
{
x = (x << 1) + (x << 3) + (ch ^ 48);
ch = getchar();
}
return x * o;
}
int n, a[N], q, k;
class Bit
{
public:
int tr[N];
void update(int u, int x)
{
while (u <= n)
{
tr[u] += x;
u += u & -u;
}
}
int query(int u)
{
int res = 0;
while (u)
{
res += tr[u];
u -= u & -u;
}
return res;
}
}tr;
int main()
{
n = read(), q = read();
for (int i = 1; i <= n; ++i)
{
a[i] = read();
tr.update(a[i], 1);
}
while (q--)
{
k = read();
if (k < 0)
{
int l = 1, r = n, res = 0;
while (l <= r)
{
int mid = l + r >> 1;
if (tr.query(mid) >= -k)
{
r = mid - 1;
res = mid;
}
else
{
l = mid + 1;
}
}
if (res)
{
tr.update(res, -1);
}
}
else
{
tr.update(k, 1);
}
}
for (int i = 1; i <= n; ++i)
{
if (tr.query(i) - tr.query(i - 1))
{
printf("%d\n", i);
return 0;
}
}
printf("0\n");
return 0;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现