#include<iostream>
#include<vector>
#include<limits.h>
#include<unordered_set>
#include<map>
using namespace std;
typedef long long ll;
const int MOD=1e9;
const int BIG=INT_MAX;
int w,h;
vector<unordered_set<int>>cols;
map<pair<int,int>,bool>vis;
struct Tree{
int val=0;
vector<Tree*>ch;
int get(){
vector<int>vals;
for(auto c:ch){
auto r=c->get_sum();
vals.push_back(r.first+r.second);
}
int res=0;
for(int i=0;i<(int)vals.size();i++){
for(int j=i-1;j>=0;j--){
res=(res+(ll)vals[i]*vals[j]*2)%MOD;
}
}
for(int i:vals)
res=(res+(ll)i*val)%MOD;
for(auto c:ch){
res+=c->get();
res%=MOD;
}
return res;
}
pair<int,int>get_sum(){ // (num_nodes,sum_of_paths)
pair<int,int>res={val,0};
for(auto c:ch){
auto r=c->get_sum();
res.first+=r.first;
res.second+=(r.second+r.first)%MOD;
res.second%=MOD;
}
return res;
}
};
Tree*get_tree(int x,int y){
if(vis[{x,y}])
return nullptr;
if(x<0||x>=w)
return nullptr;
if(cols[x].find(y)==cols[x].end())
return nullptr;
vis[{x,y}]=true;
Tree*res=new Tree;
res->val=1;
int cy=y-1;
while(cols[x].find(cy)!=cols[x].end()){
vis[{x,cy}]=true;
cy--;
res->val++;
}
cy=y+1;
while(cols[x].find(cy)!=cols[x].end()){
vis[{x,cy}]=true;
cy++;
res->val++;
}
{
auto r=get_tree(x+1,y);
if(r)
res->ch.push_back(r);
}
{
auto r=get_tree(x-1,y);
if(r)
res->ch.push_back(r);
}
cy=y-1;
while(cols[x].find(cy)!=cols[x].end()){
{
auto r=get_tree(x+1,cy);
if(r)
res->ch.push_back(r);
}
{
auto r=get_tree(x-1,cy);
if(r)
res->ch.push_back(r);
}
cy--;
}
cy=y+1;
while(cols[x].find(cy)!=cols[x].end()){
{
auto r=get_tree(x+1,cy);
if(r)
res->ch.push_back(r);
}
{
auto r=get_tree(x-1,cy);
if(r)
res->ch.push_back(r);
}
cy++;
}
return res;
}
int get_dist(vector<pair<int,int>>vec){
w=0;
h=0;
for(auto i:vec){
w=max(w,i.first+1);
h=max(h,i.second+1);
}
cols.clear();
cols.resize(w);
vis.clear();
for(auto i:vec)
cols[i.first].insert(i.second);
Tree*tree=get_tree(0,*cols[0].begin());
int r=tree->get();
return r;
}
int DistanceSum(int N,int *X,int *Y){
int mx=BIG,my=BIG;
for(int i=0;i<N;i++){
mx=min(mx,X[i]);
my=min(my,Y[i]);
}
vector<pair<int,int>>vec1,vec2;
for(int i=0;i<N;i++){
vec1.emplace_back(X[i]-mx,Y[i]-my);
vec2.emplace_back(Y[i]-my,X[i]-mx);
}
return(get_dist(vec1)+get_dist(vec2))%MOD;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
604 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
16 ms |
3720 KB |
Output is correct |
2 |
Correct |
19 ms |
3720 KB |
Output is correct |
3 |
Correct |
45 ms |
8136 KB |
Output is correct |
4 |
Correct |
50 ms |
8908 KB |
Output is correct |
5 |
Correct |
103 ms |
16100 KB |
Output is correct |
6 |
Correct |
104 ms |
17340 KB |
Output is correct |
7 |
Correct |
152 ms |
16776 KB |
Output is correct |
8 |
Correct |
81 ms |
16060 KB |
Output is correct |
9 |
Correct |
173 ms |
16952 KB |
Output is correct |
10 |
Execution timed out |
1069 ms |
25024 KB |
Time limit exceeded |
11 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
136 ms |
5652 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |