题意比较简单就是找u*v的值,在最后n的值较大,不能通过数组下标进行记忆。这也是本题的难点。可以使用二分法或map标记的方法。
二分解法:
//#include <bits/stdc++.h>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <unordered_map>
#define lowbit(x) ((x) & -(x))
#define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
const int maxn = 1e6 + 7;
typedef long long ll;
using namespace std;
const ll mod = 1e9 + 7;
const double pi = acos(-1.0);
const double c = 0.57721566490153286060651209;
const double esp = 1e-8;
const int INF = 0x7fffffff;
inline int read() {
int x = 0, f = 1;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-')
f = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
x = (x << 1) + (x << 3) + (ch ^ 48);
ch = getchar();
}
return x * f;
}
map<int,ll>mp1;
map<int,ll>mp2;
ll u1[maxn], v1[maxn], u2[maxn], v2[maxn];
int main() {
ios::sync_with_stdio(false);
//freopen("text.txt","r",stdin);
//freopen("out1.txt","w",stdout);
ll n, a, b;
cin >> n >> a >> b;
for (int i = 0; i < a; i ++){
cin >> u1[i] >> u2[i];
}
for (int i = 0; i < b; i ++){
cin >> v1[i] >> v2[i];
}
ll ans = 0;
for (int i = 0; i < a; i ++){
int p = lower_bound(v1,v1+b,u1[i]) - v1;
if (v1[p] == u1[i])
ans += (u2[i] * v2[p]);
}
cout << ans << endl;
return 0;
}
map标记的方法不在写出,需要注意的是map的常数较大,效率不如二分法。若出题人在加大问题规模,那么map就会tle了。