求改代码
查看原帖
求改代码
1469663
San_yu楼主2025/7/18 21:32

我发现不能更新i.second的值,代码39行为什么没有用?

#include <cstdio>
#include<iostream>
#include<vector>
#define LL long long
#define uLL unsigned long long 
int n;
const int mod = 999983;
std::vector<std::pair<unsigned long long, unsigned long long>> linker[mod + 2];
unsigned LL ans = 0;
LL read()
{
    char ch = 0;LL x = 0,s = 1;
    while(ch < '0' || ch > '9'){
        if(ch == '-') {
            s = -1; 
        }
        ch = getchar();
    }
    while(ch >= '0' && ch <= '9'){
        x = x * 10 + ch - '0';
        ch = getchar();
    }
    return x * s;
}
uLL hash (uLL x)
{
    return x % mod;
}
uLL insert(uLL x, uLL y)
{
    uLL tp = 0;
    for(auto i : linker[hash(x)]){
        if(i.first == x) {
            //i.first 记录x的值
            //i.second 记录y的值
            tp = i.second;
            i.second = y;
            //std::cout<<"tp = "<<tp<<'\n';
            return tp;
        }
    }
    linker[hash(x)].push_back(std::make_pair(x, y));
    return 0;
}
int main()
{
    std::cin>>n;
    for(uLL i = 1; i <= n; i++){
        //std::cout<<'\n';
        //std::cout<<"i = "<<i<<'\n';
        uLL op = 0, tmp = 0;
        op = read(), tmp = read();
        ans += i * insert(op, tmp);
        //std::cout<<"ans = "<<ans <<'\n';
    }
    std::cout<<ans;
    return 0;
}

#include <cstdio>
#include<iostream>
#include<vector>
#define LL long long
#define uLL unsigned long long 
int n;
const int mod = 999983;
std::vector<std::pair<unsigned long long, unsigned long long>> linker[mod + 2];
unsigned LL ans = 0;
LL read()
{
    char ch = 0;LL x = 0,s = 1;
    while(ch < '0' || ch > '9'){
        if(ch == '-') {
            s = -1; 
        }
        ch = getchar();
    }
    while(ch >= '0' && ch <= '9'){
        x = x * 10 + ch - '0';
        ch = getchar();
    }
    return x * s;
}
uLL hash (uLL x)
{
    return x % mod;
}
uLL insert(uLL x, uLL y)
{
    uLL tp = 0;
    for(auto i : linker[hash(x)]){
        if(i.first == x) {
            tp = i.second;
            i.second = y;
            //std::cout<<"tp = "<<tp<<'\n';
            return tp;
        }
    }
    linker[hash(x)].push_back(std::make_pair(x, y));
    return 0;
}
int main()
{
    std::cin>>n;
    for(uLL i = 1; i <= n; i++){
        //std::cout<<'\n';
        //std::cout<<"i = "<<i<<'\n';
        uLL op = 0, tmp = 0;
        op = read(), tmp = read();
        ans += i * insert(op, tmp);
        //std::cout<<"ans = "<<ans <<'\n';
    }
    std::cout<<ans;
    return 0;
}

2025/7/18 21:32
加载中...