#include<bits/stdc++.h>
using namespace std;
using i32 = int;
#define int long long
#define pii pair<int,int>
#define piii pair<pii,int>
#define fi first
#define se second
const int maxn = 1e5+5;
const int mod = 1e9;
int sx,sy;
vector<int> cx,cy;
vector<int> pos[maxn];
int sum[maxn],cnt[maxn],ls[maxn],lc[maxn],rs[maxn],rc[maxn];
int nsum[maxn],ncnt[maxn];
i32 DistanceSum(i32 N, i32 *X, i32 *Y) {
for(int i=0;i<N;i++){
cx.push_back(X[i]);
cy.push_back(Y[i]);
}
sort(cx.begin(),cx.end());
sort(cy.begin(),cy.end());
cx.erase(unique(cx.begin(),cx.end()),cx.end());
cy.erase(unique(cy.begin(),cy.end()),cy.end());
sx=(int)cx.size();sy=(int)cy.size();
for(int i=0;i<N;i++){
X[i]=lower_bound(cx.begin(),cx.end(),X[i])-cx.begin()+1;
Y[i]=lower_bound(cy.begin(),cy.end(),Y[i])-cy.begin()+1;
}
auto cal = [&](){
vector<piii> pre;
for(int t=1;t<=sx;t++) pos[t].clear();
for(int i=0;i<N;i++) pos[X[i]].push_back(Y[i]);
int cnt=0;
vector<int> val;
vector<vector<int>> edge;
for(int t=1;t<=sx;t++){
sort(pos[t].begin(),pos[t].end());
vector<piii> cur;
for(int y:pos[t]){
if(cur.empty() || cur.back().fi.se+1<y){
cur.push_back({{y,y},cnt++});
edge.push_back({});val.push_back(1);
}
else cur.back().fi.se++,val[cur.back().se]++;
}
int p=0;
for(auto [x,u]:cur){
auto [l,r]=x;
//cout << t << ' ' << l << ' ' << r << ' ' << u << '\n';
auto add = [&](int lt,int rt){
int L=max(l,lt),R=min(r,rt);
return (L<=R);
};
while(p<(int)pre.size() && pre[p].fi.se<=r){
auto [xt,v]=pre[p++];
if(add(xt.fi,xt.se)){
edge[u].push_back(v);
edge[v].push_back(u);
//cout << "edge " << u << ' ' << v << '\n';
}
}
if(p<(int)pre.size() && add(pre[p].fi.fi,pre[p].fi.se)){
edge[u].push_back(pre[p].se);
edge[pre[p].se].push_back(u);
//cout << "edge " << u << ' ' << pre[p].se << '\n';
}
}
pre=cur;
}
int res=0;
function<void(int,int)> dfs = [&](int u,int p){
for(int v:edge[u]){
if(v!=p) dfs(v,u),val[u]+=val[v];
}
res=(res+val[u]*(N-val[u]))%mod;
};
dfs(0,-1);
//cout << '\n';
return res;
};
int total=cal();
swap(sx,sy);
for(int i=0;i<N;i++) swap(X[i],Y[i]);
total=(total+cal())%mod;
return total;
}
#undef int
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4700 KB |
Output is correct |
2 |
Correct |
1 ms |
4700 KB |
Output is correct |
3 |
Correct |
1 ms |
4700 KB |
Output is correct |
4 |
Correct |
1 ms |
4700 KB |
Output is correct |
5 |
Correct |
1 ms |
4700 KB |
Output is correct |
6 |
Correct |
1 ms |
4700 KB |
Output is correct |
7 |
Correct |
1 ms |
4700 KB |
Output is correct |
8 |
Correct |
1 ms |
4952 KB |
Output is correct |
9 |
Correct |
1 ms |
4700 KB |
Output is correct |
10 |
Correct |
1 ms |
4700 KB |
Output is correct |
11 |
Correct |
1 ms |
4780 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4700 KB |
Output is correct |
2 |
Correct |
1 ms |
4700 KB |
Output is correct |
3 |
Correct |
1 ms |
4700 KB |
Output is correct |
4 |
Correct |
2 ms |
4700 KB |
Output is correct |
5 |
Correct |
2 ms |
4956 KB |
Output is correct |
6 |
Correct |
2 ms |
4700 KB |
Output is correct |
7 |
Correct |
2 ms |
4956 KB |
Output is correct |
8 |
Correct |
2 ms |
4700 KB |
Output is correct |
9 |
Correct |
2 ms |
4792 KB |
Output is correct |
10 |
Correct |
2 ms |
4700 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
5768 KB |
Output is correct |
2 |
Correct |
12 ms |
6024 KB |
Output is correct |
3 |
Correct |
19 ms |
7120 KB |
Output is correct |
4 |
Correct |
19 ms |
7632 KB |
Output is correct |
5 |
Correct |
36 ms |
9416 KB |
Output is correct |
6 |
Correct |
37 ms |
10688 KB |
Output is correct |
7 |
Correct |
38 ms |
10428 KB |
Output is correct |
8 |
Correct |
36 ms |
9420 KB |
Output is correct |
9 |
Correct |
53 ms |
10176 KB |
Output is correct |
10 |
Correct |
46 ms |
14580 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
6348 KB |
Output is correct |
2 |
Correct |
9 ms |
6424 KB |
Output is correct |
3 |
Correct |
24 ms |
8844 KB |
Output is correct |
4 |
Correct |
22 ms |
8124 KB |
Output is correct |
5 |
Correct |
62 ms |
12916 KB |
Output is correct |
6 |
Correct |
45 ms |
10688 KB |
Output is correct |
7 |
Correct |
49 ms |
12888 KB |
Output is correct |
8 |
Correct |
43 ms |
10944 KB |
Output is correct |
9 |
Correct |
42 ms |
10156 KB |
Output is correct |
10 |
Correct |
40 ms |
10432 KB |
Output is correct |