Submission #819855

# Submission time Handle Problem Language Result Execution time Memory
819855 2023-08-10T14:19:29 Z Ahmed57 Ideal city (IOI12_city) C++17
100 / 100
127 ms 20516 KB
#include <bits/stdc++.h>

using namespace std;
vector<int> adj[100001];
long long sz[100001];
long long n;
long long all = 0;
long long mod = 1000000000;
void dfs(int i,int pr){
    for(auto j:adj[i]){
        if(j==pr)continue;
        dfs(j,i);
        all+=(sz[j]*(n-sz[j]))%mod;
        all%=mod;
        sz[i]+=sz[j];
    }
}
void calc(int *X,int *Y){
    map<pair<int,int>,int> mp,ed;
    vector<pair<int,int>> v;
    for(int i =0;i<n;i++){
        v.push_back({X[i],Y[i]});
    }
    sort(v.begin(),v.end());
    for(int i = 0;i<n;i++){
        mp[{v[i].first,v[i].second}] = i+1;
    }
    int ans[n];int nod = 0;
    for(int i = 0;i<n;i++){
        if(i&&v[i].first==v[i-1].first&&v[i-1].second+1==v[i].second){
            ans[i] = ans[i-1];
        }else {
            adj[nod].clear();
            sz[nod] = 0;
            ans[i] = nod++;
        }
        sz[ans[i]]++;
        int lol = mp[{v[i].first-1,v[i].second}];
        if(lol&&ed[{ans[i],ans[lol-1]}]==0){
            ed[{ans[i],ans[lol-1]}] = 1;
            ed[{ans[lol-1],ans[i]}] = 1;
            adj[ans[i]].push_back(ans[lol-1]);
            adj[ans[lol-1]].push_back(ans[i]);
        }
    }
    dfs(0,-1);
}
int DistanceSum(int N,int *X,int *Y){
    all = 0;
    n = N;
    calc(X,Y);
    calc(Y,X);
    return all;
}
/*int main(){
    //ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    cout<<DistanceSum(11,{2,2,3,3,4,4,4,4,5,5,5},{5,6,3,6,3,4,5,6,3,4,6});
}*/
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2644 KB Output is correct
2 Correct 1 ms 2644 KB Output is correct
3 Correct 1 ms 2644 KB Output is correct
4 Correct 1 ms 2644 KB Output is correct
5 Correct 1 ms 2644 KB Output is correct
6 Correct 2 ms 2644 KB Output is correct
7 Correct 1 ms 2644 KB Output is correct
8 Correct 2 ms 2644 KB Output is correct
9 Correct 1 ms 2644 KB Output is correct
10 Correct 1 ms 2644 KB Output is correct
11 Correct 1 ms 2644 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2772 KB Output is correct
2 Correct 2 ms 2772 KB Output is correct
3 Correct 3 ms 2900 KB Output is correct
4 Correct 2 ms 2772 KB Output is correct
5 Correct 3 ms 2900 KB Output is correct
6 Correct 3 ms 2900 KB Output is correct
7 Correct 4 ms 3028 KB Output is correct
8 Correct 3 ms 2900 KB Output is correct
9 Correct 3 ms 2772 KB Output is correct
10 Correct 3 ms 2772 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 15 ms 4528 KB Output is correct
2 Correct 16 ms 4536 KB Output is correct
3 Correct 40 ms 6988 KB Output is correct
4 Correct 41 ms 7156 KB Output is correct
5 Correct 86 ms 11360 KB Output is correct
6 Correct 86 ms 11456 KB Output is correct
7 Correct 92 ms 11708 KB Output is correct
8 Correct 90 ms 11192 KB Output is correct
9 Correct 89 ms 11708 KB Output is correct
10 Correct 102 ms 18472 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 23 ms 6316 KB Output is correct
2 Correct 20 ms 5572 KB Output is correct
3 Correct 61 ms 11576 KB Output is correct
4 Correct 51 ms 9772 KB Output is correct
5 Correct 127 ms 20212 KB Output is correct
6 Correct 98 ms 14688 KB Output is correct
7 Correct 121 ms 20516 KB Output is correct
8 Correct 99 ms 15036 KB Output is correct
9 Correct 100 ms 14044 KB Output is correct
10 Correct 96 ms 13600 KB Output is correct