結果

問題 No.3016 unordered_mapなるたけ落とすマン
ユーザー niuezniuez
提出日時 2020-05-22 12:34:52
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 26 ms / 1,000 ms
コード長 6,100 bytes
コンパイル時間 1,870 ms
コンパイル使用メモリ 174,052 KB
実行使用メモリ 14,000 KB
最終ジャッジ日時 2024-10-04 09:38:45
合計ジャッジ時間 5,157 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
6,820 KB
testcase_01 AC 3 ms
6,816 KB
testcase_02 AC 3 ms
6,820 KB
testcase_03 AC 26 ms
13,876 KB
testcase_04 AC 22 ms
13,948 KB
testcase_05 AC 19 ms
13,828 KB
testcase_06 AC 21 ms
13,812 KB
testcase_07 AC 20 ms
14,000 KB
testcase_08 AC 22 ms
13,892 KB
testcase_09 AC 21 ms
13,920 KB
testcase_10 AC 20 ms
13,928 KB
testcase_11 AC 20 ms
13,808 KB
testcase_12 AC 20 ms
13,904 KB
testcase_13 AC 20 ms
13,824 KB
testcase_14 AC 20 ms
13,808 KB
testcase_15 AC 19 ms
13,828 KB
testcase_16 AC 20 ms
13,860 KB
testcase_17 AC 21 ms
13,900 KB
testcase_18 AC 20 ms
13,860 KB
testcase_19 AC 19 ms
11,832 KB
testcase_20 AC 19 ms
13,832 KB
testcase_21 AC 19 ms
13,916 KB
testcase_22 AC 19 ms
13,864 KB
testcase_23 AC 20 ms
13,832 KB
testcase_24 AC 20 ms
13,824 KB
testcase_25 AC 19 ms
13,828 KB
testcase_26 AC 18 ms
11,940 KB
testcase_27 AC 19 ms
13,860 KB
testcase_28 AC 20 ms
11,960 KB
testcase_29 AC 19 ms
13,804 KB
testcase_30 AC 19 ms
11,880 KB
testcase_31 AC 20 ms
13,856 KB
testcase_32 AC 19 ms
13,972 KB
testcase_33 AC 19 ms
13,836 KB
testcase_34 AC 20 ms
13,860 KB
testcase_35 AC 18 ms
11,928 KB
testcase_36 AC 19 ms
11,900 KB
testcase_37 AC 19 ms
13,892 KB
testcase_38 AC 18 ms
11,960 KB
testcase_39 AC 19 ms
11,964 KB
testcase_40 AC 19 ms
11,848 KB
testcase_41 AC 2 ms
6,816 KB
testcase_42 AC 2 ms
6,816 KB
testcase_43 AC 2 ms
6,816 KB
testcase_44 AC 3 ms
6,816 KB
testcase_45 AC 3 ms
6,820 KB
testcase_46 AC 4 ms
6,816 KB
testcase_47 AC 13 ms
9,064 KB
testcase_48 AC 16 ms
10,592 KB
testcase_49 AC 13 ms
9,060 KB
testcase_50 AC 11 ms
11,848 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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 << 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(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];
  }
}
0