P11615 Solution
OIerWu_829 · · 题解
题目传送门
我们可以用链地址法来解决哈希冲突。
用一个结构体表示链表中的节点,
每次输入一个键
#include <iostream>
#include <cstring>
using namespace std;
using ULL = unsigned long long;
char buf[1 << 23], *p1 = buf, *p2 = buf;
#define gc() (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 21, stdin), p1 == p2) ? EOF : *p1++)
inline ULL rd() {
ULL x = 0;
char ch = gc();
while (!isdigit(ch)) ch = gc();
while (isdigit(ch)) x = x * 10 + (ch ^ 48), ch = gc();
return x;
}
const int N = 5e6 + 5;
const int M = 1e7 + 7;
struct Edge
{
ULL k, v;
int nxt;
} e[N];
int head[M], cnt;
void insert(ULL x, ULL y, ULL &p)
{
int pos = x % M;
for (int i = head[pos]; i != -1; i = e[i].nxt)
if (e[i].k == x)
{
p = e[i].v;
e[i].v = y;
return;
}
p = 0;
e[cnt].k = x;
e[cnt].v = y;
e[cnt].nxt = head[pos];
head[pos] = cnt++;
}
int main()
{
int n = rd();
memset(head, -1, sizeof head);
cnt = 0;
ULL ans = 0;
for (int i = 1; i <= n; i++)
{
ULL x = rd(), y = rd(), p;
insert(x, y, p);
ans += (ULL)i * p;
}
cout << ans;
return 0;
}