제출 #288188

#제출 시각아이디문제언어결과실행 시간메모리
2881882qbingxuan이상적인 도시 (IOI12_city)C++14
55 / 100
118 ms2300 KiB
#include <bits/stdc++.h> #ifdef local #define debug(...) qqbx(#__VA_ARGS__, __VA_ARGS__) template <typename H, typename ...T> void qqbx(const char *s, const H& h, T &&...args) { for(; *s && *s != ','; ++s) if(*s != ' ') std::cerr << *s; std::cerr << " = " << h << (sizeof...(T) ? ", " : "\n"); if constexpr(sizeof...(T)) qqbx(++s, args...); } #define safe std::cerr<<__PRETTY_FUNCTION__<<" line "<<__LINE__<<" safe\n" #else #define debug(...) ((void)0) #define safe ((void)0) #endif // local #define pb emplace_back #define all(v) begin(v),end(v) using namespace std; const int MOD = 1000000000; int calc(int L, int i, int R) { // return \sum_{j=L}^R abs(j-i) // 0 + 1 + 2 + ... + i-L + // 1 + 2 + ... + R-i return (1LL * (i-L) * (i-L+1) / 2 + 1LL * (R-i) * (R-i+1) / 2) % MOD; } int jizz(int len) { return 1LL*len*(len+1)*(len+2)/6%MOD; } int DistanceSum(int n, int x[], int y[]) { if(n > 2000) { vector<pair<int,int>> v; vector<pair<int,int>> s; for(int i = 0; i < n; i++) v.pb(x[i], y[i]); sort(all(v)); int lastL = 0, lastR = INT_MAX; int ans = 0, cnt = 0; map<int,int> sum; for(int i = 0, j; i < n; i = j) { for(j = i+1; j < n; j++) if(v[i].first != v[j].first) break; int l = v[i].second, r = v[j-1].second; int L = max(lastL, l), R = min(lastR, r); map<int,int> tmp; for(int i = L; i <= R; i++) { int s = (sum[i] + cnt) % MOD; tmp[i] = s; } for(int i = L-1; i >= l; i--) { int s = (tmp[i+1] + cnt) % MOD; tmp[i] = s; } for(int i = R+1; i <= r; i++) { int s = (tmp[i-1] + cnt) % MOD; tmp[i] = s; } for(int i = l; i <= r; i++) ans = (ans + tmp[i]) % MOD; ans = (ans + jizz(r-l)) % MOD; for(int i = l; i <= r; i++) tmp[i] = (tmp[i] + calc(l, i, r)) % MOD; sum = tmp; cnt += r-l+1; lastL = l, lastR = r; } return ans; } vector<tuple<int,int,int>> p(n); vector<vector<int>> g(n); for(int i = 0; i < n; i++) p[i] = {i, x[i], y[i]}; sort(all(p), [](auto a, auto b){return get<1>(a) != get<1>(b) ? get<1>(a) < get<1>(b) : get<2>(a) < get<2>(b);}); for(int i = 1; i < n; i++) if(get<1>(p[i]) == get<1>(p[i-1]) && get<2>(p[i]) == get<2>(p[i-1])+1) { int a = get<0>(p[i]), b = get<0>(p[i-1]); g[a].pb(b), g[b].pb(a); } sort(all(p), [](auto a, auto b){return get<2>(a) != get<2>(b) ? get<2>(a) < get<2>(b) : get<1>(a) < get<1>(b);}); for(int i = 1; i < n; i++) if(get<2>(p[i]) == get<2>(p[i-1]) && get<1>(p[i]) == get<1>(p[i-1])+1) { int a = get<0>(p[i]), b = get<0>(p[i-1]); g[a].pb(b), g[b].pb(a); } int ans = 0; for(int i = 0; i < n; i++) { queue<int> q; vector<int> dis(n, -1); dis[i] = 0; q.push(i); while(!q.empty()) { int i = q.front(); q.pop(); for(int j: g[i]) if(dis[j] == -1) dis[j] = dis[i] + 1, q.push(j); } for(int j = 0; j < i; j++) ans = (ans + dis[j]) % MOD; } debug(ans); return ans; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...