結果
問題 | No.3016 unordered_mapなるたけ落とすマン |
ユーザー | niuez |
提出日時 | 2020-05-22 12:37:34 |
言語 | C++14 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 21 ms / 1,000 ms |
コード長 | 6,146 bytes |
コンパイル時間 | 1,730 ms |
コンパイル使用メモリ | 173,128 KB |
実行使用メモリ | 13,948 KB |
最終ジャッジ日時 | 2024-10-04 09:45:00 |
合計ジャッジ時間 | 5,212 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
6,820 KB |
testcase_01 | AC | 3 ms
6,816 KB |
testcase_02 | AC | 3 ms
6,820 KB |
testcase_03 | AC | 20 ms
13,828 KB |
testcase_04 | AC | 20 ms
13,948 KB |
testcase_05 | AC | 21 ms
13,824 KB |
testcase_06 | AC | 20 ms
13,812 KB |
testcase_07 | AC | 21 ms
13,832 KB |
testcase_08 | AC | 19 ms
13,804 KB |
testcase_09 | AC | 21 ms
13,856 KB |
testcase_10 | AC | 21 ms
12,320 KB |
testcase_11 | AC | 20 ms
12,024 KB |
testcase_12 | AC | 21 ms
12,100 KB |
testcase_13 | AC | 21 ms
11,924 KB |
testcase_14 | AC | 20 ms
12,072 KB |
testcase_15 | AC | 19 ms
12,052 KB |
testcase_16 | AC | 20 ms
12,040 KB |
testcase_17 | AC | 21 ms
11,876 KB |
testcase_18 | AC | 20 ms
11,844 KB |
testcase_19 | AC | 20 ms
11,928 KB |
testcase_20 | AC | 20 ms
11,888 KB |
testcase_21 | AC | 19 ms
11,876 KB |
testcase_22 | AC | 20 ms
11,736 KB |
testcase_23 | AC | 20 ms
11,848 KB |
testcase_24 | AC | 20 ms
11,824 KB |
testcase_25 | AC | 20 ms
11,792 KB |
testcase_26 | AC | 20 ms
11,748 KB |
testcase_27 | AC | 20 ms
11,672 KB |
testcase_28 | AC | 20 ms
11,552 KB |
testcase_29 | AC | 20 ms
11,808 KB |
testcase_30 | AC | 20 ms
11,536 KB |
testcase_31 | AC | 20 ms
11,768 KB |
testcase_32 | AC | 20 ms
11,484 KB |
testcase_33 | AC | 19 ms
11,584 KB |
testcase_34 | AC | 20 ms
11,412 KB |
testcase_35 | AC | 19 ms
11,368 KB |
testcase_36 | AC | 20 ms
11,624 KB |
testcase_37 | AC | 19 ms
11,348 KB |
testcase_38 | AC | 20 ms
11,472 KB |
testcase_39 | AC | 19 ms
11,332 KB |
testcase_40 | AC | 19 ms
11,312 KB |
testcase_41 | AC | 2 ms
5,248 KB |
testcase_42 | AC | 2 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 | 3 ms
5,248 KB |
testcase_47 | AC | 13 ms
6,704 KB |
testcase_48 | AC | 15 ms
8,568 KB |
testcase_49 | AC | 13 ms
6,856 KB |
testcase_50 | AC | 12 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]; } } 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 << 21); // key = (key << 21) - key - 1; key = key ^ (key >> 24); key = (key + (key << 3)) + (key << 8); // key * 265 key = key ^ (key >> 14); key = (key + (key << 2)) + (key << 4); // key * 21 key = key ^ (key >> 28); key = key + (key << 31); return 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(a); i64 x = 0; if(p) x = p->second; mp.insert(a, x + 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]; } }