답안 #702372

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
702372 2023-02-23T19:12:54 Z Ronin13 이상적인 도시 (IOI12_city) C++14
32 / 100
46 ms 50116 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 = 1e9;
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;
}

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 21 ms 47300 KB Output is correct
3 Correct 21 ms 47188 KB Output is correct
4 Correct 20 ms 47196 KB Output is correct
5 Correct 21 ms 47412 KB Output is correct
6 Correct 21 ms 47188 KB Output is correct
7 Correct 20 ms 47296 KB Output is correct
8 Correct 21 ms 47236 KB Output is correct
9 Correct 20 ms 47304 KB Output is correct
10 Correct 21 ms 47224 KB Output is correct
11 Correct 21 ms 47200 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 21 ms 47316 KB Output is correct
2 Correct 22 ms 47376 KB Output is correct
3 Correct 22 ms 47380 KB Output is correct
4 Correct 21 ms 47436 KB Output is correct
5 Correct 23 ms 47556 KB Output is correct
6 Correct 27 ms 47512 KB Output is correct
7 Correct 24 ms 47564 KB Output is correct
8 Correct 22 ms 47436 KB Output is correct
9 Correct 25 ms 47516 KB Output is correct
10 Correct 22 ms 47432 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 40 ms 49084 KB Output is correct
2 Incorrect 39 ms 49080 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 46 ms 50116 KB Output isn't correct
2 Halted 0 ms 0 KB -