Submission #415652

# Submission time Handle Problem Language Result Execution time Memory
415652 2021-06-01T10:27:04 Z snasibov05 Ideal city (IOI12_city) C++14
32 / 100
186 ms 15768 KB
#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <algorithm>

using namespace std;

#define oo 1000000000
#define ll long long
#define pb push_back
#define pii pair<int, int>

const ll m = 1e9;

vector<int> val;
vector<vector<int>> ed;
map<pii, int> mp;
vector<int> sm;

void init(int n){
    val.clear();
    val.resize(n+1);
    ed.clear();
    ed.resize(n+1);
    sm.clear();
    sm.resize(n+1);
    mp.clear();
}

void build_tree(int n, int k, int *x, int *y){
    init(n);

    vector<vector<int>> v(k+1);
    for (int i = 0; i < n; ++i) {
        v[x[i]].pb(y[i]);
    }

    for (int i = 1; i <= k; ++i) {
        sort(v[i].begin(), v[i].end());
    }

    int idx = 1;

    for (int i = 1; i <= k; ++i) {
        int sz = v[i].size();
        for (int j = 0; j < sz; ++j) {
            set<int> prev;
            int cur = 1;
            mp[{i, v[i][j]}] = idx;
            prev.insert(mp[{i-1, v[i][j]}]);
            while (j != sz-1 && v[i][j+1] == v[i][j] + 1) {
                cur++; j++;
                prev.insert(mp[{i-1, v[i][j]}]);
                mp[{i, v[i][j]}] = idx;
            }
            val[idx] = cur;
            for (auto p : prev){
                if (p == 0) continue;
                ed[p].pb(idx);
                ed[idx].pb(p);
            }
            idx++;
        }
    }

}

void dfs(int v, int pr){
    sm[v] = val[v];
    for (auto x : ed[v]){
        if (x == pr) continue;
        dfs(x, v);
        sm[v] += sm[x];
        sm[v] %= m;
    }
}

int calcDist(int n, int v, int pr){
    int res = 0;
    for (auto x : ed[v]){
        if (x == pr) continue;
        ll k = sm[x] * (n - sm[x]) % m;
        res += (int)k;
        res %= m;
        res += calcDist(n, x, v);
        res %= m;
    }

    return res;
}

int DistanceSum(int n, int *x, int *y) {

    int mn_x = oo, mn_y = oo;
    for (int i = 0; i < n; ++i) {
        mn_x = min(mn_x, x[i]);
        mn_y = min(mn_y, y[i]);
    }

    int mx_x = 0, mx_y = 0;

    for (int i = 0; i < n; ++i) {
        x[i] = x[i] - mn_x + 1;
        y[i] = y[i] - mn_y + 1;
        mx_x = max(x[i], mx_x);
        mx_y = max(y[i], mx_y);
    }

    int ans = 0;

    build_tree(n, mx_x, x, y);
    dfs(1, -1);
    ans += calcDist(n, 1, -1);
    ans %= m;

    build_tree(n, mx_y, y, x);
    dfs(1, -1);
    ans += calcDist(n, 1, -1);
    ans %= m;

    return ans;

}
# 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 Correct 1 ms 204 KB Output is correct
4 Correct 2 ms 264 KB Output is correct
5 Correct 2 ms 204 KB Output is correct
6 Correct 2 ms 332 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 432 KB Output is correct
2 Correct 2 ms 404 KB Output is correct
3 Correct 3 ms 452 KB Output is correct
4 Correct 3 ms 460 KB Output is correct
5 Correct 4 ms 588 KB Output is correct
6 Correct 3 ms 588 KB Output is correct
7 Correct 4 ms 588 KB Output is correct
8 Correct 3 ms 568 KB Output is correct
9 Correct 3 ms 460 KB Output is correct
10 Correct 3 ms 560 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 17 ms 2636 KB Output is correct
2 Correct 18 ms 2764 KB Output is correct
3 Correct 43 ms 6196 KB Output is correct
4 Correct 47 ms 6204 KB Output is correct
5 Incorrect 93 ms 11972 KB Output isn't correct
6 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 26 ms 3476 KB Output is correct
2 Correct 22 ms 3244 KB Output is correct
3 Correct 70 ms 8040 KB Output is correct
4 Correct 63 ms 7420 KB Output is correct
5 Incorrect 186 ms 15768 KB Output isn't correct
6 Halted 0 ms 0 KB -