202006-2 Sparse vector (digging hole)

Insert picture description here
Insert picture description here
Don’t do anything when overtime, don’t cin, don’t cout, give it all to him printf scanf
is a super simple question, no way AC is so hot I can’t write it, I’m so confused I can’t write anything else, I’m speechless

AC code

#include <bits/stdc++.h>
using namespace std;
int main()
{
    
    
    int n, a, b;
    scanf("%d %d %d",&n,&a,&b);
    vector<pair<int, int>> v1;
    for(int i = 0; i < a; i++)
    {
    
    
        int x, y;
        scanf("%d %d",&x,&y);
        v1.push_back(make_pair(x, y));
    }
    long long ans = 0;
    int l = 0;
    for(int j = 0; j < b; j++)
    {
    
    
        int x1, y1;
        scanf("%d%d",&x1,&y1);
        while(l < a)
        {
    
    
            if(v1[l].first > x1) break;
            else if(v1[l].first < x1) l++;
            else
            {
    
    
                ans += y1 * v1[l].second;
                l++;
            }
        }
    }
    printf("%lld",ans);
    return 0;
}



Dig a hole (60 points)

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
struct node
{
    
    
    int index;
    int value;
};
vector<node> A,B;
int main()
{
    
    
    int n, a, b;
    scanf("%d %d %d",&n,&a,&b);
    for(int i = 0; i < a; i++)
    {
    
    
        node temp;
        scanf("%d %d",&temp.index, &temp.value);
        A.push_back(temp);
    }
    for(int j = 0; j < b; j++)
    {
    
    
        node t;
        scanf("%d %d",&t.index, &t.value);
        B.push_back(t);
    }
    LL ans = 0;
    for(int i = 0, j = 0; i < A.size(), j < B.size(); )
    {
    
    
        if(A[i].index > B[j].index) j++;
        else if( A[i].index == B[j].index)
        {
    
    
            ans += (LL)A[i].value * B[j].value;
            i++;
            j++;
        }
        else i++;
    }
    printf("%lld", ans);
    return 0;
}


Guess you like

Origin blog.csdn.net/moumoumouwang/article/details/108804577