题目描述
注意事项
- 对于N=10^9的维度,如果这样遍历: for (int i = 1; i <= n; i++) 去处理两个向量的乘积会超时!正确的做法应该是读取第二个向量的时候即一边处理。
- 数据范围较大,乘积的结果可能会超过Int的范围, 需要用 long long来存储结果
- 当数据规模N较大的时候,推荐用scanf, printf进行输入输出。
代码
#include <iostream>
#include <cstdio>
#include <vector>
#include <map>
using namespace std;
typedef long long LL;
int n, a, b;
map<int, int> s;
int main()
{
scanf("%d%d%d", &n, &a, &b);
while (a--)
{
int i, v;
scanf("%d%d", &i, &v);
s[i] = v;
}
LL ans = 0;
while (b--)
{
int i, v;
scanf("%d%d", &i, &v);
if (s.count(i))
ans += (LL)s[i] * v;
}
cout << ans << endl;
return 0;
}