結果
問題 | No.8016 unordered_mapなるたけ落とすマン |
ユーザー | niuez |
提出日時 | 2020-05-22 12:43:48 |
言語 | C++14 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 20 ms / 1,000 ms |
コード長 | 6,345 bytes |
コンパイル時間 | 1,508 ms |
コンパイル使用メモリ | 173,276 KB |
実行使用メモリ | 12,272 KB |
最終ジャッジ日時 | 2024-10-04 09:58:36 |
合計ジャッジ時間 | 4,875 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
5,248 KB |
testcase_01 | AC | 2 ms
5,248 KB |
testcase_02 | AC | 2 ms
5,248 KB |
testcase_03 | AC | 18 ms
11,944 KB |
testcase_04 | AC | 19 ms
12,208 KB |
testcase_05 | AC | 20 ms
12,160 KB |
testcase_06 | AC | 19 ms
12,272 KB |
testcase_07 | AC | 19 ms
12,132 KB |
testcase_08 | AC | 19 ms
12,116 KB |
testcase_09 | AC | 19 ms
12,100 KB |
testcase_10 | AC | 19 ms
12,192 KB |
testcase_11 | AC | 18 ms
12,156 KB |
testcase_12 | AC | 18 ms
12,100 KB |
testcase_13 | AC | 17 ms
12,080 KB |
testcase_14 | AC | 17 ms
12,072 KB |
testcase_15 | AC | 17 ms
12,056 KB |
testcase_16 | AC | 18 ms
11,912 KB |
testcase_17 | AC | 18 ms
11,980 KB |
testcase_18 | AC | 18 ms
11,972 KB |
testcase_19 | AC | 17 ms
11,792 KB |
testcase_20 | AC | 17 ms
11,884 KB |
testcase_21 | AC | 18 ms
11,876 KB |
testcase_22 | AC | 17 ms
11,860 KB |
testcase_23 | AC | 17 ms
11,720 KB |
testcase_24 | AC | 16 ms
11,696 KB |
testcase_25 | AC | 16 ms
11,792 KB |
testcase_26 | AC | 17 ms
11,752 KB |
testcase_27 | AC | 17 ms
11,572 KB |
testcase_28 | AC | 17 ms
11,684 KB |
testcase_29 | AC | 17 ms
11,676 KB |
testcase_30 | AC | 18 ms
11,660 KB |
testcase_31 | AC | 16 ms
11,640 KB |
testcase_32 | AC | 17 ms
11,608 KB |
testcase_33 | AC | 17 ms
11,584 KB |
testcase_34 | AC | 16 ms
11,540 KB |
testcase_35 | AC | 17 ms
11,500 KB |
testcase_36 | AC | 17 ms
11,452 KB |
testcase_37 | AC | 16 ms
11,352 KB |
testcase_38 | AC | 17 ms
11,472 KB |
testcase_39 | AC | 16 ms
11,332 KB |
testcase_40 | AC | 16 ms
11,444 KB |
testcase_41 | AC | 2 ms
5,248 KB |
testcase_42 | AC | 1 ms
5,248 KB |
testcase_43 | AC | 2 ms
5,248 KB |
testcase_44 | AC | 3 ms
5,248 KB |
testcase_45 | AC | 3 ms
5,248 KB |
testcase_46 | AC | 2 ms
5,248 KB |
testcase_47 | AC | 12 ms
6,580 KB |
testcase_48 | AC | 14 ms
8,436 KB |
testcase_49 | AC | 12 ms
6,856 KB |
testcase_50 | AC | 10 ms
7,040 KB |
ソースコード
#include <vector> #include <cstdint> #include <set> template<class Key, class Value, class Hash> struct hash_map { using size_type = std::size_t; using key_type = Key; using value_type = Value; using pair_type = std::pair<key_type, value_type>; enum class State: std::uint8_t { INACTIVE, ACTIVE, FILLED }; Hash hashf; std::vector<State> st; std::vector<pair_type> bck; size_type mask; size_type prode; size_type sz; hash_map(): mask(0), prode(-1), sz(0) { } size_type find_empty(const key_type& key) { size_type h = hashf(key); for(size_type delta = 0;;delta++) { size_type i = (h + delta) & mask; if(st[i] != State::FILLED) { if(prode < delta) prode = delta; return i; } } } size_type find_filled(const key_type& key) { if(sz == 0) return size_type(-1); size_type h = hashf(key); for(size_type delta = 0; delta <= prode; delta++) { size_type i = (h + delta) & mask; if(st[i] == State::FILLED) { if(bck[i].first == key) { return i; } } else if(st[i] == State::INACTIVE) { return size_type(-1); } } return size_type(-1); } size_type find_or_allocate(const key_type& key) { size_type h = hashf(key); bool hole = false; size_type delta = 0; for(; delta <= prode; delta++) { size_type i = (h + delta) & mask; if(st[i] == State::FILLED) { if(bck[i].first == key) return i; } else if(st[i] == State::INACTIVE) return i; else { hole = true; } } if(!hole) return size_type(-1); for(; ; delta++) { size_type i = (h + delta) & mask; if(st[i] != State::FILLED) { prode = delta; return i; } } } void reserve(int next_cnt) { size_type required_cnt = next_cnt + (next_cnt >> 1) + 1; if(required_cnt <= bck.size()) { return; } next_cnt = 4; while(next_cnt < required_cnt) next_cnt <<= 1; std::vector<State> old_st(next_cnt, State::INACTIVE); std::vector<pair_type> old_bck(next_cnt); std::swap(old_st, st); std::swap(old_bck, bck); mask = next_cnt - 1; sz = 0; prode = -1; for(size_type pos = 0; pos < old_bck.size(); pos++) { if(old_st[pos] == State::FILLED) { size_type i = find_empty(old_bck[pos].first); st[i] = State::FILLED; bck[i] = std::move(old_bck[pos]); sz += 1; } } } void insert(const key_type& key, const value_type& val) { reserve(sz + 1); size_type i = find_or_allocate(key); if(st[i] != State::FILLED) { st[i] = State::FILLED; bck[i] = pair_type(key, val); sz++; } else { bck[i] = pair_type(key, val); } } bool erase(const key_type& key) { size_type i = find_filled(key); if(i == size_type(-1)) { return false; } else { st[i] = State::ACTIVE; bck[i].~pair_type(); sz--; return true; } } pair_type* get(const key_type& key) { size_type i = find_filled(key); if(i == size_type(-1)) { return nullptr; } else { return &bck[i]; } } pair_type* get_or_insert(const key_type& key, const value_type val) { reserve(sz + 1); size_type i = find_or_allocate(key); if(st[i] != State::FILLED) { st[i] = State::FILLED; bck[i] = pair_type(key, val); sz++; } return &bck[i]; } template<class Func> void search_all(Func func) { for(size_type i = 0; i < bck.size(); i++) { if(st[i] == State::FILLED) { size_type res = func(bck[i]); if(res & 0b10) { st[i] = State::ACTIVE; bck[i].~pair_type(); sz--; } if(res & 0b01) { return; } } } } }; struct Hashu32 { std::uint32_t operator()(std::uint32_t key) { int c2=0x27d4eb2d; // a prime or an odd constant key = (key ^ 61) ^ (key >> 16); key = key + (key << 3); key = key ^ (key >> 4); key = key * c2; key = key ^ (key >> 15); return key; } }; struct Hashu64 { std::size_t operator()(std::uint64_t key) { key = (~key) + (key << 18); // key = (key << 18) - key - 1; key = key ^ (key >> 31); key = key * 21; // key = (key + (key << 2)) + (key << 4); key = key ^ (key >> 11); key = key + (key << 6); key = key ^ (key >> 22); return (int) key; } }; #include <bits/stdc++.h> using namespace std; using i64 = long long; #define rep(i,s,e) for(int (i) = (s);(i) < (e);(i)++) #define all(x) x.begin(),x.end() #include <unistd.h> namespace niu { struct fastin { static const int bufsize = 1 << 24; char buf[bufsize]; char* iter; fastin() { iter = buf; for(int t = 0, k; (k = read(STDIN_FILENO, buf + t, sizeof(buf)) - t) > 0; t += k); } fastin& operator>>(long long& num) { num = 0; bool neg = false; while(*iter < '+') iter++; if(*iter == '-') { neg = true; iter++; } else if(*iter == '+') iter++; while(*iter >= '0') num = 10 * num + *(iter++) - '0'; if(neg) num = -num; return *this; } } fin; struct fastout { static const int bufsize = 1 << 24; char buf[bufsize]; char* iter; fastout() { iter = buf; } ~fastout() { for(int t = 0, k; (k = write(STDOUT_FILENO, buf + t, iter - buf - t)) > 0; t += k); } fastout& operator<<(int num) { static char tmp[20]; if(num == 0) { *(iter++) = '0'; return *this; } if(num < 0) { *(iter++) = '-'; num = -num; } int i = 0; while(num) { tmp[i++] = num % 10; num /= 10; } while(i--) { *(iter++) = tmp[i] + '0'; } return *this; } fastout& operator<<(char c) { *(iter++) = c; return *this; } } fout; } int main(){ cin.tie(nullptr); std::ios::sync_with_stdio(false); i64 N, M; niu::fin >> N >> M; hash_map<std::uint64_t, int, Hashu64> mp; rep(i,0,N){ i64 a; niu::fin >> a; auto p = mp.get_or_insert(a, 0); p->second += 1; } rep(i,0,M) { i64 a; niu::fin >> a; auto p = mp.get(a); int x = 0; if(p) x = p->second; niu::fout << x << " \n"[i + 1 == M]; } }