답안 #1031224

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1031224 2024-07-22T16:15:39 Z VMaksimoski008 이상적인 도시 (IOI12_city) C++17
32 / 100
65 ms 6864 KB
#include "bits/stdc++.h"
using namespace std;
using ll = long long;
using pii = pair<int, int>;
const int maxn = 1e5 + 5;
const int mod = 1e9;

vector<int> graph[maxn], sub(maxn), sz(maxn);

void dfs(int u, int p) {
    sub[u] = sz[u];
    for(int &v : graph[u]) {
        if(v == p) continue;
        dfs(v, u);
        sub[u] += sub[v];
    }
}

ll solve(vector<pii> v, int N, int *X, int *Y) {
    for(int i=0; i<maxn; i++) {
        graph[i].clear();
        sub[i] = sz[i] = 0;
    }

    int mn = 1e9, mx = 0;
    for(int i=0; i<N; i++) {
        mn = min(mn, X[i]);
        mx = max(mx, X[i]);
    }

    int curr = 1;
    int p = 0, start = 0;

    vector<array<int, 3> > ivals;
    for(int i=mn; i<=mx; i++) {
        // cout << i << ": " << ivals.size() << '\n';
        start = p;
        int last = v[p].second-1;
        vector<array<int, 3> > nvals;
        int ptr = 0;
        while(p < N && v[p].first == i) {
            if(v[p].second - last > 1) {
                // cout << "! " << ptr << '\n';
                // while(ptr < ivals.size() && ivals[ptr][0] >= v[start].second && ivals[ptr][0] <= v[p-1].second) {
                //     graph[curr].push_back(ivals[ptr][2]);
                //     graph[ivals[ptr][2]].push_back(curr);
                //     cout << curr << " <-> " << ivals[ptr][2] << '\n';
                //     ptr++;
                // }
                nvals.push_back({ v[start].second, v[p-1].second, curr });
                start = p;
                curr++;
            }
            sz[curr]++;
            last = v[p].second;
            p++;
        }

        if(start < p) {
            // cout << "! " << ptr << '\n';
            // while(ptr < ivals.size() && ivals[ptr][0] >= v[start].second && ivals[ptr][0] <= v[p-1].second) {
            //     graph[curr].push_back(ivals[ptr][2]);
            //     graph[ivals[ptr][2]].push_back(curr);
            //     cout << curr << " <-> " << ivals[ptr][2] << '\n';
            //     ptr++;
            // }
            nvals.push_back({ v[start].second, v[p-1].second, curr });
            start = p;
        }

        if(i > mn) {
            graph[curr].push_back(curr-1);
            graph[curr-1].push_back(curr);
        } 
        curr++;
        ivals = nvals;
        // for(auto &[l, r, ski] : ivals) cout << l << " " << r << '\n';

    }

    curr--;

    dfs(1, 1);

    ll ans = 0;
    for(int i=2; i<=curr; i++) ans = (ans + sub[i] * (N - sub[i])) % mod;
    return ans;
}

int DistanceSum(int N, int *X, int *Y) {
    if(N <= 2000) {
        vector<int> graph[N];
        for(int i=0; i<N; i++) {
            for(int j=i+1; j<N; j++) {
                if(abs(X[i] - X[j]) + abs(Y[i] - Y[j]) == 1) {
                    graph[i].push_back(j);
                    graph[j].push_back(i);
                }
            }
        }
 
        int ans = 0;
 
        for(int i=0; i<N; i++) {
            queue<int> q;
            vector<bool> vis(N);
            vector<int> dist(N);
            vis[i] = 1; q.push(i);
 
            while(!q.empty()) {
                int u = q.front(); q.pop();
                for(int &v : graph[u]) {
                    if(vis[v]) continue;
                    vis[v] = 1;
                    dist[v] = dist[u] + 1;
                    q.push(v);
                }
            }
 
            for(int j=i+1; j<N; j++) ans += dist[j];
        }
 
        return ans; 
    }
    
    vector<pii> v;
    for(int i=0; i<N; i++) v.push_back({ X[i], Y[i] });
    sort(v.begin(), v.end());

    ll ans = solve(v, N, X, Y);

    for(int i=0; i<N; i++) swap(v[i].first, v[i].second);
    sort(v.begin(), v.end());

    return (ans + solve(v, N, X, Y)) % mod;
}

Compilation message

city.cpp: In function 'll solve(std::vector<std::pair<int, int> >, int, int*, int*)':
city.cpp:40:13: warning: unused variable 'ptr' [-Wunused-variable]
   40 |         int ptr = 0;
      |             ^~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 3420 KB Output is correct
2 Correct 1 ms 3420 KB Output is correct
3 Correct 1 ms 3416 KB Output is correct
4 Correct 1 ms 3420 KB Output is correct
5 Correct 1 ms 3420 KB Output is correct
6 Correct 3 ms 3420 KB Output is correct
7 Correct 3 ms 3476 KB Output is correct
8 Correct 2 ms 3420 KB Output is correct
9 Correct 2 ms 3420 KB Output is correct
10 Correct 2 ms 3420 KB Output is correct
11 Correct 2 ms 3420 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 3420 KB Output is correct
2 Correct 14 ms 3660 KB Output is correct
3 Correct 37 ms 3676 KB Output is correct
4 Correct 35 ms 3676 KB Output is correct
5 Correct 49 ms 3672 KB Output is correct
6 Correct 65 ms 3724 KB Output is correct
7 Correct 57 ms 3672 KB Output is correct
8 Correct 61 ms 3676 KB Output is correct
9 Correct 61 ms 3676 KB Output is correct
10 Correct 63 ms 3676 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 4312 KB Output is correct
2 Correct 6 ms 4312 KB Output is correct
3 Correct 14 ms 5340 KB Output is correct
4 Correct 22 ms 5340 KB Output is correct
5 Incorrect 27 ms 6864 KB Output isn't correct
6 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 8 ms 4308 KB Output isn't correct
2 Halted 0 ms 0 KB -