답안 #702373

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
702373 2023-02-23T19:13:58 Z Ronin13 이상적인 도시 (IOI12_city) C++14
100 / 100
172 ms 61732 KB
#include <bits/stdc++.h>
#define ll long long
#define ull unsigned ll
#define f first
#define s second
#define pii pair <int, int>
#define pll pair<ll,ll>
#define pb push_back
#define epb emplace_back
using namespace std;
const ll mod = 1e18;
map <pll, int> mp;
const int nmax = 1000001;
vector <vector <int> > g(nmax);
vector <ll> dp(nmax);
ll ans = 0;
vector <ll> sz(nmax);
vector <ll> a(nmax);

int cnt = 1;

/*
11
2 5
2 6
3 3
4 3
4 4
4 5
4 6

5 3
5 4
5 6
3 6

*/
ll add(ll a, ll b){
    return (a + b) % mod;
}

ll subs(ll a, ll b){
    return (a - b + mod) % mod;
}

ll mult(ll a, ll b){
    return a * b % mod;
}

void dfs(int v, int e = -1){
    sz[v] = a[v];
    dp[v] = 0;
    for(int to : g[v]){
        if(to == e) continue;
        dfs(to, v);
        dp[v] = add(dp[v], dp[to]);
        dp[v] = add(dp[v], sz[to]);
        sz[v] = add(sz[v], sz[to]);
    }
}

void DFS(int v, int e = -1){
    ans = add(ans, mult(dp[v], a[v]));
    for(int to : g[v]){
        if(to == e) continue;
        ll pr1 = dp[to], pr2 = sz[to];
        sz[v] = subs(sz[v], sz[to]);
        dp[v] = subs(dp[v], add(dp[to], sz[to]));
        dp[to] = add(dp[to], add(dp[v], sz[v]));
        sz[to] = add(sz[v], sz[to]);
        DFS(to, v);
        sz[to] = pr2;
        dp[to] = pr1;
        sz[v] = add(sz[v], sz[to]);
        dp[v] = add(dp[v], add(dp[to],sz[to]));
    }
}

int DistanceSum(int N, int *X, int *Y) {
    vector <pii > vec;
    int n = N;
    for(int i = 0; i < n ;i++){
        vec.pb({X[i], Y[i]});
    }
    sort(vec.begin(), vec.end());
    for(int i = 0; i < vec.size(); i++){
        if(i == 0){
            mp[vec[i]] = cnt++;
            a[cnt - 1] = 1;
            continue;
        }
        ll x = vec[i].f, y = vec[i].s;
        if(!mp[{x, y - 1}]) mp[{x, y}] = cnt++, a[cnt - 1]++;
        else mp[{x, y}] = cnt - 1, a[cnt - 1]++;
        if(mp[{x - 1, y}]){
            if((!mp[{x - 1, y - 1}]) || (!mp[{x, y - 1}])){
                int u = mp[{x, y}];
                int v = mp[{x - 1, y}];
                g[u].pb(v);
                g[v].pb(u);
            }
        }
    }
    //cout << 1;
    dfs(1);
    DFS(1);
    for(int i = 0; i < cnt; i++){
        g[i].clear();
        a[i] = 0;
    }
    vec.clear();
    mp.clear();
    for(int i = 0; i < n ;i++){
        vec.pb({Y[i], X[i]});
    }
    cnt = 1;
    sort(vec.begin(), vec.end());
    for(int i = 0; i < vec.size(); i++){
        if(i == 0){
            mp[vec[i]] = cnt++;
            a[cnt - 1] =1;
            continue;
        }
        ll x = vec[i].f, y = vec[i].s;
        if(!mp[{x, y - 1}]) mp[{x, y}] = cnt++, a[cnt - 1]++;
        else mp[{x, y}] = cnt- 1, a[cnt - 1]++;
        if(mp[{x - 1, y}]){
            if((!mp[{x - 1, y - 1}]) || (!mp[{x, y - 1}])){
                int u = mp[{x, y}];
                int v = mp[{x - 1, y}];
                g[u].pb(v);
                g[v].pb(u);
            }
        }
    }
    dfs(1);
    DFS(1);
    return (ans / 2) % (ll)(1e9);
}

Compilation message

city.cpp: In function 'int DistanceSum(int, int*, int*)':
city.cpp:86:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   86 |     for(int i = 0; i < vec.size(); i++){
      |                    ~~^~~~~~~~~~~~
city.cpp:118:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  118 |     for(int i = 0; i < vec.size(); i++){
      |                    ~~^~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 21 ms 47188 KB Output is correct
2 Correct 20 ms 47188 KB Output is correct
3 Correct 20 ms 47260 KB Output is correct
4 Correct 20 ms 47188 KB Output is correct
5 Correct 20 ms 47204 KB Output is correct
6 Correct 20 ms 47204 KB Output is correct
7 Correct 20 ms 47188 KB Output is correct
8 Correct 20 ms 47316 KB Output is correct
9 Correct 20 ms 47188 KB Output is correct
10 Correct 20 ms 47188 KB Output is correct
11 Correct 20 ms 47188 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 21 ms 47404 KB Output is correct
2 Correct 25 ms 47312 KB Output is correct
3 Correct 21 ms 47476 KB Output is correct
4 Correct 22 ms 47424 KB Output is correct
5 Correct 25 ms 47508 KB Output is correct
6 Correct 22 ms 47412 KB Output is correct
7 Correct 22 ms 47572 KB Output is correct
8 Correct 23 ms 47472 KB Output is correct
9 Correct 22 ms 47416 KB Output is correct
10 Correct 22 ms 47388 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 39 ms 48964 KB Output is correct
2 Correct 39 ms 48984 KB Output is correct
3 Correct 72 ms 51680 KB Output is correct
4 Correct 72 ms 51740 KB Output is correct
5 Correct 127 ms 56008 KB Output is correct
6 Correct 126 ms 56160 KB Output is correct
7 Correct 126 ms 56392 KB Output is correct
8 Correct 127 ms 55960 KB Output is correct
9 Correct 131 ms 56392 KB Output is correct
10 Correct 130 ms 61732 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 47 ms 50040 KB Output is correct
2 Correct 47 ms 49916 KB Output is correct
3 Correct 92 ms 54336 KB Output is correct
4 Correct 105 ms 53404 KB Output is correct
5 Correct 171 ms 61368 KB Output is correct
6 Correct 143 ms 58288 KB Output is correct
7 Correct 172 ms 61720 KB Output is correct
8 Correct 146 ms 58288 KB Output is correct
9 Correct 146 ms 57700 KB Output is correct
10 Correct 143 ms 57320 KB Output is correct