-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathfast_hash_table.cpp
More file actions
368 lines (340 loc) · 9.92 KB
/
Copy pathfast_hash_table.cpp
File metadata and controls
368 lines (340 loc) · 9.92 KB
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
//
// 連想配列 (64 bit 整数用)
//
// verified:
// Yosupo Library Checker - Associative Array
// https://judge.yosupo.jp/problem/associative_array
//
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
// Associative Array
template<class Key, class Val, uint32_t N = 20> struct FastMap {
static constexpr uint32_t SIZE = 1u << N;
static constexpr uint32_t MASK = SIZE - 1;
static constexpr uint32_t SHIFT = 64 - N;
static constexpr uint64_t MAGIC = 11995408973635179863ULL;
static constexpr uint32_t get_hash(const Key& k) { return k * MAGIC >> SHIFT; }
// inner values
array<Key, SIZE> key;
array<Val, SIZE> val;
bitset<SIZE> used;
// constructors
FastMap() { clear(); }
void clear() { used.reset(); }
FastMap(const FastMap&) = default;
FastMap& operator = (const FastMap&) = default;
// getters
Val &operator [] (const Key &k) {
auto hash = get_hash(k);
while (true) {
if (!used[hash]) {
used[hash] = 1;
key[hash] = k;
return val[hash] = Val();
}
if (key[hash] == k) return val[hash];
++hash &= MASK;
}
}
const Val &operator [] (const Key &k) const {
auto hash = get_hash(k);
while (true) {
if (!used[hash]) {
used[hash] = 1;
key[hash] = k;
return val[hash] = Val();
}
if (key[hash] == k) return val[hash];
++hash &= MASK;
}
}
Val get(const Key &k) const {
auto hash = get_hash(k);
while (true) {
if (!used[hash]) return Val();
if (key[hash] == k) return val[hash];
++hash &= MASK;
}
}
bool count(const Key &k) const {
auto hash = get_hash(k);
while (true) {
if (!used[hash]) return false;
if (key[hash] == k) return true;
++hash &= MASK;
}
}
};
//------------------------------//
// Fast IO
//------------------------------//
static constexpr int BUF_SIZE = 1 << 17;
struct FastRead {
private:
FILE *stream_;
array<char, BUF_SIZE> buf_;
char *begin_, *end_, *ptr_;
// reader
void skip_space() {
while (*ptr_ <= ' ') ++ptr_;
}
template<int N = 0> void read() {
if (const auto n = end_ - ptr_; n <= N) {
ignore = fread(copy_n(ptr_, n, begin_), 1, BUF_SIZE - n, stream_);
ptr_ = begin_;
}
}
// parser
template<typename T> void parse(T &x) {
common_type_t<T, uint64_t> x2 = 0;
while (true) {
uint64_t v;
memcpy(&v, ptr_, 8);
if ((v -= 0x3030303030303030) & 0x8080808080808080) break;
v = (v * 10 + (v >> 8)) & 0xff00ff00ff00ff;
v = (v * 100 + (v >> 16)) & 0xffff0000ffff;
v = (v * 10000 + (v >> 32)) & 0xffffffff;
x2 = 100000000 * x2 + v;
ptr_ += 8;
}
while (true) {
uint32_t v;
memcpy(&v, ptr_, 4);
if ((v -= 0x30303030) & 0x80808080) break;
v = (v * 10 + (v >> 8)) & 0xff00ff;
v = (v * 100 + (v >> 16)) & 0xffff;
x2 = 10000 * x2 + v;
ptr_ += 4;
break;
}
while (true) {
uint16_t v;
memcpy(&v, ptr_, 2);
if ((v -= 0x3030) & 0x8080) break;
v = (v * 10 + (v >> 8)) & 0xff;
x2 = 100 * x2 + v;
ptr_ += 2;
break;
}
if (' ' < *ptr_) {
x2 *= 10;
x2 += *ptr_++ - '0';
}
++ptr_;
x = static_cast<T>(x2);
}
public:
// constructor
FastRead() : FastRead(stdin) {}
explicit FastRead(const filesystem::path& p) : FastRead(fopen(p.c_str(), "r")) {}
explicit FastRead(FILE *stream)
: stream_(stream), begin_(buf_.data()), end_(begin_ + BUF_SIZE), ptr_(end_) {
read();
}
~FastRead() {
if (stream_ != stdin) fclose(stream_);
}
FastRead(const FastRead&) = delete;
FastRead &operator = (const FastRead&) = delete;
// operators
template<unsigned_integral T> void operator () (T &x) {
skip_space();
read<64>();
parse(x);
}
template<signed_integral T> void operator () (T &x) {
skip_space();
read<64>();
make_unsigned_t<T> u;
if (*ptr_ == '-') {
++ptr_;
parse(u);
u = -u;
} else {
parse(u);
}
x = u;
}
void operator () (char &x) {
skip_space();
read<64>();
x = *ptr_;
++ptr_;
}
void operator () (string &x) {
x = "";
skip_space();
read<64>();
while (*ptr_ > ' ' && *ptr_ != '\0') {
x.push_back(*ptr_);
++ptr_;
}
++ptr_;
}
template<class... Ts> requires(sizeof...(Ts) != 1) void operator () (Ts&... xs) {
((*this)(xs), ...);
}
template<class T> FastRead& operator >> (T &x) { (*this)(x); return *this; }
};
class FastWrite {
private:
FILE *stream_;
array<char, BUF_SIZE> buf_;
char *begin_, *end_, *ptr_;
// preparation
template <class T> static constexpr int DIGITS = numeric_limits<T>::digits10 + 1;
template <class T> static constexpr auto POW10 = [] {
array<T, DIGITS<T>> ret;
ret[0] = 1;
for (int i = 1; i < DIGITS<T>; ++i) {
ret[i] = 10 * ret[i - 1];
}
return ret;
} ();
static constexpr auto LUT = [] {
array<char, 40000> res;
char* p = res.data();
char a = '0', b = '0', c = '0', d = '0';
do {
*p++ = a, *p++ = b, *p++ = c, *p++ = d;
} while (d++ < '9'
|| (d = '0', c++ < '9'
|| (c = '0', b++ < '9'
|| (b = '0', a++ < '9'))));
return res;
} ();
// flush
template<int N = BUF_SIZE> void flush() {
if (end_ - ptr_ <= N) {
fwrite(begin_, 1, ptr_ - begin_, stream_);
ptr_ = begin_;
}
}
// writer
template<int N = 4> void le4(uint64_t x) {
if constexpr (1 < N) {
if (x < POW10<uint64_t>[N - 1]) {
le4<N - 1>(x);
return;
}
}
ptr_ = copy_n(&LUT[x * 4 + (4 - N)], N, ptr_);
}
template<int N> void w4(uint64_t x) {
if constexpr (0 < N) {
ptr_ = copy_n(&LUT[x / POW10<uint64_t>[N - 4] * 4], 4, ptr_);
w4<N - 4>(x % POW10<uint64_t>[N - 4]);
}
}
template<int N> void write(uint64_t x) {
if constexpr (N < DIGITS<uint64_t>) {
if (POW10<uint64_t>[N] <= x) {
write<N + 4>(x);
return;
}
}
le4(x / POW10<uint64_t>[N - 4]);
w4<N - 4>(x % POW10<uint64_t>[N - 4]);
}
template<typename T> void write(T x) {
write<4>(x);
}
void write(__uint128_t x) {
if (x < POW10<__uint128_t>[16]) {
write(static_cast<uint64_t>(x));
} else if (x < POW10<__uint128_t>[32]) {
write(static_cast<uint64_t>(x / POW10<__uint128_t>[16]));
w4<16>(static_cast<uint64_t>(x % POW10<__uint128_t>[16]));
} else {
write(static_cast<uint64_t>(x / POW10<__uint128_t>[32]));
x %= POW10<__uint128_t>[32];
w4<16>(static_cast<uint64_t>(x / POW10<__uint128_t>[16]));
w4<16>(static_cast<uint64_t>(x % POW10<__uint128_t>[16]));
}
}
public:
// constructor
FastWrite() : FastWrite(stdout) {}
explicit FastWrite(const filesystem::path& p) : FastWrite(fopen(p.c_str(), "w")) {}
explicit FastWrite(FILE* stream)
: stream_(stream), begin_(buf_.data()), end_(begin_ + BUF_SIZE), ptr_(begin_) {}
~FastWrite() {
flush();
if (stream_ != stdout) { fclose(stream_); }
}
FastWrite(const FastWrite&) = delete;
FastWrite& operator = (const FastWrite&) = delete;
// operators
template<unsigned_integral T> void operator () (T x) {
flush<DIGITS<T>>();
write(x);
}
template<signed_integral T> void operator () (T x) {
flush<1 + DIGITS<T>>();
using U = make_unsigned_t<T>;
const U u = x;
if (x < 0) {
*ptr_++ = '-';
write(static_cast<U>(-u));
} else {
write(u);
}
}
void operator () (char c) {
flush<1>();
*ptr_++ = c;
}
void operator () (string_view s) {
while (!s.empty()) {
flush<0>();
const auto n = min(ssize(s), end_ - ptr_);
if (n == BUF_SIZE) {
fwrite(s.data(), 1, BUF_SIZE, stream_);
} else {
ptr_ = copy_n(s.data(), n, ptr_);
}
s.remove_prefix(n);
}
flush<0>();
}
template <char End = '\n', char Sep = ' ', class T, class... Ts>
void ln(T&& x, Ts&&... xs) {
(*this)(std::forward<T>(x));
if constexpr (sizeof...(Ts) == 0) {
*ptr_++ = End;
} else {
*ptr_++ = Sep;
ln<End, Sep>(std::forward<Ts>(xs)...);
}
}
template<class T> FastWrite& operator << (T x) { (*this)(x); return *this; }
};
//------------------------------//
// Examples
//------------------------------//
// Yosupo Library Checker - Associative Array
void Yosupo_Associative_Array() {
FastRead Read;
FastWrite Write;
FastMap<long long, long long> mp;
int Q;
Read(Q);
for (int q = 0; q < Q; q++) {
int typ;
Read(typ);
if (typ == 0) {
long long k, v;
Read(k, v);
mp[k] = v;
} else {
long long k;
Read(k);
Write.ln(mp[k]);
}
}
}
int main() {
Yosupo_Associative_Array();
}