AcWing 3288. 稀疏向量csp19(1)
原题链接
简单
作者:
YAX_AC
,
2024-11-30 18:56:20
,
所有人可见
,
阅读 8
法1
#include<iostream>
#include<algorithm>
#include<cstring>
#include<unordered_map>
#include<vector>
using namespace std;
typedef long long LL;
const int N = 5*100010;
LL n,a,b;
unordered_map<LL,LL> mp1,mp2;
LL sum = 0;
int main()
{
cin>>n>>a>>b;
LL l,r;
while(a--)
{
cin>>l>>r;
mp1[l]=r;
}
while(b--)
{
cin>>l>>r;
mp2[l]=r;
}
for(auto i=mp1.begin(); i!=mp1.end(); i++)
{
//cout<<i->first<<' ';
if(mp2.count((int)i->first)!=0)
{
sum+=mp1[i->first]*mp2[i->first];
//cout<<sum<<endl;
}
}
cout<<sum;
return 0;
}
法2
#include<iostream>
#include<algorithm>
#include<cstring>
#include<unordered_map>
#include<vector>
using namespace std;
typedef long long LL;
LL n,a,b;
unordered_map<LL,LL> mp;
LL sum = 0;
int main()
{
cin>>n>>a>>b;
LL l,r;
while(a--)
{
cin>>l>>r;
mp[l]=r;
}
while(b--)
{
cin>>l>>r;
if(mp[l])
{
sum+=mp[l]*r;
}
}
cout<<sum;
return 0;
}