답안 #987728

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
987728 2024-05-23T12:58:32 Z emad234 이상적인 도시 (IOI12_city) C++17
100 / 100
175 ms 26792 KB
 #pragma once
#include "bits/stdc++.h"
#define F first
#define S second
#define ll long long
#define pii pair<ll,ll>
const int mxN = 2e5 + 5;
const int mod = 1e9;
using namespace std;
vector<vector<int>>v;
ll dpD[mxN],dpU[mxN];
ll subD[mxN],subU[mxN];
ll w[mxN];
int p[mxN];
ll ans;
ll n;
void dfs(int u,int par){
	subD[u] = w[u];
    p[u] = par;
	for(auto x : v[u]){
		if(x == par) continue;
		dfs(x,u);
		subD[u] += subD[x];
	}
    dpD[u] = (n - subD[u]) * subD[u];
}
ll solve(int N,int X[],int Y[]){
    ans = 0;
    for(int i = 0; i <= N;i++){
        dpD[i] = 0;
        dpU[i] = 0;
        subD[i] = 0;
        subU[i] = 0;
        w[i] = 0;
        p[i] = 0;
    }
    map<pii,int>mp;
    map<pii,bool>vis;
    for(auto &x : v) x.clear();
    v.clear();
    v.resize(N + 1);
    int id = 0;
    vector<pii>a;
    for(int i = 0;i < N;i++){
        a.push_back({X[i],Y[i]});
    }
    sort(a.begin(),a.end());
    int pX = -1,pY = -1;
    for(auto x : a){
        if(x.F != pX || x.S != pY + 1){
            id++;
        }
        mp[x] = id;
        w[id]++;
        if(mp[{x.F - 1,x.S}] && !vis[{mp[x],mp[{x.F - 1,x.S}]}]){
            v[mp[{x.F - 1,x.S}]].push_back(mp[x]);
            v[mp[x]].push_back(mp[{x.F - 1,x.S}]);
            vis[{mp[x],mp[{x.F - 1,x.S}]}] = 1;
        }
        pX = x.F;
        pY = x.S;
    }
    // for(int i = 1;i < 7;i++){
    //     cout<<i<<' '<<w[i]<<"{ ";
    //     for(auto x : v[i]){
    //         cout<<x<<' ';
    //     }
    //     cout<<"}\n";
    // }
    dfs(1,0);
    for(int i = 1;i <= N;i++){
        ans += dpD[i];
        // cout<<dpD[i]<<' ';
    }
    // cout<<'\n';
    // for(int i = 1;i <= N;i++){
    //     cout<<dpU[i]<<' ';
    // }
    // cout<<'\n';
    // cout<<'\n';
    // ans /= 2;
    // ans %= mod;
    return ans;
}
int DistanceSum(int N, int X[], int Y[]){
    n = N;
    return (((solve(N,Y,X) + solve(N,X,Y))) % mod);
}

Compilation message

city.cpp:1:10: warning: #pragma once in main file
    1 |  #pragma once
      |          ^~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 8540 KB Output is correct
2 Correct 2 ms 8540 KB Output is correct
3 Correct 3 ms 8540 KB Output is correct
4 Correct 2 ms 8540 KB Output is correct
5 Correct 3 ms 8540 KB Output is correct
6 Correct 2 ms 8540 KB Output is correct
7 Correct 2 ms 8540 KB Output is correct
8 Correct 2 ms 8540 KB Output is correct
9 Correct 2 ms 8644 KB Output is correct
10 Correct 2 ms 8540 KB Output is correct
11 Correct 2 ms 8540 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 8792 KB Output is correct
2 Correct 3 ms 8796 KB Output is correct
3 Correct 4 ms 8796 KB Output is correct
4 Correct 3 ms 8796 KB Output is correct
5 Correct 4 ms 8796 KB Output is correct
6 Correct 4 ms 8796 KB Output is correct
7 Correct 4 ms 8924 KB Output is correct
8 Correct 4 ms 8796 KB Output is correct
9 Correct 4 ms 8796 KB Output is correct
10 Correct 4 ms 8792 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 20 ms 11160 KB Output is correct
2 Correct 19 ms 11108 KB Output is correct
3 Correct 50 ms 14692 KB Output is correct
4 Correct 46 ms 14680 KB Output is correct
5 Correct 97 ms 20400 KB Output is correct
6 Correct 99 ms 20440 KB Output is correct
7 Correct 96 ms 20508 KB Output is correct
8 Correct 96 ms 20276 KB Output is correct
9 Correct 97 ms 20340 KB Output is correct
10 Correct 127 ms 25508 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 29 ms 12428 KB Output is correct
2 Correct 26 ms 11920 KB Output is correct
3 Correct 74 ms 17644 KB Output is correct
4 Correct 64 ms 16572 KB Output is correct
5 Correct 168 ms 26520 KB Output is correct
6 Correct 122 ms 22908 KB Output is correct
7 Correct 175 ms 26792 KB Output is correct
8 Correct 124 ms 23024 KB Output is correct
9 Correct 121 ms 22060 KB Output is correct
10 Correct 134 ms 21924 KB Output is correct