# BZOJ4939: [Ynoi2016]掉进兔子洞(莫队 bitset)

## Sol

```#include<cstdio>
#include<algorithm>
#include<cstring>
#include<iostream>
#include<cmath>
#include<bitset>
#define LL long long
// #define int long long
using namespace std;
const int MAXN = 1e5 + 10, B = 25000;
char c = getchar(); int x = 0, f = 1;
while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
return x * f;
}
int N, M;
int a[MAXN], date[MAXN], L[MAXN][5], R[MAXN][5], belong[MAXN], base, cnt = 0, flag[MAXN], tim[MAXN], ans[MAXN];
struct Query {
int l, r, opt, id;
bool operator < (const Query &rhs) const {
return belong[l] == belong[rhs.l] ? belong[r] < belong[rhs.r] : belong[l] < belong[rhs.l];
}
}Q[MAXN * 3 + 1];
bitset<MAXN> bit[B + 50], now;
tim[a[x]]++;
now.set(a[x] + tim[a[x]] - 1);
}
void Delet(int x) {
now.reset(a[x] + tim[a[x]] - 1);
tim[a[x]]--;
}
void solve(int ll, int rr) {
memset(flag, 0, sizeof(flag));
memset(tim, 0, sizeof(tim));
memset(ans, 0, sizeof(ans));
now.reset();
for(int i = 0; i <= B; i++) bit[i].reset();
cnt = 0;
for(int ti = ll; ti <= rr; ti++) {
for(int i = 1; i <= 3; i++)
Q[++cnt] = (Query){L[ti][i], R[ti][i], i, ti - ll + 1},
ans[ti - ll + 1] += (R[ti][i] - L[ti][i] + 1);
}
sort(Q + 1, Q + cnt + 1);
int l = 1, r = 0;
for(int i = 1; i <= cnt; i++) {
while(l < Q[i].l) Delet(l++);
while(r > Q[i].r) Delet(r--);
if(!flag[Q[i].id])
bit[Q[i].id] = now, flag[Q[i].id] = 1;
else
bit[Q[i].id] &= now;
}
// sort(Q + 1, Q + cnt + 1, comp);
for(int i = ll; i <= rr; i++)
printf("%d\n", ans[i - ll + 1] - bit[i - ll + 1].count() * 3);
}
main() {
///    freopen("xp1.in", "r", stdin);
//    freopen("ans.out", "w", stdout);
for(int i = 1; i <= N; i++) a[i] = read(), date[i] = a[i], belong[i] = (i - 1) / base + 1;
sort(date + 1, date + N + 1);
for(int i = 1; i <= N; i++) a[i] = lower_bound(date + 1, date + N + 1, a[i]) - date;
for(int i = 1; i <= M; i++) {
for(int j = 1; j <= 3; j++) L[i][j] = read(), R[i][j] = read();
}
for(int i = 1; i <= N; i += B + 1)
solve(i, min(i + B, M));

return 0;
}
/*
*/```

1811 篇文章121 人订阅

0 条评论

## 相关文章

10210

### concrrent类下 BlockingDeque 下 自己实现代码编写

java6增加了两种容器类型，Deque和BlockingDeque,它们分别对Queue和BlockingQueue进行了扩展。 Deque是一个双端队...

8420

66260

18920

350110

9710

57440

### (转)Java--栈与队列

Java中栈与队列相比集合来说不是很常用的数据结构,因此经常被忽略.个人觉得还是有必要掌握下,以备不时之需. Java中实际上提供了java.util.Stac...

63430

11410