#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define f first
#define s second
#define x first
#define y second
#define elif else if
#define endl '\n'
#define all(x) begin(x), end(x)
#define MOD 1000000000
template <typename T>
ostream& operator << (ostream& os, const vector<T>& a) {
os << '[';
bool E = false;
for(const T& x : a) {
if(E) os << ' ';
os << x;
E = true;
}
return os << ']';
}
int n,*x,*y;
ll total;
unordered_map<ll,int> mp;
int done[100069][2];
ll convert(pair<int,int> p){
return ((ll)p.f<<32)+p.s;
}
int dfs(int node,int orientation){
if (done[node][orientation]) return 0;
done[node][orientation]=true;
int temp,cnt=0;
if (orientation) {
if (mp.find(convert({x[node]+1,y[node]}))!=mp.end()) {
cnt+=dfs(mp[convert({x[node]+1,y[node]})],orientation);
}
if (mp.find(convert({x[node]-1,y[node]}))!=mp.end()) {
cnt+=dfs(mp[convert({x[node]-1,y[node]})],orientation);
}
if (mp.find(convert({x[node],y[node]+1}))!=mp.end()) {
temp=dfs(mp[convert({x[node],y[node]+1})],orientation);
total+=(ll)temp*(n-temp);
cnt+=temp;
}
if (mp.find(convert({x[node],y[node]-1}))!=mp.end()) {
temp=dfs(mp[convert({x[node],y[node]-1})],orientation);
total+=(ll)temp*(n-temp);
cnt+=temp;
}
}
else{
if (mp.find(convert({x[node],y[node]+1}))!=mp.end()) {
cnt+=dfs(mp[convert({x[node],y[node]+1})],orientation);
}
if (mp.find(convert({x[node],y[node]-1}))!=mp.end()) {
cnt+=dfs(mp[convert({x[node],y[node]-1})],orientation);
}
if (mp.find(convert({x[node]+1,y[node]}))!=mp.end()) {
temp=dfs(mp[convert({x[node]+1,y[node]})],orientation);
total+=(ll)temp*(n-temp);
cnt+=temp;
}
if (mp.find(convert({x[node]-1,y[node]}))!=mp.end()) {
temp=dfs(mp[convert({x[node]-1,y[node]})],orientation);
total+=(ll)temp*(n-temp);
cnt+=temp;
}
}
return cnt+1;
}
int DistanceSum(int N, int *X, int *Y) {
n=N;x=X;y=Y;
for (int i = 0; i < n; i++) {
mp[((ll)x[i]<<32)+y[i]]=i;
}
dfs(0,0);dfs(0,1);
return total%MOD;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
320 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
22 ms |
3020 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
16 ms |
1816 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |