SP30906 ADAUNIQ - Ada and Unique Vegetable
传送门:SP30906 ADAUNIQ - Ada and Unique Vegetable。
题意
给定一个
解法
大家可以先做 另一道题,与这道题基本一样。
本题需要维护修改操作,所以可以使用带修莫队。考虑对于每一个询问操作添加一个时间戳
另外排序时,我们考虑对
代码:
#pragma GCC optimize(2)
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
using namespace std;
const int N = 2e5 + 5;
int a[N], cnt[N], ans[N], len, ca = -1, cb = 0, n, m;
inline int read()
{
register char ch = getchar();
register int x = 0;
while (ch < '0' || ch > '9') ch = getchar();
while (ch >= '0' && ch <= '9')
{
x = (x << 1) + (x << 3) + (ch ^ 48);
ch = getchar();
}
return x;
}
struct Query
{
int id, l, r, t;
}q[N];
struct Modify
{
int x, y;
/*
将 a[x] 改成 y
*/
}mod[N];
int get(int x)
{
return x / len;
}
inline bool cmp(const Query& x, const Query& y)
{
int xl = get(x.l), xr = get(x.r);
int yl = get(y.l), yr = get(y.r);
if (xl != yl) return xl < yl;
if (xr != yr) return xr < yr;
return x.t < y.t;
}
inline void add(int x, int& res)
{
cnt[x]++;
if (cnt[x] == 1) res++;
else if (cnt[x] == 2) res--;
}
inline void del(int x, int& res)
{
cnt[x]--;
if (cnt[x] == 1) res++;
else if (cnt[x] == 0) res--;
}
inline void change(int x, int l, int r, int& res)
{
if (mod[x].x >= l && mod[x].x <= r)
{
del(a[mod[x].x], res);
add(mod[x].y, res);
}
swap(a[mod[x].x], mod[x].y);
}
int main()
{
n = read(), m = read();
for (int i = 1; i <= n; i++) a[i] = read();
for (int i = 0; i < m; i++)
{
int opt = read(), l = read(), r = read();
l++;
if (opt == 1)
{
cb++;
mod[cb] = { l, r };
}
else
{
r++;
ca++;
q[ca] = { ca, l, r, cb };
}
}
len = pow(n, 2.0 / 3);
sort(q, q + ca + 1, cmp);
int nl = 1, nr = 0, now = 0, res = 0;
for (int i = 0; i <= ca; i++)
{
int l = q[i].l, r = q[i].r, t = q[i].t;
while (nl < l) del(a[nl++], res);
while (nl > l) add(a[--nl], res);
while (nr < r) add(a[++nr], res);
while (nr > r) del(a[nr--], res);
while (now < t) change(++now, nl, nr, res);
while (now > t) change(now--, nl, nr, res);
ans[q[i].id] = res;
}
for (int i = 0; i <= ca; i++) printf("%d\n", ans[i]);
return 0;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现