![Insert image description here](https://img-blog.csdnimg.cn/20200727121855472.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQzNzQ5MjFFYy,size_16
Inscription
Brush both arrays like a zipper, and be careful not to cross the border when brushing back inside. In addition, the ans to save the answer should use long long. In fact, this question is in ascending subscript order by default. If you are worried, you can write a cmp and use sort to sort it out.
code show as below
#include <iostream>
#include <algorithm>
using namespace std;
const int Maxn=1000000;
//输入
int n,a,b;
struct vertex{
int index;
int value;
};
bool cmp(vertex&a,vertex&b){
return a.index<=b.index;
}
vertex u[Maxn],v[Maxn];
void solve(){
int i=0,j=0;
long long ans=0;
sort(u,u+a,cmp);
sort(v,v+b,cmp);
while(i<a&&j<b){
if(u[i].index<v[j].index){
while(u[i].index<v[j].index&&i<a)i++;
}
else if (u[i].index>v[j].index&&j<b) {
while(u[i].index>v[j].index)j++;
}
if (u[i].index==v[j].index) {
ans+=u[i].value*v[j].value;
i++,j++;
}
}
printf("%lld\n",ans);
}
int main()
{
scanf("%d %d %d",&n,&a,&b);
for(int i=0;i<a;i++)
scanf("%d %d",&u[i].index,&u[i].value);
for(int i=0;i<b;i++)
scanf("%d %d",&v[i].index,&v[i].value);
solve();
return 0;
}