답안 #1086154

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1086154 2024-09-09T16:08:17 Z steveonalex 이상적인 도시 (IOI12_city) C++17
32 / 100
1000 ms 20108 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 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);
        
        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;
    }
    if (p == -1){
        int m = flow_cnt[u].size();
        dp[u] *= m;
        for(int i = 0; i<m; ++i){
            ll fu = i * (i+1) / 2 + (m-i)*(m-i-1) / 2;
            dp[u] += fu * flow_cnt[u][i];
        }
    }
}

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 i= 0; i<m; ++i){
        for(int j = 0; j < m; ++j) dp[j] = 0;
        for(int j = 0; j < m; ++j) flow_cnt[j].clear();
        dfs(i, -1);
        ans += dp[i];
    }
    ans /= 2;

    return ans % MOD;
}

# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 4952 KB Output is correct
2 Correct 2 ms 4952 KB Output is correct
3 Correct 2 ms 4956 KB Output is correct
4 Correct 2 ms 4956 KB Output is correct
5 Correct 2 ms 4956 KB Output is correct
6 Correct 2 ms 4956 KB Output is correct
7 Correct 3 ms 5172 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 2 ms 4956 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 5212 KB Output is correct
2 Correct 4 ms 5260 KB Output is correct
3 Correct 15 ms 5208 KB Output is correct
4 Correct 7 ms 5152 KB Output is correct
5 Correct 29 ms 5212 KB Output is correct
6 Correct 7 ms 5212 KB Output is correct
7 Correct 25 ms 5208 KB Output is correct
8 Correct 8 ms 5396 KB Output is correct
9 Correct 6 ms 5212 KB Output is correct
10 Correct 6 ms 5212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 28 ms 7260 KB Output is correct
2 Correct 57 ms 7260 KB Output is correct
3 Correct 81 ms 10276 KB Output is correct
4 Correct 172 ms 10444 KB Output is correct
5 Correct 205 ms 15700 KB Output is correct
6 Correct 445 ms 15684 KB Output is correct
7 Correct 571 ms 15696 KB Output is correct
8 Correct 136 ms 15516 KB Output is correct
9 Correct 929 ms 15444 KB Output is correct
10 Execution timed out 1046 ms 20108 KB Time limit exceeded
11 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 1042 ms 7508 KB Time limit exceeded
2 Halted 0 ms 0 KB -