社区讨论

为什么本地UB

P11615【模板】哈希表参与者 1已保存回复 0

讨论操作

快速查看讨论及其快照的属性,并进行相关操作。

当前回复
0 条
当前快照
1 份
快照标识符
@mdmjjtd6
此快照首次捕获于
2025/07/28 11:21
7 个月前
此快照最后确认于
2025/11/04 03:37
4 个月前
查看原帖
rt,看见题解区里用struct封装的写法挺便利的,自己写了一份本地UB
CPP
#include <bits/stdc++.h>
using namespace std;
#define ull unsigned long long

const int N = 1e7 + 9;

struct hasht {
	ull h[N], b[N];
	bool vis[N];
	void yu() {
		fill(h, h + N, 0);
		fill(b, b + N, 0);
		fill(vis, vis + N, false);
		return;
	}
	void insert(ull x, ull y) {
		int tmp = x % N;
		while (vis[tmp]) {
			if (b[tmp] == x)
				break;
			tmp++;
			if (tmp == N)
				tmp = 0;
		}
		b[tmp] = x, h[tmp] = y, vis[tmp] = true;
		return;
	}
	ull find(ull x) {
		int tmp = x % N;
		while (vis[tmp]) {
			if (b[tmp] == x)
				return h[tmp];
			tmp++;
			if (tmp == N)
				tmp = 0;
		}
		return 0;
	}
};

int main() {
	ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
	int n;
	cin >> n;
	hasht ha;
	ha.yu();
	ull sum = 0;
	for (int i = 1; i <= n; i++) {
		ull x, y;
		cin >> x >> y;
		//	cout << 1;
		ull ans = i * ha.find(x);
		sum += ans;
		ha.insert(x, y);
	}
	cout << sum;
	return 0;
}

回复

0 条回复,欢迎继续交流。

正在加载回复...