# bzoj4939: [Ynoi2016]掉进兔子洞 莫队 bitset

32 篇文章 0 订阅

#### bzoj4939: [Ynoi2016]掉进兔子洞

5 2
1 2 2 3 3
1 2 2 3 3 4
1 5 1 5 1 5

3
0

##### HINT

n , m <= 100000 , 1 <= a[i] <= 1000000000

#### 分析

A n s = ∑ l e n − 3 ∑ m a x ( c n t 1 , c n t 2 , c n t 3 ) Ans=\sum len-3\sum max(cnt_1,cnt_2,cnt_3)

#### 代码

#include<bits/stdc++.h>
const int M = 34010, N = 1e5 + 10;
typedef unsigned long long ULL;
int ri() {
char c = getchar(); int x = 0, f = 1; for(;c < '0' || c > '9'; c = getchar()) if(c == '-') f = -1;
for(;c >= '0' && c <= '9'; c = getchar()) x = (x << 1) + (x << 3) - '0' + c; return x * f;
}
ULL All = 0;
int fs[65536], cnt[N], A[N], a[N], b[N], tot = 1, n, m, tp, B;
struct Bit {
ULL s[1564];
void Ini() {for(int i = 0;i <= 1563; ++i) s[i] = All;}
void operator &= (Bit a) {for(int i = 0;i <= 1563; ++i) s[i] &= a.s[i];}
void Add(int p) {s[p >> 6] ^= (ULL)1 << (p & 63);}
int Cnt() {
int r = 0;
for(int i = 0;i <= 1563; ++i)
for(ULL x = s[i]; x; x >>= 16)
r += fs[x & 65535];
return r;
}
}c[M], nw;
struct Que {
int l, r, id;
void in(int i) {l = ri(); r = ri(); id = i;}
}q[M * 3];
void Add(int v, int p) {
if(!~p) cnt[v] += p;
if(~p) cnt[v] += p;
}
bool cmp(Que a, Que b) {
int x = a.l / B, y = b.l / B;
return x == y ? ((x & 1) ? a.r < b.r : a.r > b.r) : x < y;
}
void Solve() {
if(tot > m) return ;
for(int i = 1;i <= M - 10 && tot <= m; ++i, ++tot)
for(int k = 3; k--; )
q[++tp].in(i), A[i] += q[tp].r - q[tp].l + 1;
for(int i = 1;i <= tp / 3; ++i) c[i].Ini();
B = sqrt(tp); std::sort(q + 1, q + tp + 1, cmp);
int L = 1, R = 0;
for(int i = 1;i <= tp; ++i) {
for(;R < q[i].r;) Add(a[++R], 1);
for(;L > q[i].l;) Add(a[--L], 1);
for(;L < q[i].l;) Add(a[L++], -1);
for(;R > q[i].r;) Add(a[R--], -1);
c[q[i].id] &= nw;
}
for(int i = L;i <= R; ++i) Add(a[i], -1);
for(int i = 1;i <= tp / 3; ++i) printf("%d\n", A[i] - c[i].Cnt() * 3), A[i] = 0; tp = 0;
}
int F(int x) {
if(x > b[n]) return n + 1;
int L = 1, R = n, m;
for(;L != R; b[m = L + R >> 1] >= x ? R = m : L = m + 1) ;
return L;
}
int main() {
for(int i = 0;i < 64; ++i) All |= (ULL)1 << i;
for(int i = 1;i < 65536; ++i) fs[i] = fs[i ^ (i&-i)] + 1;
n = ri(); m = ri();
for(int i = 1;i <= n; ++i) b[i] = a[i] = ri();
std::sort(b + 1, b + n + 1);
for(int i = 1;i <= n; ++i) a[i] = F(a[i] + 1) - 1;
Solve(); Solve(); Solve();
return 0;
}

• 0
点赞
• 0
收藏
觉得还不错? 一键收藏
• 0
评论
12-01 2665
12-05 757
10-16 681
08-29 416
11-24 405
11-14 340
08-28 318

1.余额是钱包充值的虚拟货币，按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载，可以购买VIP、付费专栏及课程。