This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define MASK(i) (1LL << (i))
#define GETBIT(mask, i) (((mask) >> (i)) & 1)
#define ALL(v) (v).begin(), (v).end()
#define block_of_code if(true)
ll max(ll a, ll b){return (a > b) ? a : b;}
ll min(ll a, ll b){return (a < b) ? a : b;}
ll gcd(ll a, ll b){return __gcd(a, b);}
ll lcm(ll a, ll b){return a / gcd(a, b) * b;}
ll LASTBIT(ll mask){return (mask) & (-mask);}
int pop_cnt(ll mask){return __builtin_popcountll(mask);}
int ctz(ull mask){return __builtin_ctzll(mask);}
int logOf(ull mask){return 63 - __builtin_clzll(mask);}
mt19937_64 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
ll rngesus(ll l, ll r){return l + (ull) rng() % (r - l + 1);}
double rngesus_d(double l, double r){
double cur = rngesus(0, MASK(60) - 1);
cur /= MASK(60) - 1;
return l + cur * (r - l);
}
template <class T1, class T2>
bool maximize(T1 &a, T2 b){
if (a < b) {a = b; return true;}
return false;
}
template <class T1, class T2>
bool minimize(T1 &a, T2 b){
if (a > b) {a = b; return true;}
return false;
}
template <class T>
void printArr(T container, string separator = " ", string finish = "\n", ostream &out = cout){
for(auto item: container) out << item << separator;
out << finish;
}
template <class T>
void remove_dup(vector<T> &a){
sort(ALL(a));
a.resize(unique(ALL(a)) - a.begin());
}
const int MOD = 1e9;
const int N = 1e5 + 69;
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
int n;
vector<array<int, 3>> nodes;
vector<int> graph[N];
ll dp[N];
vector<int> flow_cnt[N];
void dfs(int u, int p){
flow_cnt[u].resize(nodes[u][2] - nodes[u][1] + 1, 1);
for(int v: graph[u]) if (v != p){
dfs(v, u);
dp[u] += dp[v];
vector<int> sigma = flow_cnt[v];
int l = 0, r = sigma.size() - 1;
l += max(0, nodes[u][1] - nodes[v][1]);
r -= max(0, nodes[v][2] - nodes[u][2]);
int sum = 0;
for(int i: sigma) sum += i;
for(int i = 0; i < l; ++i){
sigma[l] += sigma[i];
dp[u] += 1LL * sigma[i] * (l - i);
sigma[i] = 0;
}
for(int i = r + 1; i < (int)sigma.size(); ++i){
sigma[r] += sigma[i];
dp[u] += 1LL * sigma[i] * (i - r);
sigma[i] = 0;
}
for(int i = l; i<=r; ++i){
flow_cnt[u][i + nodes[v][1] - nodes[u][1]] += sigma[i];
}
dp[u] += sum;
}
if (p == -1){
int m = flow_cnt[u].size();
dp[u] *= m;
for(int i = 0; i<m; ++i){
ll fu = i * (i+1) / 2 + (m-i)*(m-i-1) / 2;
dp[u] += fu * flow_cnt[u][i];
}
}
}
int DistanceSum(int N, int *X, int *Y) {
n = N;
set<pair<int, int>> S;
for(int i = 0; i<n; ++i)
S.insert({X[i], Y[i]});
map<pair<int, int>, int> own;
nodes.clear();
ll ans = 0;
while(S.size()){
pair<int, int> cu = *S.begin();
S.erase(cu);
nodes.push_back({{cu.first, cu.second, cu.second}});
own[cu] = nodes.size() - 1;
int l = cu.second, r = cu.second;
while(S.size()){
r++;
auto it = S.find(make_pair(cu.first, r));
if (it != S.end()){
own[{cu.first, r}] = nodes.size() - 1;
S.erase(it);
nodes.back()[2]++;
}
else break;
}
while(S.size()){
l--;
auto it = S.find(make_pair(cu.first, l));
if (it != S.end()){
own[{cu.first, l}] = nodes.size() - 1;
S.erase(it);
nodes.back()[1]--;
}
else break;
}
}
int m = nodes.size();
for(int i = 0; i<m; ++i) graph[i].clear();
for(int i = 0; i<m; ++i){
for(int y = nodes[i][1]; y <= nodes[i][2]; ++y){
pair<int, int> cur = {nodes[i][0] - 1, y};
if (own.count(cur)){
graph[i].push_back(own[cur]);
graph[own[cur]].push_back(i);
}
cur.first += 2;
}
}
for(int i = 0; i<m; ++i) remove_dup(graph[i]);
for(int i= 0; i<m; ++i){
for(int j = 0; j < m; ++j) dp[j] = 0;
for(int j = 0; j < m; ++j) flow_cnt[j].clear();
dfs(i, -1);
ans += dp[i];
}
ans /= 2;
return ans % MOD;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |