結果

問題 No.3016 unordered_mapなるたけ落とすマン
ユーザー niuezniuez
提出日時 2020-05-22 12:37:34
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 22 ms / 1,000 ms
コード長 6,146 bytes
コンパイル時間 1,646 ms
コンパイル使用メモリ 171,764 KB
実行使用メモリ 14,148 KB
最終ジャッジ日時 2024-04-15 01:56:07
合計ジャッジ時間 5,204 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 3 ms
6,944 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 20 ms
13,820 KB
testcase_04 AC 21 ms
13,864 KB
testcase_05 AC 21 ms
13,820 KB
testcase_06 AC 21 ms
13,812 KB
testcase_07 AC 22 ms
13,872 KB
testcase_08 AC 22 ms
13,872 KB
testcase_09 AC 21 ms
13,800 KB
testcase_10 AC 20 ms
13,908 KB
testcase_11 AC 22 ms
14,004 KB
testcase_12 AC 21 ms
13,924 KB
testcase_13 AC 21 ms
14,148 KB
testcase_14 AC 21 ms
13,872 KB
testcase_15 AC 21 ms
13,908 KB
testcase_16 AC 21 ms
13,928 KB
testcase_17 AC 21 ms
13,924 KB
testcase_18 AC 20 ms
13,988 KB
testcase_19 AC 21 ms
13,780 KB
testcase_20 AC 20 ms
11,824 KB
testcase_21 AC 20 ms
13,984 KB
testcase_22 AC 21 ms
13,844 KB
testcase_23 AC 21 ms
13,848 KB
testcase_24 AC 20 ms
11,820 KB
testcase_25 AC 20 ms
13,912 KB
testcase_26 AC 20 ms
14,068 KB
testcase_27 AC 19 ms
11,860 KB
testcase_28 AC 19 ms
11,820 KB
testcase_29 AC 20 ms
11,912 KB
testcase_30 AC 19 ms
11,864 KB
testcase_31 AC 19 ms
11,804 KB
testcase_32 AC 20 ms
11,972 KB
testcase_33 AC 20 ms
13,908 KB
testcase_34 AC 19 ms
11,916 KB
testcase_35 AC 20 ms
14,008 KB
testcase_36 AC 20 ms
13,872 KB
testcase_37 AC 19 ms
11,892 KB
testcase_38 AC 18 ms
11,864 KB
testcase_39 AC 19 ms
12,044 KB
testcase_40 AC 20 ms
11,860 KB
testcase_41 AC 2 ms
6,940 KB
testcase_42 AC 2 ms
6,940 KB
testcase_43 AC 2 ms
6,944 KB
testcase_44 AC 4 ms
6,944 KB
testcase_45 AC 3 ms
6,940 KB
testcase_46 AC 3 ms
6,940 KB
testcase_47 AC 12 ms
9,020 KB
testcase_48 AC 16 ms
10,512 KB
testcase_49 AC 14 ms
8,964 KB
testcase_50 AC 13 ms
8,136 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 << 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];
  }
}
0