Submission #1086165

# Submission time Handle Problem Language Result Execution time Memory
1086165 2024-09-09T16:25:24 Z steveonalex Ideal city (IOI12_city) C++17
100 / 100
92 ms 24464 KB
#include <bits/stdc++.h>
 
using namespace std;
 
typedef long long ll;
typedef unsigned long long ull;
 
#define MASK(i) (1LL << (i))
#define GETBIT(mask, i) (((mask) >> (i)) & 1)
#define ALL(v) (v).begin(), (v).end()
#define block_of_code if(true)
 
ll max(ll a, ll b){return (a > b) ? a : b;}
ll min(ll a, ll b){return (a < b) ? a : b;}
ll gcd(ll a, ll b){return __gcd(a, b);}
ll lcm(ll a, ll b){return a / gcd(a, b) * b;}
 
ll LASTBIT(ll mask){return (mask) & (-mask);}
int pop_cnt(ll mask){return __builtin_popcountll(mask);}
int ctz(ull mask){return __builtin_ctzll(mask);}
int logOf(ull mask){return 63 - __builtin_clzll(mask);}
 
mt19937_64 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
ll rngesus(ll l, ll r){return l + (ull) rng() % (r - l + 1);}
double rngesus_d(double l, double r){
    double cur = rngesus(0, MASK(60) - 1);
    cur /= MASK(60) - 1;
    return l + cur * (r - l);
}
 
template <class T1, class T2>
    bool maximize(T1 &a, T2 b){
        if (a < b) {a = b; return true;}
        return false;
    }
 
template <class T1, class T2>
    bool minimize(T1 &a, T2 b){
        if (a > b) {a = b; return true;}
        return false;
    }
 
template <class T>
    void printArr(T container, string separator = " ", string finish = "\n", ostream &out = cout){
        for(auto item: container) out << item << separator;
        out << finish;
    }
 
template <class T>
    void remove_dup(vector<T> &a){
        sort(ALL(a));
        a.resize(unique(ALL(a)) - a.begin());
    }

const int MOD = 1e9;
const int N = 1e5 + 69;
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};

int n;
vector<array<int, 3>> nodes;
vector<int> graph[N];
ll dp[N];
vector<int> flow_cnt[N];

void combine_node(int u, int v){
    dp[u] += dp[v];
    vector<int> sigma = flow_cnt[v];
    int l = 0, r = sigma.size() - 1;
    l += max(0, nodes[u][1] - nodes[v][1]);
    r -= max(0, nodes[v][2] - nodes[u][2]);

    int sum = 0;
    for(int i: sigma) sum += i;

    for(int i = 0; i < l; ++i){
        sigma[l] += sigma[i];
        dp[u] += 1LL * sigma[i] * (l - i);
        sigma[i] = 0;
    }
    for(int i = r + 1; i < (int)sigma.size(); ++i){
        sigma[r] += sigma[i];
        dp[u] += 1LL * sigma[i] * (i - r);
        sigma[i] = 0;
    }
    for(int i = l; i<=r; ++i){
        flow_cnt[u][i + nodes[v][1] - nodes[u][1]] += sigma[i];
    }
    dp[u] += sum;
}

void cut_node(int u, int v){
    dp[u] -= dp[v];
    vector<int> sigma = flow_cnt[v];
    int l = 0, r = sigma.size() - 1;
    l += max(0, nodes[u][1] - nodes[v][1]);
    r -= max(0, nodes[v][2] - nodes[u][2]);

    int sum = 0;
    for(int i: sigma) sum += i;

    for(int i = 0; i < l; ++i){
        sigma[l] += sigma[i];
        dp[u] -= 1LL * sigma[i] * (l - i);
        sigma[i] = 0;
    }
    for(int i = r + 1; i < (int)sigma.size(); ++i){
        sigma[r] += sigma[i];
        dp[u] -= 1LL * sigma[i] * (i - r);
        sigma[i] = 0;
    }
    for(int i = l; i<=r; ++i){
        flow_cnt[u][i + nodes[v][1] - nodes[u][1]] -= sigma[i];
    }
    dp[u] -= sum;

}

void dfs(int u, int p){
    flow_cnt[u].resize(nodes[u][2] - nodes[u][1] + 1, 1);
    for(int v: graph[u]) if (v != p){
        dfs(v, u);

        combine_node(u, v);
    }
}

void reroot_dp(int u, int p, ll &ans){
    int m = flow_cnt[u].size();
    ans += dp[u] * m;
    for(int i = 0; i<m; ++i){
        ll fu = i * (i+1) / 2 + (m-i)*(m-i-1) / 2;
        ans += fu * flow_cnt[u][i];
    }

    for(int v: graph[u]) if (v != p){
        ll tmp1 = dp[u], tmp2 = dp[v];
        vector<int> cnt1 = flow_cnt[u], cnt2 = flow_cnt[v];

        cut_node(u, v);
        combine_node(v, u);
        reroot_dp(v, u, ans);

        dp[u] = tmp1, dp[v] = tmp2;
        flow_cnt[u] = cnt1, flow_cnt[v] = cnt2;
    }
}

int DistanceSum(int N, int *X, int *Y) {
    n = N;
    set<pair<int, int>> S;
    for(int i = 0; i<n; ++i)
        S.insert({X[i], Y[i]});

    map<pair<int, int>, int> own;
    nodes.clear();

    ll ans = 0;
    while(S.size()){
        pair<int, int> cu = *S.begin();
        S.erase(cu);
        nodes.push_back({{cu.first, cu.second, cu.second}});
        own[cu] = nodes.size() - 1;
        int l = cu.second, r = cu.second;
        while(S.size()){
            r++;
            auto it = S.find(make_pair(cu.first, r));
            if (it != S.end()){
                own[{cu.first, r}] = nodes.size() - 1;
                S.erase(it);
                nodes.back()[2]++;
            }
            else break;
        }
        while(S.size()){
            l--;
            auto it = S.find(make_pair(cu.first, l));
            if (it != S.end()){
                own[{cu.first, l}] = nodes.size() - 1;
                S.erase(it);
                nodes.back()[1]--;
            }
            else break;
        }
    }

    int m = nodes.size();
    for(int i = 0; i<m; ++i) graph[i].clear();
    for(int i = 0; i<m; ++i){
        for(int y = nodes[i][1]; y <= nodes[i][2]; ++y){
            pair<int, int> cur = {nodes[i][0] - 1, y};
            if (own.count(cur)){
                graph[i].push_back(own[cur]);
                graph[own[cur]].push_back(i);
            }
            cur.first += 2;
        }
    }
    for(int i = 0; i<m; ++i) remove_dup(graph[i]);


    for(int j = 0; j < m; ++j) dp[j] = 0;
    for(int j = 0; j < m; ++j) flow_cnt[j].clear();
    dfs(0, -1);
    reroot_dp(0, -1, ans);

    ans /= 2;

    return ans % MOD;
}

# Verdict Execution time Memory Grader output
1 Correct 2 ms 5720 KB Output is correct
2 Correct 2 ms 5720 KB Output is correct
3 Correct 1 ms 5724 KB Output is correct
4 Correct 2 ms 5720 KB Output is correct
5 Correct 2 ms 5720 KB Output is correct
6 Correct 2 ms 5724 KB Output is correct
7 Correct 1 ms 5928 KB Output is correct
8 Correct 2 ms 4956 KB Output is correct
9 Correct 2 ms 4956 KB Output is correct
10 Correct 2 ms 4956 KB Output is correct
11 Correct 3 ms 5724 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5976 KB Output is correct
2 Correct 3 ms 5212 KB Output is correct
3 Correct 4 ms 5212 KB Output is correct
4 Correct 5 ms 5212 KB Output is correct
5 Correct 4 ms 6204 KB Output is correct
6 Correct 4 ms 5212 KB Output is correct
7 Correct 4 ms 5452 KB Output is correct
8 Correct 4 ms 5372 KB Output is correct
9 Correct 3 ms 5980 KB Output is correct
10 Correct 3 ms 5212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 15 ms 8216 KB Output is correct
2 Correct 15 ms 8284 KB Output is correct
3 Correct 39 ms 11548 KB Output is correct
4 Correct 43 ms 11536 KB Output is correct
5 Correct 80 ms 17440 KB Output is correct
6 Correct 88 ms 17144 KB Output is correct
7 Correct 81 ms 17204 KB Output is correct
8 Correct 92 ms 17028 KB Output is correct
9 Correct 78 ms 17292 KB Output is correct
10 Correct 87 ms 24464 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 18 ms 8284 KB Output is correct
2 Correct 16 ms 8284 KB Output is correct
3 Correct 45 ms 11608 KB Output is correct
4 Correct 45 ms 11712 KB Output is correct
5 Correct 85 ms 17520 KB Output is correct
6 Correct 81 ms 16592 KB Output is correct
7 Correct 91 ms 18052 KB Output is correct
8 Correct 86 ms 16796 KB Output is correct
9 Correct 79 ms 16056 KB Output is correct
10 Correct 85 ms 16240 KB Output is correct