結果

問題 No.3016 unordered_mapなるたけ落とすマン
ユーザー tonegawatonegawa
提出日時 2020-11-25 03:06:36
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 54 ms / 1,000 ms
コード長 4,857 bytes
コンパイル時間 1,403 ms
コンパイル使用メモリ 136,128 KB
実行使用メモリ 6,320 KB
最終ジャッジ日時 2023-10-01 01:36:02
合計ジャッジ時間 6,730 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 42 ms
6,136 KB
testcase_04 AC 50 ms
6,160 KB
testcase_05 AC 50 ms
6,080 KB
testcase_06 AC 54 ms
6,132 KB
testcase_07 AC 49 ms
6,288 KB
testcase_08 AC 49 ms
6,092 KB
testcase_09 AC 49 ms
5,984 KB
testcase_10 AC 49 ms
6,136 KB
testcase_11 AC 49 ms
6,040 KB
testcase_12 AC 47 ms
6,144 KB
testcase_13 AC 47 ms
6,140 KB
testcase_14 AC 49 ms
6,172 KB
testcase_15 AC 47 ms
6,140 KB
testcase_16 AC 48 ms
6,036 KB
testcase_17 AC 47 ms
6,036 KB
testcase_18 AC 46 ms
6,160 KB
testcase_19 AC 47 ms
6,140 KB
testcase_20 AC 47 ms
6,084 KB
testcase_21 AC 46 ms
5,984 KB
testcase_22 AC 48 ms
6,040 KB
testcase_23 AC 46 ms
6,092 KB
testcase_24 AC 45 ms
5,980 KB
testcase_25 AC 45 ms
6,096 KB
testcase_26 AC 44 ms
6,320 KB
testcase_27 AC 45 ms
6,140 KB
testcase_28 AC 46 ms
6,088 KB
testcase_29 AC 44 ms
6,096 KB
testcase_30 AC 44 ms
6,148 KB
testcase_31 AC 44 ms
6,164 KB
testcase_32 AC 44 ms
5,976 KB
testcase_33 AC 44 ms
6,096 KB
testcase_34 AC 45 ms
6,100 KB
testcase_35 AC 44 ms
6,320 KB
testcase_36 AC 44 ms
6,092 KB
testcase_37 AC 44 ms
6,040 KB
testcase_38 AC 43 ms
6,036 KB
testcase_39 AC 43 ms
6,096 KB
testcase_40 AC 42 ms
6,136 KB
testcase_41 AC 1 ms
4,376 KB
testcase_42 AC 2 ms
4,376 KB
testcase_43 AC 1 ms
4,376 KB
testcase_44 AC 4 ms
4,376 KB
testcase_45 AC 4 ms
4,376 KB
testcase_46 AC 4 ms
4,376 KB
testcase_47 AC 34 ms
4,376 KB
testcase_48 AC 37 ms
4,524 KB
testcase_49 AC 37 ms
4,416 KB
testcase_50 AC 41 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

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

template<typename Key>
struct hashset{
private:
  float load_factor = 0.8;
  int table_size = 16;
  int mod = 15;
  int valid_data;
  const Key null_key;
  uint32_t mask;
  vector<int> idx_table;
  vector<Key> 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]!=null_key) inner_insert(i);
  }
  void inner_insert(int idx){
    uint32_t h = f(data[idx]^mask) & mod;
    while(true){
      if(idx_table[h]==-1){
        idx_table[h] = idx;
        return;
      }
      h = (h + 1) & mod;
    }
  }
public:
  hashset():
     valid_data(0),null_key(std::numeric_limits<Key>::max()),
     mask(random_device()()),idx_table(table_size, -1){}
  int size(){
    return valid_data;
  }
  void erase(Key k){
    assert(k!=null_key);
    uint32_t h = f(k^mask) & mod;
    while(true){
      if(idx_table[h]==-1) return;
      if(data[idx_table[h]]==k){
        data[idx_table[h]] = null_key;
        valid_data--;
        return;
      }
      h = (h + 1) & mod;
    }
  }
  void insert(Key k){
    assert(k!=null_key);
    uint32_t h = f(k^mask) & mod;
    while(true){
      if(idx_table[h]!=-1){
        if(data[idx_table[h]]==k) return;
        h = (h + 1) & mod;
      }else{
        data.push_back(k);
        idx_table[h] = int(data.size())-1;
        valid_data++;
        if(float(data.size())>load_factor*float(table_size)){
          expand_table();
        }
        return;
      }
    }
  }
  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]]==k) return true;
      h = (h + 1) & mod;
    }
  }
};

template<typename Key, typename Cnt=int>
struct hashmultiset{
private:
  float load_factor = 0.8;
  int table_size = 16;
  int mod = 15;
  const Key null_key;
  uint32_t mask;
  vector<int> idx_table;
  vector<pair<Key, Cnt>> 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_insert(i);
    }
  }
  void inner_insert(int idx){
    uint32_t h = f(data[idx].first^mask) & mod;
    while(true){
      if(idx_table[h]==-1){
        idx_table[h] = idx;
        return;
      }
      h = (h + 1) & mod;
    }
  }
public:
  hashmultiset():
    null_key(std::numeric_limits<Key>::max()),
    mask(random_device()()),idx_table(table_size, -1){}
  /*
  int size(){
    return data.size();
  }
  */
  void insert(Key k, Cnt x=1){
    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 += x;
          return;
        }
        h = (h + 1) & mod;
      }else{
        data.push_back({k, x});
        idx_table[h] = int(data.size()) - 1;
        if(float(data.size())>load_factor*float(table_size)){
          expand_table();
        }
        return;
      }
    }
  }
  void erase(Key k, Cnt x=std::numeric_limits<Cnt>::max()){
    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){
        if(data[idx].second > x) data[idx].second -= x;
        else data[idx].first = null_key;
        return;
      }
      h = (h + 1) & mod;
    }
  }
  int count(Key k){
    assert(k!=null_key);
    uint32_t h = f(k^mask) & mod;
    while(true){
      int idx = idx_table[h];
      if(idx==-1) return 0;
      if(data[idx].first==k) return data[idx].second;
      h = (h + 1) & mod;
    }
  }
};

int main(){
  ll n, m;scanf("%lld %lld", &n, &m);
  hashmultiset<ll> st;
  for(int i=0;i<n;i++){
    ll a;scanf("%lld", &a);
    st.insert(a);
  }
  for(int i=0;i<m;i++){
    ll a;scanf("%lld", &a);
    if(i==m-1) printf("%d\n", st.count(a));
    else printf("%d ", st.count(a));
  }
}
0