使用 unordered_map 有 3 个 TLE?
查看原帖
使用 unordered_map 有 3 个 TLE?
599966
okok8260楼主2022/8/7 23:33

想着用无序 map 能比 map 快一点就用了, 结果发现有 3 个TLE: #21 #22 #24, 同时, #17 和 #19 时间都很长。是因为计算 pos = (x << 32) | y; 的时候太慢了吗?

代码如下:

#include <bits/stdc++.h>

using ull = unsigned long long;
using ll = long long;
using namespace std;

ll n, x, y, z;
ull sum=0;

unordered_map<ull, ll> m;

inline ull pos(ll x, ll y){ return (x << 32) | y;}

ll xoff[4] = {-1, 1, 0, 0};
ll yoff[4] = {0, 0, 1, -1};

ull over(ll xoff, ll yoff){  // 0,1 | 1,0 | 0,-1 | -1,0
    ll self = m[pos(x, y)], that = m[pos(x+xoff, y+yoff)];
    if ( self >= that) return 0;
    return min(that-self, z);
}

int main() {

    scanf("%lld", &n);
    while(n--){
        scanf("%lld%lld%lld", &x, &y, &z);
        sum += 4 * z;

        for (int i = 0; i < 4; ++i) {
            if (m.find(pos(x+xoff[i], y+yoff[i])) != m.end())
                sum -= 2 * over(xoff[i], yoff[i]);
        }
        m[pos(x, y)] += z;

        printf("%llu\n", sum);
    }


    return 0;
}
2022/8/7 23:33
加载中...