csp认证 202006-2 稀疏向量

解
用map来存储同一位置的u和v即可,key是index。注意可能溢出。
map<int, pair<int, int>> m;
//
// Created by 29273 on 2021-04-07.
//
#include "bits/stdc++.h"
using namespace std;
long long res;
int main() {
map<int, pair<int, int>> m;
int n, a, b, x, y;
cin >> n >> a >> b;
for (int i = 1; i <= a; ++i) {
cin >> x >> y;
m[x].first = y;
}
for (int i = 1; i <= b; ++i) {
cin >> x >> y;
m[x].second = y;
}
for (auto i : m) {
res += i.second.first * i.second.second;
}
cout << res << endl;
return 0;
}

浙公网安备 33010602011771号