昨天做题的时候用unordered_map一直TLE,改用离散化过了。
今天去cf题解下面翻评论区发现map能过。了解之后知道unordered_map可以被卡哈希卡到O(n)的访问效率。
解决办法的话有一个自定义hash函数,这里放上大佬连接添加链接描述
具体用法是添加头文件#include<chrono>
加入如下代码块
struct custom_hash {
static uint64_t splitmix64(uint64_t x) {
x += 0x9e3779b97f4a7c15;
x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
return x ^ (x >> 31);
}
size_t operator()(uint64_t x) const {
static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
return splitmix64(x + FIXED_RANDOM);
}
};
声明时使用如下格式
unordered_map<int,int,custom_hash>mp;
即可