結果

問題 No.8016 unordered_mapなるたけ落とすマン
ユーザー antaanta
提出日時 2016-05-22 00:40:22
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 29 ms / 1,000 ms
コード長 11,190 bytes
コンパイル時間 1,098 ms
コンパイル使用メモリ 105,144 KB
実行使用メモリ 11,464 KB
最終ジャッジ日時 2024-10-11 05:01:48
合計ジャッジ時間 4,881 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 6 ms
11,392 KB
testcase_01 AC 6 ms
11,264 KB
testcase_02 AC 6 ms
11,224 KB
testcase_03 AC 27 ms
11,352 KB
testcase_04 AC 25 ms
11,288 KB
testcase_05 AC 26 ms
11,296 KB
testcase_06 AC 25 ms
11,360 KB
testcase_07 AC 28 ms
11,392 KB
testcase_08 AC 29 ms
11,224 KB
testcase_09 AC 26 ms
11,392 KB
testcase_10 AC 26 ms
11,360 KB
testcase_11 AC 26 ms
11,348 KB
testcase_12 AC 26 ms
11,392 KB
testcase_13 AC 25 ms
11,464 KB
testcase_14 AC 26 ms
11,392 KB
testcase_15 AC 24 ms
11,384 KB
testcase_16 AC 27 ms
11,392 KB
testcase_17 AC 25 ms
11,392 KB
testcase_18 AC 25 ms
11,384 KB
testcase_19 AC 24 ms
11,396 KB
testcase_20 AC 25 ms
11,408 KB
testcase_21 AC 22 ms
11,284 KB
testcase_22 AC 23 ms
11,264 KB
testcase_23 AC 25 ms
11,368 KB
testcase_24 AC 24 ms
11,372 KB
testcase_25 AC 24 ms
11,264 KB
testcase_26 AC 23 ms
11,372 KB
testcase_27 AC 18 ms
11,392 KB
testcase_28 AC 17 ms
11,392 KB
testcase_29 AC 18 ms
11,384 KB
testcase_30 AC 18 ms
11,228 KB
testcase_31 AC 20 ms
11,392 KB
testcase_32 AC 21 ms
11,284 KB
testcase_33 AC 19 ms
11,392 KB
testcase_34 AC 18 ms
11,392 KB
testcase_35 AC 20 ms
11,384 KB
testcase_36 AC 23 ms
11,348 KB
testcase_37 AC 20 ms
11,344 KB
testcase_38 AC 19 ms
11,392 KB
testcase_39 AC 22 ms
11,280 KB
testcase_40 AC 20 ms
11,392 KB
testcase_41 AC 6 ms
11,392 KB
testcase_42 AC 6 ms
11,392 KB
testcase_43 AC 6 ms
11,364 KB
testcase_44 AC 7 ms
11,440 KB
testcase_45 AC 7 ms
11,264 KB
testcase_46 AC 6 ms
11,352 KB
testcase_47 AC 17 ms
11,460 KB
testcase_48 AC 20 ms
11,392 KB
testcase_49 AC 20 ms
11,392 KB
testcase_50 AC 16 ms
11,340 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <string>
#include <vector>
#include <algorithm>
#include <numeric>
#include <set>
#include <map>
#include <queue>
#include <iostream>
#include <sstream>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <cstring>
#include <cctype>
#include <cassert>
#include <limits>
#include <functional>
#include <random>
#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))
#define rer(i,l,u) for(int (i)=(int)(l);(i)<=(int)(u);++(i))
#define reu(i,l,u) for(int (i)=(int)(l);(i)<(int)(u);++(i))
#if defined(_MSC_VER) || __cplusplus > 199711L
#define aut(r,v) auto r = (v)
#else
#define aut(r,v) __typeof(v) r = (v)
#endif
#define each(it,o) for(aut(it, (o).begin()); it != (o).end(); ++ it)
#define all(o) (o).begin(), (o).end()
#define pb(x) push_back(x)
#define mp(x,y) make_pair((x),(y))
#define mset(m,v) memset(m,v,sizeof(m))
#define INF 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3fLL
using namespace std;
typedef vector<int> vi; typedef pair<int, int> pii; typedef vector<pair<int, int> > vpii; typedef long long ll;
template<typename T, typename U> inline void amin(T &x, U y) { if(y < x) x = y; }
template<typename T, typename U> inline void amax(T &x, U y) { if(x < y) x = y; }

class FastInput {
	bool _end;
public:
	FastInput() : _end(false) {}

	operator void*() { return _end ? 0 : (void*)this; }

	template<typename T>
	void read_unsigned(T *res) {
		T x = 0;
		for(char c = skip(); '0' <= c && c <= '9'; c = gc())
			x = x * 10 + (c - '0');
		*res = x;
	}

	template<typename T>
	void read_signed(T *res) {
		char c = skip();
		bool sign = false;
		if(c == '-') sign = true, c = gc();
		T x = 0;
		for(; '0' <= c && c <= '9'; c = gc())
			x = x * 10 + (c - '0');
		*res = !sign ? x : -x;
	}

	void read_c_string(char *str, int *len) {
		int n = 0;
		for(char c = skip(); !is_delim(c); c = gc())
			str[n ++] = c;
		str[n] = 0;
		*len = n;
	}

	void read_string(std::string *str) {
		str->clear();
		for(char c = skip(); !is_delim(c); c = gc())
			*str += c;
	}

	void read_line(std::string *str) {
		str->clear();
		for(char c = gc(); c != '\n'; c = gc())
			*str += c;
		if(!str->empty() && (*str)[str->size() - 1] == '\r') str->resize(str->size() - 1);
	}

	void read_double(double *res) {
		std::string buf;
		read_string(&buf);
		sscanf(buf.c_str(), "%lf", res);
	}

	void read_char(char *res) {
		*res = skip();
	}

	void read_string_buf(char *res, int n) {
		*res = skip();
	}

	FastInput &operator()(char &res) { read_char(&res); return *this; }
	FastInput &operator()(int &res) { read_signed(&res); return *this; }
	FastInput &operator()(unsigned &res) { read_unsigned(&res); return *this; }
	FastInput &operator()(long long &res) { read_signed(&res); return *this; }
	FastInput &operator()(unsigned long long &res) { read_unsigned(&res); return *this; }
	FastInput &operator()(char *res) { int len; read_c_string(res, &len); return *this; }
	FastInput &operator()(std::string &res) { read_string(&res); return *this; }
	FastInput &operator()(double &res) { read_double(&res); return *this; }

	template<typename T1, typename T2>
	FastInput &operator()(T1 &res1, T2 &res2) {
		return operator()(res1)(res2);
	}
	template<typename T1, typename T2, typename T3>
	FastInput &operator()(T1 &res1, T2 &res2, T3 &res3) {
		return operator()(res1)(res2)(res3);
	}

	template<typename T>
	FastInput &a(T *a, int n) {
		for(int i = 0; i < n; ++ i)
			operator()(a[i]);
		return *this;
	}

	template<typename T>
	FastInput &operator()(vector<T> &v) {
		for(size_t i = 0; i < v.size(); ++ i)
			operator()(v[i]);
		return *this;
	}

private:
	static char gc() {
#if defined(__GNUC__) && !defined(__MINGW32__)
		return (char)getchar_unlocked();
#elif defined(_MSC_VER)
		return (char)_getchar_nolock();
#else
		return (char)getchar();
#endif
	}

	static bool is_delim(char c) {
		return c == ' ' || c == '\t' || c == '\n' || c == '\r' || c == EOF;
	}

	char skip() {
		if(_end) return EOF;
		char c;
		for(c = gc(); c != -1 && is_delim(c); c = gc());
		if(c == EOF) _end = true;
		return c;
	}
} in;

class FastOutput {
public:

	template<typename T>
	void print_unsigned(T x) {
		char buf[24]; int n = 0;
		do buf[n ++] = x % 10, x /= 10; while(x != 0);
		while(n > 0) pc('0' + buf[-- n]);
	}

	template<typename T>
	void print_signed(T x) {
		char buf[24]; int n = 0; bool sign = false;
		if(x < 0) sign = true;
		//depends on the -x % y behaviour
		do buf[n ++] = x % 10, x /= 10; while(x != 0);
		if(!sign) {
			while(n > 0) pc('0' + buf[-- n]);
		} else {
			pc('-');
			while(n > 0) pc('0' - buf[-- n]);
		}
	}

	void print_c_string(const char *str) {
		for(const char *p = str; *p; ++ p)
			pc(*p);
	}

	void print_string(const std::string &str) {
		print_c_string(str.c_str());
	}

	void print_double(double x, int digits) {
		//|res| < 2^1024 ~ 10^308
		char buf[512];
#ifdef _MSC_VER
		sprintf_s(buf, "%.*f", digits, x);
#else
		sprintf(buf, "%.*f", digits, x);
#endif
		print_c_string(buf);
	}

	void print_char(char x) {
		pc(x);
	}

	FastOutput &operator()(char x) { print_char(x); return *this; }
	FastOutput &operator()(int x) { print_signed(x); return *this; }
	FastOutput &operator()(unsigned x) { print_unsigned(x); return *this; }
	FastOutput &operator()(long long x) { print_signed(x); return *this; }
	FastOutput &operator()(unsigned long long x) { print_unsigned(x); return *this; }
	FastOutput &operator()(const char *str) { print_c_string(str); return *this; }
	FastOutput &operator()(const std::string &str) { print_string(str); return *this; }
	FastOutput &operator()(double x) { print_double(x, 10); return *this; }

	template<typename T1, typename T2>
	FastOutput &operator()(T1 x1, T2 x2) { return operator()(x1)(x2); }
	template<typename T1, typename T2, typename T3>
	FastOutput &operator()(T1 x1, T2 x2, T3 x3) { return operator()(x1)(x2)(x3); }
	template<typename T1, typename T2, typename T3, typename T4>
	FastOutput &operator()(T1 x1, T2 x2, T3 x3, T4 x4) { return operator()(x1)(x2)(x3)(x4); }

private:
	static void pc(char c) {
#if defined(__GNUC__) && !defined(__MINGW32__)
		putchar_unlocked(c);
#elif defined(_MSC_VER)
		_putchar_nolock(c);
#else
		putchar(c);
#endif
	}
} out;


struct HashTable {
public:
	static unsigned long long a1, a0;
	typedef unsigned long long Key;
	typedef int Val;
	struct KeyVal {
		Key key;
		Val val;
		KeyVal() {}
		KeyVal(const Key &key_, const Val &val_) : key(key_), val(val_) {}
	};
	typedef int Size, Index;

	class ConstIterator {
		friend HashTable;
		const HashTable *hashTable;
		const KeyVal *p, *pEnd;
		explicit ConstIterator(const HashTable *hashTable_, const KeyVal *p_, const KeyVal *pEnd_) :
			hashTable(hashTable_), p(p_), pEnd(pEnd_) {
			skip();
		}
		void skip() { while(p != pEnd && hashTable->isEmptyKey(p->key)) ++ p; }
	public:
		ConstIterator() : p(NULL), pEnd(NULL) {}
		const KeyVal &operator*() const { return *p; }
		ConstIterator &operator++() { ++ p; skip(); return *this; }
		bool operator!=(const ConstIterator &that) const { return p != that.p; }
	};

	KeyVal emptyKeyVal() const { return{ ~0ULL, 0 }; }

	Index hash(const Key &key) const { return (Index)((a1 * key + a0) >> 32); }

	bool equals(const Key &key1, const Key &key2) const { return key1 == key2; }
	bool isEmptyKey(const Key &key) const { return key == emptyKeyVal().key; }

public:
	HashTable(Size minCapacity = 0) : table(NULL), tableSize(0), numElements(0) {
		reserve(minCapacity);
	}

	HashTable(const HashTable &that) : table(NULL), tableSize(0), numElements(0) {
		*this = that;
	}

	HashTable &operator=(const HashTable &that) {
		if(this == &that) return *this;
		numElements = 0;	//
		reserve(that.numElements);
		clear();
		copyTable(that.table, that.table + that.tableSize);
		numElements = that.numElements;
		return *this;
	}

	~HashTable() {
		delete[] table;
		table = NULL;
	}

	void swap(HashTable &that) {
		std::swap(table, that.table);
		std::swap(tableSize, that.tableSize);
		std::swap(numElements, that.numElements);
	}

	void clear() {
		for(KeyVal *p = table, *pEnd = p + tableSize; p != pEnd; ++ p)
			if(!isEmptyKey(p->key))
				*p = emptyKeyVal();
		numElements = 0;
	}

	bool empty() const { return numElements == 0; }
	Size size() const { return numElements; }
	Size capacity() const { return calculateCapacity(tableSize); }

	bool reserve(Size minCapacity) {
		if(minCapacity <= calculateCapacity(tableSize))
			return false;
		Size newSize = tableSize == 0 ? 4 : tableSize * 2;
		while(calculateCapacity(newSize) < minCapacity)
			newSize *= 2;
		reallocate(newSize);
		return true;
	}

	ConstIterator begin() const { return ConstIterator(this, table, table + tableSize); }
	ConstIterator end() const { return ConstIterator(this, table + tableSize, table + tableSize); }

private:
	static Size calculateCapacity(Size tableSize) {
		return tableSize * 3 / 4;
	}

	void copyTable(const KeyVal *p, const KeyVal *pEnd) {
		for(; p != pEnd; ++ p) {
			if(!isEmptyKey(p->key)) {
				Index index;
				bool found = probe(p->key, index);
				assert(!found);
				table[index] = *p;
			}
		}
	}

	void reallocate(Size newSize) {
		assert(newSize > tableSize && (newSize & (newSize - 1)) == 0);
		KeyVal *oldTable = table;
		Size oldSize = tableSize;
		table = new KeyVal[newSize];
		tableSize = newSize;
		std::fill(table, table + tableSize, emptyKeyVal());
		if(numElements != 0)
			copyTable(oldTable, oldTable + oldSize);
		delete[] oldTable;
	}

	bool probe(const Key &key, Index &resIndex) const {
		assert(numElements < tableSize);
		Size mask = tableSize - 1;
		Index h = hash(key) & mask, numProbes = 0;
		while(1) {
			const KeyVal &kv = table[h];
			if(isEmptyKey(kv.key)) {
				resIndex = h;
				return false;
			} else if(equals(kv.key, key)) {
				resIndex = h;
				return true;
			}
			++ numProbes;
			h = (h + numProbes) & mask;
			assert(numProbes < tableSize);
		}
	}

public:
	bool insert(const Key &key, const Val &val) {
		reserve(numElements + 1);
		Index index;
		bool found = probe(key, index);
		table[index] = KeyVal(key, val);
		++ numElements;
		return found;
	}

	const Val *find(const Key &key) const {
		if(empty()) return NULL;
		Index index;
		bool found = probe(key, index);
		if(!found) return NULL;
		return &table[index].val;
	}

	Val *find(const Key &key) {
		return const_cast<Val*>(const_cast<const HashTable*>(this)->find(key));
	}

	Val getVal(const Key &key) const {
		const Val *p = find(key);
		return !p ? Val() : *p;
	}

	Val *findInsert(const Key &key, const Val &val) {
		reserve(numElements + 1);
		Index index;
		bool found = probe(key, index);
		if(!found) {
			table[index] = KeyVal(key, val);
			++ numElements;
		}
		return &table[index].val;
	}

private:
	KeyVal *table;
	Size tableSize;		//power of two (or 0)
	Size numElements;	//number of nonempty elements
};
unsigned long long HashTable::a0, HashTable::a1;

int main() {
	mt19937_64 mt{ random_device{}() };
	HashTable::a1 = mt();
	HashTable::a0 = mt();

	int N; int M;
	while(in(N, M)) {
		HashTable table;
		table.reserve(200000);
		rep(i, N) {
			long long a;
			in(a);
			++ *table.findInsert(a, 0);
		}
		rep(i, M) {
			long long b;
			in(b);
			auto p = table.find(b);
			int ans = !p ? 0 : *p;
			if(i != 0) out(' ');
			out(ans);
		}
		out('\n');
	}
	return 0;
}
0