为什么本地UB
查看原帖
为什么本地UB
766934
Bismuth_楼主2025/7/28 11:19

rt,看见题解区里用struct封装的写法挺便利的,自己写了一份本地UB

#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;
}
2025/7/28 11:19
加载中...