# 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++) {
*/