#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;
pair<int,int>sum;
bool sumt=false;
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)
if(sumt)
return sum;
sum={val,0};
for(auto c:ch){
auto r=c->get_sum();
sum.first+=r.first;
sum.second+=(r.second+r.first)%MOD;
sum.second%=MOD;
}
sumt=true;
return sum;
}
};
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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
604 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
19 ms |
3396 KB |
Output is correct |
2 |
Correct |
16 ms |
3856 KB |
Output is correct |
3 |
Correct |
40 ms |
7916 KB |
Output is correct |
4 |
Correct |
42 ms |
8396 KB |
Output is correct |
5 |
Correct |
84 ms |
15420 KB |
Output is correct |
6 |
Correct |
86 ms |
16572 KB |
Output is correct |
7 |
Correct |
100 ms |
15944 KB |
Output is correct |
8 |
Correct |
83 ms |
15292 KB |
Output is correct |
9 |
Correct |
99 ms |
16216 KB |
Output is correct |
10 |
Correct |
128 ms |
27580 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
29 ms |
5768 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |