結果

問題 No.8016 unordered_mapなるたけ落とすマン
ユーザー tonegawa
提出日時 2020-11-24 18:00:23
言語 C++17(gcc12)
(gcc 12.3.0 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 3,927 bytes
コンパイル時間 4,064 ms
コンパイル使用メモリ 155,040 KB
最終ジャッジ日時 2025-01-16 05:24:25
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 4 WA * 44
権限があれば一括ダウンロードができます

ソースコード

diff #

#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>
#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;


template<typename Key>
struct hashset{
  float load_factor = 0.8;
  int table_size = 16;
  int mod = 15;
  Key null_key;
  vector<Key> table;
  vector<Key> data;
  function<uint32_t(Key)> f = std::hash<uint32_t>();
  hashset():
    null_key(std::numeric_limits<Key>::max()),
    table(table_size, null_key){}
  int size(){
    return data.size();
  }
  void expand_table(){
    table_size *= 2;
    mod = table_size - 1;
    table.resize(table_size);
    fill(table.begin(), table.end(), null_key);
    for(auto v:data) insert(v, false);
  }
  void insert(Key k, bool g=true){
    assert(k!=null_key);
    uint32_t h = f(k) & mod;
    while(true){
      if(table[h]!=null_key){
        if(table[h]==k) return;
        h = (h + 1) & mod;
      }else{
        if(g) data.push_back(k);
        table[h] = k;
        if(float(data.size())>load_factor*float(table_size)){
          expand_table();
        }
        return;
      }
    }
  }
  bool contain(Key k){
    unsigned h = f(k) & mod;
    while(true){
      if(table[h]==null_key) return false;
      if(table[h]==k) return true;
      h = (h + 1) & mod;
    }
  }
};

template<typename Key, typename Val>
struct hashmap{
  float load_factor = 0.8;
  int table_size = 16;
  int mod = 15;
  Key null_key;
  vector<Key> key_table;
  vector<Val> val_table;
  vector<pair<Key, Val>> data;
  function<uint32_t(Key)> f = std::hash<uint32_t>();

  hashmap():
    null_key(std::numeric_limits<Key>::max()),
    key_table(table_size, null_key), val_table(table_size){}
  int size(){
    return data.size();
  }
  void expand_table(){
    table_size *= 2;
    mod = table_size - 1;
    key_table.resize(table_size);
    val_table.resize(table_size);
    fill(key_table.begin(), key_table.end(), null_key);
    for(auto d:data) emplace(d.first, d.second, false);
  }
  void emplace(Key k, Val v, bool g=true){
    assert(k!=null_key);
    uint32_t h = f(k) & mod;
    while(true){
      if(key_table[h]!=null_key){
        if(key_table[h]==k) return;
        h = (h + 1) & mod;
      }else{
        if(g) data.push_back({k, v});
        key_table[h] = k;
        val_table[h] = v;
        if(float(data.size())>load_factor*float(table_size)){
          expand_table();
        }
        return;
      }
    }
  }
  bool contain(Key k){
    assert(k!=null_key);
    unsigned h = f(k) & mod;
    while(true){
      if(key_table[h]==null_key) return false;
      if(key_table[h]==k) return true;
      h = (h + 1) & mod;
    }
  }
  Val at(Key k){
    assert(k!=null_key);
    unsigned h = f(k) & mod;
    while(true){
      assert(key_table[h]!=null_key);
      if(key_table[h]==k) return val_table[h];
      h = (h + 1) & mod;
    }
  }
  Val &at_ref(Key k){
    assert(k!=null_key);
    unsigned h = f(k) & mod;
    while(true){
      assert(key_table[h]!=null_key);
      if(key_table[h]==k) return val_table[h];
      h = (h + 1) & mod;
    }
  }
};

int main(){
  ll n, m;scanf("%lld %lld", &n, &m);
  hashmap<ll, ll> mp;
  for(int i=0;i<n;i++){
    ll a;scanf("%lld", &a);
    if(mp.contain(a)) mp.at_ref(a)++;
    else mp.emplace(a, 1);
  }
  for(int i=0;i<m;i++){
    ll a;scanf("%lld", &a);
    ll ans;
    if(mp.contain(a)) ans = mp.at(a);
    else ans = 0;
    if(i==m-1) printf("%lld\n", ans);
    else printf("%lld ", ans);
  }
}
0