[Ynoi2016] 掉进兔子洞

[Ynoi2016] 掉进兔子洞

来补一发这个题,比较经典的莫队配合bitset

求三个区间中同一个数的最小出现次数,那么考虑用莫队维护bitset的办法,具体来说先离散化,然后从前往后填当前区间里的数。求出三个bitset的交即可。

这题还有一个值得注意的小trick,由于直接做空间开不下,所以可以分多次做,重复利用空间,然后就解决了空间的问题。

没什么好卡的,随便写一下就过了。

#include <cstdio>
#include <iostream>
#include <bitset>
#include <algorithm>
#define LL long long
using namespace std;
template <typename T>
inline void read(T &x) {
x = 0; int f = 0; char ch = getchar();
for(; !isdigit(ch); ch = getchar()) if(ch == '-') f = 1;
for(; isdigit(ch); ch = getchar()) x = (x << 3) + (x << 1) + (ch ^ 48);
if(f) x = ~x + 1;
}
const int T = 34000;
const int N = 1e5 + 10;
const int B = 330;
struct Query {
int l, r, id;
}q[N << 2];
inline bool cmp(Query x, Query y) {
return (x.l / B == y.l / B) ?
(((x.l / B) & 1) ? (x.r > y.r) : (x.r < y.r)) :
(x.l < y.l);
}
bitset<N> bit[T + 10], cur;
int len[N];
int a[N], cnt[N];
inline void add(int x) {
++cnt[x];
cur[x + cnt[x] - 1] = 1;
}
inline void del(int x) {
cur[x + cnt[x] - 1] = 0;
--cnt[x];
}
int n;
void solve(int m) {
for(int i = 1; i <= m; ++i) {
bit[i].set();
int id1 = i * 3 - 2, id2 = i * 3 - 1, id3 = i * 3;
read(q[id1].l), read(q[id1].r), q[id1].id = i;
read(q[id2].l), read(q[id2].r), q[id2].id = i;
read(q[id3].l), read(q[id3].r), q[id3].id = i;
len[i] = q[id1].r - q[id1].l + 1 + q[id2].r - q[id2].l + 1 + q[id3].r - q[id3].l + 1;
}
int tot = m * 3;
sort(q + 1, q + tot + 1, cmp);
cur.reset();
for(int i = 1; i <= n; ++i) cnt[i] = 0;
for(int i = 1, l = 1, r = 0; i <= tot; ++i) {
while(r < q[i].r) add(a[++r]);
while(l > q[i].l) add(a[--l]);
while(r > q[i].r) del(a[r--]);
while(l < q[i].l) del(a[l++]);
bit[q[i].id] &= cur;
}
for(int i = 1; i <= m; ++i) printf("%d\n",len[i] - 3 * (int)bit[i].count());
}
int m, b[N], tot;
int main() {
read(n), read(m);
for(int i = 1; i <= n; ++i) read(a[i]), b[++tot] = a[i];
sort(b + 1, b + n + 1);
for(int i = 1; i <= n; ++i)
a[i] = lower_bound(b + 1, b + n + 1, a[i]) - b;
while(m) {
if(m < T) solve(m), m = 0;
else solve(T), m -= T;
}
}
posted @   DCH233  阅读(41)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
点击右上角即可分享
微信分享提示