#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define ll long long
#define itr ::iterator
typedef pair<ll,ll> pii;
const ll MAX=1e6;
const ll mod=1e9;
vector<pii> vec;
set<ll> adj[MAX];
ll res,value[MAX],sum[MAX],dp[MAX];
void dfs(int node,int par)
{
//cout<<node<<" "<<value[node]<<" dfs \n";
for(auto A:adj[node])
{
if(A==par)
continue;
dfs(A,node);
sum[node]+=sum[A];
sum[node]%=mod;
dp[node]+=dp[A];
dp[node]%=mod;
}
//cout<<"sum "<<node<<" "<<sum[node]<<"\n";
dp[node]+=sum[node];
dp[node]%=mod;
sum[node]+=value[node];
sum[node]%=mod;
res+=(dp[node]*value[node])%mod;
res%=mod;
return ;
}
void cal(int N,int *X,int *Y)
{
for(int A=1;A<=N;A++)
{
dp[A]=0;
sum[A]=0;
value[A]=0;
adj[A].clear();
}
int ptr=0;
vector<pii> vec;
map<pii,int> mark;
for(int A=0;A<N;A++)
vec.pb(mp(X[A],Y[A]));
vec.pb(mp(0,0));
sort(vec.begin(),vec.end());
for(int A=1;A<vec.size();A++)
{
if(vec[A].first==vec[A-1].first and vec[A].second==vec[A-1].second+1)
{
value[ptr]++;
mark[mp(vec[A].first,vec[A].second)]=ptr;
}
else
{
value[++ptr]++;
mark[mp(vec[A].first,vec[A].second)]=ptr;
}
//cout<<vec[A].first<<" "<<vec[A].second<<" "<<mark[vec[A]]<<"\n";
}
for(int A=1;A<vec.size();A++)
{
if(mark[mp(vec[A].first-1,vec[A].second)])
{
adj[mark[vec[A]]].insert(mark[mp(vec[A].first-1,vec[A].second)]);
adj[mark[mp(vec[A].first-1,vec[A].second)]].insert(mark[vec[A]]);
//cout<<mark[vec[A]]<<" "<<mark[mp(vec[A].first-1,vec[A].second)]<<"\n";
}
}
dfs(1,0);
return ;
}
int DistanceSum(int N, int *X, int *Y)
{
cal(N,X,Y);
for(int A=0;A<N;A++)
swap(X[A],Y[A]);
cal(N,X,Y);
return res;
}
/*signed main()
{
ios_base::sync_with_stdio(false);
int X[]={1,1,2,2};
int Y[]={1,2,1,2};
cout<<DistanceSum(4,X,Y);
return 0;
}*/
Compilation message
city.cpp: In function 'void cal(int, int*, int*)':
city.cpp:59:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int A=1;A<vec.size();A++)
~^~~~~~~~~~~
city.cpp:73:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int A=1;A<vec.size();A++)
~^~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
46 ms |
47352 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
59 ms |
47592 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
108 ms |
50132 KB |
Output is correct |
2 |
Correct |
107 ms |
50188 KB |
Output is correct |
3 |
Correct |
220 ms |
54000 KB |
Output is correct |
4 |
Correct |
197 ms |
54284 KB |
Output is correct |
5 |
Correct |
359 ms |
60604 KB |
Output is correct |
6 |
Correct |
406 ms |
61504 KB |
Output is correct |
7 |
Correct |
372 ms |
62516 KB |
Output is correct |
8 |
Correct |
356 ms |
62904 KB |
Output is correct |
9 |
Correct |
345 ms |
64112 KB |
Output is correct |
10 |
Correct |
352 ms |
69540 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
119 ms |
69540 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |