BZOJ4939 [YNOI2016]掉进兔子洞

Code

#include <bits/stdc++.h>

using namespace std;

#define squ(x) ((LL)(x) * (x))
#define debug(...) fprintf(stderr, __VA_ARGS__)

typedef long long LL;
typedef pair<int, int> pii;

int sum = 0, fg = 1; char c = getchar();
for (; !isdigit(c); c = getchar()) if (c == '-') fg = -1;
for (; isdigit(c); c = getchar()) sum = (sum << 3) + (sum << 1) + (c ^ 0x30);
return fg * sum;
}

const int maxn = 1e5 + 10;
const int maxm = 33333 + 10;

bitset<maxn> s[maxm];

int len;
struct node {
int l, r, id;
bool operator < (const node &t) const {
if (l / len == t.l / len) return r < t.r;
return l < t.l;
}
}op[maxn];

int n, m, top, a[maxn], t[maxn], sum[maxn], cnt[maxn];

void solve(int q) {
top = 0;
for (int i = 1; i <= q; i++) {
sum[i] = y1 - x1 + y2 - x2 + y3 - x3 + 3;
op[++top] = (node){x1, y1, i}, op[++top] = (node){x2, y2, i}, op[++top] = (node){x3, y3, i};
s[i].set();
}
sort(op + 1, op + top + 1);

int l = 1, r = 0;
static bitset<maxn> now; now.reset();
memset(cnt, 0, sizeof cnt);
for (int i = 1; i <= top; i++) {
int x = op[i].l, y = op[i].r, id = op[i].id;
while (l > x) {
--l;
now.flip(a[l] + cnt[a[l]]);
++cnt[a[l]];
}
while (r < y) {
++r;
now.flip(a[r] + cnt[a[r]]);
++cnt[a[r]];
}
while (l < x) {
--cnt[a[l]];
now.flip(a[l] + cnt[a[l]]);
++l;
}
while (r > y) {
--cnt[a[r]];
now.flip(a[r] + cnt[a[r]]);
--r;
}
s[id] &= now;
}

for (int i = 1; i <= q; i++) printf("%d\n", sum[i] - s[i].count() * 3);
}

int main() {
#ifdef xunzhen
freopen("bitset.in", "r", stdin);
freopen("bitset.out", "w", stdout);
#endif

for (int i = 1; i <= n; i++) t[i] = a[i] = read();
sort(t + 1, t + n + 1);
for (int i = 1; i <= n; i++) a[i] = lower_bound(t + 1, t + n + 1, a[i]) - t;

while (m) {
int x = min(m, 33334);
solve(x), m -= x;
}

return 0;
}


Summary

posted @ 2018-09-21 13:49  xunzhen  阅读(225)  评论(0编辑  收藏  举报