第19次CSP CCF认证 稀疏向量(svector)

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
题意比较简单就是找u*v的值,在最后n的值较大,不能通过数组下标进行记忆。这也是本题的难点。可以使用二分法或map标记的方法。
二分解法:
在这里插入图片描述

//#include <bits/stdc++.h>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <unordered_map>
#define lowbit(x) ((x) & -(x))
#define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
const int maxn = 1e6 + 7;
typedef long long ll;
using namespace std;
const ll mod = 1e9 + 7;
const double pi = acos(-1.0);
const double c = 0.57721566490153286060651209;
const double esp = 1e-8;
const int INF = 0x7fffffff;
inline int read() {
    int x = 0, f = 1;
    char ch = getchar();
    while (ch < '0' || ch > '9') {
        if (ch == '-')
            f = -1;
        ch = getchar();
    }
    while (ch >= '0' && ch <= '9') {
        x = (x << 1) + (x << 3) + (ch ^ 48);
        ch = getchar();
    }
    return x * f;
}
map<int,ll>mp1;
map<int,ll>mp2;
ll u1[maxn], v1[maxn], u2[maxn], v2[maxn];
int main() {
    ios::sync_with_stdio(false);
    //freopen("text.txt","r",stdin);
    //freopen("out1.txt","w",stdout);
    ll n, a, b;
    cin >> n >> a >> b;
    for (int i = 0; i < a; i ++){
        cin >> u1[i] >> u2[i];
    }
    for (int i = 0; i < b; i ++){
        cin >> v1[i] >> v2[i];
    }   
    ll ans = 0;
    for (int i = 0; i < a; i ++){
        int p = lower_bound(v1,v1+b,u1[i]) - v1;
        if (v1[p] == u1[i])
            ans += (u2[i] * v2[p]);
    }
    cout << ans << endl;
    return 0;
}

map标记的方法不在写出,需要注意的是map的常数较大,效率不如二分法。若出题人在加大问题规模,那么map就会tle了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值