이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <vector>
using namespace std;
// BEGIN NO SAD
#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define trav(a, x) for(auto& a : x)
#define all(x) x.begin(), x.end()
#define sz(x) (int)(x).size()
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define lb lower_bound
#define ub upper_bound
typedef vector<int> vi;
#define f first
#define s second
// END NO SAD
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<vector<ll>> matrix;
vector<int> edges[100005];
int weight[100005];
ll dfs(int sz, int curr, int par) {
ll ret = 0;
for(int out: edges[curr]) {
if(out == par) continue;
ret += dfs(sz, out, curr);
ret += weight[out] * (ll)(sz - weight[out]);
weight[curr] += weight[out];
}
return ret;
}
ll solve(vector<pii>& v) {
sort(all(v));
int n = sz(v);
for(int i = 0; i < n; i++) edges[i].clear();
memset(weight, 0, sizeof(weight));
vector<array<int, 3>> intervals; // x, yl, yr
for(int i = 0; i < n;) {
int j = i+1;
while(j < n && v[i].f == v[j].f && v[j].s - v[i].s == j - i) j++;
weight[sz(intervals)] = j-i;
intervals.pb({v[i].f, v[i].s, v[j-1].s});
i = j;
}
for(int a = 0; a < sz(intervals);) {
int b = a+1;
while(b < sz(intervals) && intervals[a][0] == intervals[b][0]) b++;
if(b == sz(intervals)) break;
// [a, b)
int c = b+1;
while(c < sz(intervals) && intervals[b][0] == intervals[c][0]) c++;
int lhs = a;
int rhs = b;
while(lhs < b && rhs < c) {
if(intervals[lhs][2] <= intervals[rhs][2]) {
if(intervals[rhs][1] <= intervals[lhs][2]) {
edges[lhs].pb(rhs);
edges[rhs].pb(lhs);
}
lhs++;
}
else {
if(intervals[lhs][1] <= intervals[rhs][2]) {
edges[lhs].pb(rhs);
edges[rhs].pb(lhs);
}
rhs++;
}
}
a = b;
}
return dfs(n, 0, -1);
}
int DistanceSum(int n, int *X, int *Y) {
vector<pii> v(n);
for(int i = 0; i < n; i++) v[i] = {X[i], Y[i]};
ll ret = solve(v);
for(auto& x: v) swap(x.f, x.s);
ret += solve(v);
return ret%1000000000;
}
/*
void solve() {
int n;
cin >> n;
vector<pii> v(n);
for(auto& x: v) cin >> x.f >> x.s;
ll ret = solve(v);
for(auto& x: v) swap(x.f, x.s);
ret += solve(v);
cout << ret%1000000000 << "\n";
}
// what would chika do
// are there edge cases (N=1?)
// are array sizes proper (scaled by proper constant, for example 2* for koosaga tree)
// integer overflow?
// DS reset properly between test cases
// are you doing geometry in floating points
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
solve();
}
*/
# | 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... |