#include <bits/stdc++.h>
#define f first
#define s second
using namespace std;
typedef int ll;
typedef pair<ll,ll> pi;
//간선 연결후 tree dp해보자
ll x[100005],y[100005],sz[100005],num[100005],cnt,anw,n;
const long long mod=1e9;
vector<ll> g[100005];
vector<pi> ar;
ll dfs(ll a,ll p)
{
ll ret=sz[a];
for(int i=0;i<g[a].size();i++)
if(g[a][i]!=p)
ret+=dfs(g[a][i],a);
anw=(anw+(long long)ret*(n-ret))%mod;
return ret;
}
void add(ll a,ll b)
{
g[a].push_back(b),g[b].push_back(a);
}
void calc()
{
sort(ar.begin(),ar.end()); //점 0부터.
for(int i=0;i<=n;i++)
g[i].clear(),sz[i]=0;
cnt=0;
num[0]=++cnt,sz[cnt]=1;
for(int i=1;i<ar.size();i++)
{
if(ar[i-1].s+1==ar[i].s) num[i]=cnt;
else num[i]=++cnt;
sz[cnt]++;
}
ll j=0,i=0;
while(ar[j].f!=ar[0].f) j++;
while(j<n)
{
while(j<n&&ar[i].f+1!=ar[j].f)
{
if(ar[i].f>=ar[j].f) j++;
else i++;
}
while(j<n&&ar[i].f+1==ar[j].f&&ar[i].s!=ar[j].s)
{
if(ar[i].s>ar[j].s) j++;
else i++;
}
ll ad=j;
if(num[i]!=num[j])
add(num[i],num[j]),j++;
while(num[j]==num[ad]) j++;
}
dfs(1,-1);
}
int DistanceSum (int N, int *X, int *Y)
{
ios::sync_with_stdio(false);
cin.tie(NULL);
n=N;
for(int i=1;i<=n;i++)
x[i]=X[i-1],y[i]=Y[i-1];
for(int i=1;i<=n;i++)
ar.push_back({x[i],y[i]});
calc();
for(int i=0;i<n;i++)
swap(ar[i].f,ar[i].s);
calc();
return anw;
}
Compilation message
city.cpp: In function 'll dfs(ll, ll)':
city.cpp:18:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<g[a].size();i++)
~^~~~~~~~~~~~
city.cpp: In function 'void calc()':
city.cpp:37:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=1;i<ar.size();i++)
~^~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
4 ms |
2680 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
4 ms |
2680 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
3576 KB |
Output is correct |
2 |
Correct |
13 ms |
3704 KB |
Output is correct |
3 |
Correct |
27 ms |
4596 KB |
Output is correct |
4 |
Correct |
27 ms |
4696 KB |
Output is correct |
5 |
Correct |
49 ms |
6168 KB |
Output is correct |
6 |
Correct |
51 ms |
6264 KB |
Output is correct |
7 |
Correct |
50 ms |
6256 KB |
Output is correct |
8 |
Correct |
50 ms |
6128 KB |
Output is correct |
9 |
Correct |
51 ms |
6256 KB |
Output is correct |
10 |
Correct |
52 ms |
8432 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
14 ms |
3576 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |