結果
問題 | No.8016 unordered_mapなるたけ落とすマン |
ユーザー |
![]() |
提出日時 | 2020-11-25 03:38:19 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 58 ms / 1,000 ms |
コード長 | 3,793 bytes |
コンパイル時間 | 1,445 ms |
コンパイル使用メモリ | 129,860 KB |
最終ジャッジ日時 | 2025-01-16 05:35:24 |
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 48 |
コンパイルメッセージ
main.cpp: In function ‘int main()’: main.cpp:150:16: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result] 150 | ll n, m;scanf("%lld %lld", &n, &m); | ~~~~~^~~~~~~~~~~~~~~~~~~~~ main.cpp:153:15: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result] 153 | ll a;scanf("%lld", &a); | ~~~~~^~~~~~~~~~~~ main.cpp:158:15: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result] 158 | ll a;scanf("%lld", &a); | ~~~~~^~~~~~~~~~~~
ソースコード
#include <iostream> #include <string> #include <vector> #include <array> #include <stack> #include <queue> #include <deque> #include <algorithm> #include <set> #include <map> #include <bitset> #include <cmath> #include <functional> #include <cassert> #include <iomanip> #include <numeric> #include <random> #define vll vector<ll> #define vvvl vector<vvl> #define vvl vector<vector<ll>> #define VV(a, b, c, d) vector<vector<d>>(a, vector<d>(b, c)) #define VVV(a, b, c, d) vector<vvl>(a, vvl(b, vll (c, d))); #define re(c, b) for(ll c=0;c<b;c++) #define all(obj) (obj).begin(), (obj).end() typedef long long int ll; typedef long double ld; using namespace std; #include <type_traits> #include <typeinfo> template<typename Key, typename Val> struct hashmap{ private: float load_factor = 0.8; int table_size = 16; int mod = 15; Key null_key; uint32_t mask; int valid_size; vector<int> idx_table; vector<pair<Key, Val>> data; function<uint32_t(Key)> f = std::hash<uint32_t>(); void expand_table(){ table_size *= 2; mod = table_size - 1; idx_table.resize(table_size); fill(idx_table.begin(), idx_table.end(), -1); for(int i=0;i<data.size();i++){ if(data[i].first==null_key) continue; inner_emplace(i); } } void inner_emplace(int idx){ Key k = data[idx].first; uint32_t h = f(k^mask) & mod; while(true){ if(idx_table[h]==-1){ idx_table[h] = idx; return; } h = (h + 1) & mod; } } public: hashmap(): null_key(std::numeric_limits<Key>::max()), mask(random_device()()), valid_size(0), idx_table(table_size, -1){} int size(){ return valid_size; } void emplace(Key k, Val v){ assert(k!=null_key); uint32_t h = f(k^mask) & mod; while(true){ int idx = idx_table[h]; if(idx!=-1){ if(data[idx].first==k) return; h = (h + 1) & mod; }else{ data.push_back({k, v}); idx_table[h] = int(data.size()) - 1; valid_size++; if(float(data.size())>load_factor*float(table_size)){ expand_table(); } return; } } } void overwrite(Key k, Val v){ assert(k!=null_key); uint32_t h = f(k^mask) & mod; while(true){ int idx = idx_table[h]; if(idx!=-1){ if(data[idx].first==k){ data[idx].second = v; return; } h = (h + 1) & mod; }else{ data.push_back({k, v}); idx_table[h] = int(data.size()) - 1; valid_size++; if(float(data.size())>load_factor*float(table_size)){ expand_table(); } return; } } } void erase(Key k){ assert(k!=null_key); uint32_t h = f(k^mask) & mod; while(true){ int idx = idx_table[h]; if(idx==-1) return; if(data[idx].first==k){ data[idx].first = null_key; valid_size--; return; } h = (h + 1) & mod; } } bool contain(Key k){ assert(k!=null_key); uint32_t h = f(k^mask) & mod; while(true){ if(idx_table[h]==-1) return false; if(data[idx_table[h]].first==k) return true; h = (h + 1) & mod; } } Val at(Key k){ assert(k!=null_key); uint32_t h = f(k^mask) & mod; while(true){ int idx = idx_table[h]; assert(idx!=-1); if(data[idx].first==k) return data[idx].second; h = (h + 1) & mod; } } }; int main(){ ll n, m;scanf("%lld %lld", &n, &m); hashmap<ll, int> mp; for(int i=0;i<n;i++){ ll a;scanf("%lld", &a); if(mp.contain(a)) mp.overwrite(a, mp.at(a)+1); else mp.emplace(a, 1); } for(int i=0;i<m;i++){ ll a;scanf("%lld", &a); int ans = (mp.contain(a)?mp.at(a):0); if(i==m-1) printf("%d\n", ans); else printf("%d ", ans); } }