#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int mod=1000000000;
int add(int a,int b){return a+b-(a+b>=mod?mod:0);}
int mul(int a,int b){return (ll(a)*b)%mod;}
int res=0;
const int N=100000;
int n;
vector<array<int,2>> tree[4*N];
vector<array<int,4>> upd[4*N];
int p[N];
int sz[N];
int events=0;
int find_set(int a)
{
if(a==p[a]) return a;
else return find_set(p[a]);
}
void merge_sets(int a,int b,vector<array<int,4>> &u)
{
a=find_set(a);
b=find_set(b);
if(a==b) return;
if(sz[a]<sz[b]) swap(a,b);
u.push_back({b,p[b],a,sz[a]});
p[b]=a;
sz[a]+=sz[b];
}
void apply(int idx,int l,int r,int ql,int qr,int a,int b)
{
if(ql>qr) return;
if(l==ql&&r==qr) tree[idx].push_back({a,b});
else
{
int m=(l+r)/2;
apply(2*idx,l,m,ql,min(qr,m),a,b);
apply(2*idx+1,m+1,r,max(ql,m+1),qr,a,b);
}
}
void process(int idx,int l,int r)
{
for(auto [a,b]:tree[idx]) merge_sets(a,b,upd[idx]);
if(l<r)
{
int m=(l+r)/2;
process(2*idx,l,m);
process(2*idx+1,m+1,r);
}
else res=add(res,mul(sz[find_set(0)],n-sz[find_set(0)]));
reverse(upd[idx].begin(),upd[idx].end());
for(auto [a,b,c,d]:upd[idx]){p[a]=b; sz[c]=d;}
}
void go(int *X,int *Y)
{
vector<array<int,2>> blocks(n);
for(int i=0;i<n;i++) blocks[i]={X[i],Y[i]};
for(int i=0;i<n;i++)
{
p[i]=i;
sz[i]=1;
}
events=0;
map<array<int,2>,int> m;
for(int i=0;i<n;i++) m[{X[i],Y[i]}]=i;
auto id=[&](int x,int y)
{
if(m.find({x,y})!=m.end()) return m[{x,y}];
else return -1;
};
vector<array<int,4>> v;
for(int i=0;i<n;i++)
{
int t=id(X[i]+1,Y[i]);
if(t!=-1) v.push_back({X[i],Y[i],i,t});
int j=id(X[i],Y[i]+1);
if(j!=-1) merge_sets(i,j,upd[0]);
}
sort(v.begin(),v.end());
int len=v.size();
vector<array<int,2>> groups;
int l=0;
while(l<len)
{
int r=l;
while(r+1<len&&v[r+1][0]==v[l][0]&&v[r][1]+1==v[r+1][1]) r++;
groups.push_back({l,r});
events++;
l=r+1;
}
for(int i=1;i<=events;i++)
{
auto [one,two]=groups[i-1];
for(int j=one;j<=two;j++)
{
if(i-1>=1) apply(1,1,events,1,i-1,v[j][2],v[j][3]);
if(i+1<=events) apply(1,1,events,i+1,events,v[j][2],v[j][3]);
}
}
process(1,1,events);
//spring cleaning
for(int i=0;i<4*N;i++)
{
tree[i].clear();
upd[i].clear();
}
}
int DistanceSum(int _n,int *x,int *y)
{
n=_n;
go(x,y);
go(y,x);
return res;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
19028 KB |
Output is correct |
2 |
Correct |
13 ms |
19096 KB |
Output is correct |
3 |
Correct |
11 ms |
19100 KB |
Output is correct |
4 |
Correct |
11 ms |
19028 KB |
Output is correct |
5 |
Correct |
12 ms |
19056 KB |
Output is correct |
6 |
Correct |
13 ms |
19144 KB |
Output is correct |
7 |
Correct |
13 ms |
19028 KB |
Output is correct |
8 |
Correct |
12 ms |
19144 KB |
Output is correct |
9 |
Correct |
12 ms |
19144 KB |
Output is correct |
10 |
Correct |
12 ms |
19028 KB |
Output is correct |
11 |
Correct |
12 ms |
19108 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
19364 KB |
Output is correct |
2 |
Correct |
12 ms |
19284 KB |
Output is correct |
3 |
Correct |
15 ms |
19468 KB |
Output is correct |
4 |
Correct |
15 ms |
19412 KB |
Output is correct |
5 |
Correct |
17 ms |
19720 KB |
Output is correct |
6 |
Correct |
17 ms |
19540 KB |
Output is correct |
7 |
Correct |
19 ms |
19596 KB |
Output is correct |
8 |
Correct |
17 ms |
19484 KB |
Output is correct |
9 |
Correct |
17 ms |
19540 KB |
Output is correct |
10 |
Correct |
16 ms |
19532 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
68 ms |
23740 KB |
Output is correct |
2 |
Correct |
65 ms |
24012 KB |
Output is correct |
3 |
Correct |
184 ms |
30572 KB |
Output is correct |
4 |
Correct |
163 ms |
31412 KB |
Output is correct |
5 |
Correct |
387 ms |
42624 KB |
Output is correct |
6 |
Correct |
362 ms |
44156 KB |
Output is correct |
7 |
Correct |
398 ms |
49500 KB |
Output is correct |
8 |
Correct |
368 ms |
40936 KB |
Output is correct |
9 |
Correct |
436 ms |
44632 KB |
Output is correct |
10 |
Correct |
330 ms |
54116 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
84 ms |
25268 KB |
Output is correct |
2 |
Correct |
82 ms |
25172 KB |
Output is correct |
3 |
Correct |
183 ms |
36788 KB |
Output is correct |
4 |
Correct |
226 ms |
35256 KB |
Output is correct |
5 |
Correct |
413 ms |
55708 KB |
Output is correct |
6 |
Correct |
422 ms |
49180 KB |
Output is correct |
7 |
Correct |
417 ms |
55724 KB |
Output is correct |
8 |
Correct |
413 ms |
49168 KB |
Output is correct |
9 |
Correct |
463 ms |
49144 KB |
Output is correct |
10 |
Correct |
441 ms |
48684 KB |
Output is correct |