답안 #768053

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
768053 2023-06-27T11:55:25 Z boris_mihov 이상적인 도시 (IOI12_city) C++17
32 / 100
11 ms 9300 KB
#include <algorithm>
#include <iostream>
#include <numeric>
#include <cassert>
#include <vector>
#include <queue>
#include <map>

typedef long long llong;
const llong MAXN = 100000 + 10;
const llong MOD = 1'000'000'000;

llong n;
llong ans;
llong sz[MAXN];
llong par[MAXN];
llong dp[MAXN];
llong chain[MAXN];
llong inChain[MAXN];
llong x[MAXN], y[MAXN];
std::vector <llong> out[MAXN];
std::pair <llong,llong> delta[] = {{-1, 0}, {0, -1}, {1, 0}, {0, 1}};
std::map <std::pair <llong,llong>, llong> map;
std::vector <llong> g[MAXN];
std::vector <llong> t[MAXN];
std::queue <llong> q;

void dfsDown(llong node)
{
    dp[node] = 0;
    sz[node] = 1;

    for (const llong &u : t[node])
    {
        if (u == par[node])
        {
            continue;
        }
        
        dfsDown(u);
        sz[node] += sz[u];
        dp[node] += dp[u] + sz[u];
        dp[node] %= MOD;
    }
}

void dfsUp(llong node)
{
    if (par[node] != 0)
    {
        dp[node] = (dp[par[node]] + n + 2 * (MOD - chain[inChain[node]])) % MOD;
    }
    
    for (const llong &u : t[node])
    {
        if (u != par[node])
        {
            dfsUp(u);
        }
    }
}

llong timer;
void dfsChain(llong node)
{
    inChain[node] = timer;
    chain[inChain[node]] += sz[node];
    chain[inChain[node]] %= MOD;

    for (const llong &u : out[node])
    {
        if (inChain[u] == 0)
        {
            dfsChain(u);
        }
    }
}

int DistanceSum(int N, int *X, int *Y) 
{
    n = N;
    llong minX = X[0];
    llong maxX = X[0];
    llong minY = Y[0];
    llong maxY = Y[0];

    for (llong i = 1 ; i <= n ; ++i)
    {
        x[i] = X[i - 1];
        y[i] = Y[i - 1];
        map[{x[i], y[i]}] = i;
        minX = std::min(minX, x[i]);
        maxX = std::max(maxX, x[i]);
        minY = std::min(minY, y[i]);
        maxY = std::max(maxY, y[i]);
    }   

    if (n > 2000)
    {
        llong lenX = maxX - minX + 1;
        llong lenY = maxY - minY + 1;
        for (llong currX = 1 ; currX <= maxX - minX + 1 ; ++currX)
        {
            for (llong currY = 1 ; currY <= maxY - minY + 1 ; ++currY)
            {
                ans += lenX * currY * (currY - 1) / 2;
                ans += lenX * (lenY * (lenY + 1) / 2 - currY * (currY + 1) / 2 - (lenY - currY) * currY);

                ans += lenY * currX * (currX - 1) / 2;
                ans += lenY * (lenX * (lenX + 1) / 2 - currX * (currX + 1) / 2 - (lenX - currX) * currX);
                // std::cout << "ans: " << currX << ' ' << currY << ' ' << ans << '\n';
                ans %= MOD;
            }
        }

        return ans / 2;
    }
    for (llong i = 1 ; i <= n ; ++i)
    {
        for (const auto &[dx, dy] : delta)
        {
            if (map.count({x[i] + dx, y[i] + dy}))
            {
                g[i].push_back(map[{x[i] + dx, y[i] + dy}]);
            }
        }
    }

    q.push(n/2 + 1);
    std::fill(par + 1, par + n + 1, -1);
    par[n/2 + 1] = 0;

    while (!q.empty())
    {
        llong top = q.front();
        q.pop();

        for (const llong &i : g[top])
        {
            if (par[i] == -1)
            {
                par[i] = top;
                t[top].push_back(i);
                q.push(i);
            } else if (par[i] != top && par[top] != i)
            {
                out[top].push_back(i);
            }
        }
    }

    dfsDown(n/2 + 1);
    for (llong i = 1 ; i <= n ; ++i)
    {
        if (inChain[i] == 0)
        {
            timer++;
            dfsChain(i);
        }
    }

    dfsUp(n/2 + 1);

    for (llong i = 1 ; i <= n ; ++i)
    {
        ans += dp[i];
        ans %= MOD;
    }

    return ans / 2;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 7380 KB Output is correct
2 Correct 3 ms 7380 KB Output is correct
3 Correct 3 ms 7380 KB Output is correct
4 Correct 4 ms 7380 KB Output is correct
5 Correct 3 ms 7368 KB Output is correct
6 Correct 4 ms 7368 KB Output is correct
7 Correct 3 ms 7364 KB Output is correct
8 Correct 4 ms 7380 KB Output is correct
9 Correct 4 ms 7380 KB Output is correct
10 Correct 4 ms 7364 KB Output is correct
11 Correct 3 ms 7380 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 7492 KB Output is correct
2 Correct 5 ms 7508 KB Output is correct
3 Correct 5 ms 7636 KB Output is correct
4 Correct 6 ms 7636 KB Output is correct
5 Correct 5 ms 7764 KB Output is correct
6 Correct 6 ms 7764 KB Output is correct
7 Correct 6 ms 7764 KB Output is correct
8 Correct 6 ms 7756 KB Output is correct
9 Correct 6 ms 7764 KB Output is correct
10 Correct 6 ms 7764 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 11 ms 9172 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 11 ms 9300 KB Output isn't correct
2 Halted 0 ms 0 KB -