想着用无序 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){
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;
}