「hihoCoder 1236」Scores-分块+bitset

给 $n$ 个人,每人有 $5$ 科成绩,给出 $q$ 个成绩查询,输出 $5$ 科都比要查询的这 $5$ 科低的数目。

链接

hihoCoder 1236

题解

一种简单的想法是先排序,对于每一科开一个 $n$ 个 bitset,表示 $< i$ 的人,时间复杂度为 $O(n \log n + \frac {n ^ 2 + qn} {w})$,但是空间复杂度为 $O(n ^ 2)$,会 MLE。

于是我们可以考虑分块,我们开 $m = \sqrt{n}$ 个 bitset,表示前 $i$ 块的人,即 $< im$ 的人,每次查询时,先二分找到对应块,然后对于不在块内的暴力加入,最后对于 $5$ 个 bitset 与起来就好了。

时间复杂度 $O(n \log n + q\sqrt{n} + \frac {qn + n ^ 2} {w})$。

代码

练了练手写 bitset

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/**
* Copyright (c) 2017, xehoth
* All rights reserved.
* 「hihoCoder 1236」Scores 01-12-2017
* 分块 + bitset
* @author xehoth
*/
#include <bits/stdc++.h>
namespace {
inline char read() {
static const int IN_LEN = 100000;
static char buf[IN_LEN], *s, *t;
s == t ? t = (s = buf) + fread(buf, 1, IN_LEN, stdin) : 0;
return s == t ? -1 : *s++;
}
const int OUT_LEN = 100000;
char obuf[OUT_LEN], *oh = obuf;
inline void print(char c) {
oh == obuf + OUT_LEN ? (fwrite(obuf, 1, OUT_LEN, stdout), oh = obuf) : 0;
*oh++ = c;
}
template <typename T>
inline void print(T x) {
static int buf[30], cnt;
if (x == 0) {
print('0');
} else {
x < 0 ? (print('-'), x = -x) : 0;
for (cnt = 0; x; x /= 10) buf[++cnt] = x % 10 | 48;
while (cnt) print((char)buf[cnt--]);
}
}
inline void print(const char *s) {
for (; *s; s++) print(*s);
}
inline void flush() { fwrite(obuf, 1, oh - obuf, stdout); }
struct InputOutputStream {
~InputOutputStream() { flush(); }
template <typename T>
inline InputOutputStream &operator>>(T &x) {
static char c;
static bool iosig;
for (c = read(), iosig = false; !isdigit(c); c = read()) {
if (c == -1) return *this;
c == '-' ? iosig = true : 0;
}
for (x = 0; isdigit(c); c = read()) x = x * 10 + (c ^ '0');
iosig ? x = -x : 0;
}
template <typename T>
inline InputOutputStream &operator<<(const T &x) {
print(x);
return *this;
}
} io;
template <size_t MAXN>
struct BitSet {
static int n;
unsigned long long a[MAXN / 64 + ((MAXN & 63) != 0)];
inline void operator|=(const BitSet &p) {
for (register int i = 0; i < n; i++) a[i] |= p.a[i];
}
inline void operator&=(const BitSet &p) {
for (register int i = 0; i < n; i++) a[i] &= p.a[i];
}
inline void operator^=(const BitSet &p) {
for (register int i = 0; i < n; i++) a[i] ^= p.a[i];
}
inline unsigned int count() {
register unsigned int cnt = 0;
for (register int i = 0; i < n; i++) cnt += __builtin_popcountll(a[i]);
return cnt;
}
inline void reset() { memset(a, 0, sizeof(unsigned long long) * n); }
inline void reset(const int i) { a[i >> 6] &= ~(1ull << (i & 63)); }
inline void set(const int i) { a[i >> 6] |= (1ull << (i & 63)); }
inline void set() { memset(a, 0xff, sizeof(unsigned long long) * n); }
inline void flip() {
for (register int i = 0; i < n; i++) a[i] ^= ULLONG_MAX;
}
inline void flip(const int i) { a[i >> 6] ^= (1ull << (i & 63)); }
inline bool test(const int i) { return (a[i >> 6] >> (i & 63)) & 1; }
inline BitSet operator|(const BitSet &p) const {
BitSet ret = *this;
ret |= p;
return ret;
}
inline BitSet operator&(const BitSet &p) const {
BitSet ret = *this;
ret &= p;
return ret;
}
inline BitSet operator^(const BitSet &p) const {
BitSet ret = *this;
ret ^= p;
return ret;
}
inline int operator[](const int i) const {
return (a[i >> 6] >> (i & 63)) & 1;
}
};
template <size_t size>
int BitSet<size>::n;
const int MAXN = 50000 + 5;
const int MAX_BLOCK = 300 + 5;
BitSet<MAXN> d[5][MAX_BLOCK], ans[5];
int n, m, l[MAX_BLOCK], r[MAX_BLOCK], id[MAXN];
std::pair<int, int> scores[5][MAXN];
inline void init() {
register int blockSize = sqrt(n) * 1.3;
register int blockCnt = n / blockSize + (n % blockSize ? 1 : 0);
l[0] = r[0] = -1;
for (register int i = 1; i <= blockCnt; i++)
l[i] = (i - 1) * blockSize, r[i] = i * blockSize;
r[blockCnt] = n;
for (register int i = 0; i < 5; i++) d[i][0].reset();
for (register int i = 0; i < 5; i++) {
std::sort(scores[i], scores[i] + n);
for (register int j = 1; j <= blockCnt; j++) {
d[i][j] = d[i][j - 1];
for (register int k = l[j]; k < r[j]; k++)
d[i][j].set(scores[i][k].second);
}
}
for (register int i = 1; i <= blockCnt; i++)
for (register int j = l[i]; j < r[i]; j++) id[j] = i;
}
void solve(int u, int x) {
register int t =
std::lower_bound(scores[u], scores[u] + n, std::make_pair(x, n)) -
scores[u];
ans[u] = d[u][id[t] - 1];
for (register int i = l[id[t]]; i < t; i++) ans[u].set(scores[u][i].second);
}
inline void solveCase() {
io >> n >> m;
BitSet<MAXN>::n = n / 64 + ((n & 63) != 0);
for (register int i = 0; i < n; i++)
for (register int j = 0; j < 5; j++)
io >> scores[j][i].first, scores[j][i].second = i;
init();
register int q;
io >> q;
for (register int last = 0, x; q--;) {
for (register int i = 0; i < 5; i++) {
io >> x;
solve(i, x ^ last);
}
for (register int i = 4; i; i--) ans[i - 1] &= ans[i];
last = ans[0].count();
io << last << '\n';
}
}
inline void solve() {
register int T;
io >> T;
while (T--) solveCase();
}
} // namespace
int main() {
// freopen("sample/1.in", "r", stdin);
solve();
return 0;
}

分享到