結果
| 問題 |
No.8016 unordered_mapなるたけ落とすマン
|
| ユーザー |
tonegawa
|
| 提出日時 | 2020-11-25 03:03:18 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
RE
|
| 実行時間 | - |
| コード長 | 4,853 bytes |
| コンパイル時間 | 1,549 ms |
| コンパイル使用メモリ | 132,552 KB |
| 最終ジャッジ日時 | 2025-01-16 05:35:00 |
|
ジャッジサーバーID (参考情報) |
judge5 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | RE * 3 |
| other | RE * 48 |
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:196:16: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
196 | ll n, m;scanf("%lld %lld", &n, &m);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~
main.cpp:199:15: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
199 | ll a;scanf("%lld", &a);
| ~~~~~^~~~~~~~~~~~
main.cpp:203:15: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
203 | 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 <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){}
/*
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));
}
}
tonegawa