답안 #1103843

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1103843 2024-10-22T02:17:45 Z vjudge1 Bitaro's travel (JOI23_travel) C++17
0 / 100
11 ms 70748 KB
#include <bits/stdc++.h>

using namespace std;

const long long mod = 1e9 + 7;
const int MaxN = 1e6 + 5;
const int MaxLog = 21;

int n, q;
long long arr[MaxN];

void Inp()
{
    cin >> n;
    for (int x = 1; x <= n; x++)
    {
        cin >> arr[x];
    }
    sort(arr + 1, arr + n + 1);
}

long long Sparse[MaxLog][MaxN];

void Build()
{
    for (int x = 1; x <= n; x++)
    {
        Sparse[0][x] = 2 * arr[x] - arr[x + 1];
    }
    for (int x = 1; x < MaxLog; x++)
    {
        for (int y = 1; y <= n; y++)
        {
            Sparse[x][y] = min(Sparse[x - 1][y], Sparse[x - 1][y + (1 << (x - 1))]);
        }
    }
}

void Build2()
{
    for (int x = 1; x <= n; x++)
    {
        Sparse[0][x] = 2 * arr[x] - arr[x - 1];
    }
    for (int x = 1; x < MaxLog; x++)
    {
        for (int y = 1; y <= n; y++)
        {
            Sparse[x][y] = max(Sparse[x - 1][y], Sparse[x - 1][y + (1 << (x - 1))]);
        }
    }
}

long long Get(int l, int r)
{
    int p = __lg(r - l + 1);
    return min(Sparse[p][l], Sparse[p][r - (1 << p) + 1]);
}

long long Get2(int l, int r)
{
    int p = __lg(r - l + 1);
    return max(Sparse[p][l], Sparse[p][r - (1 << p) + 1]);
}

vector<int> graph[MaxN];
long long res[MaxN];

void DFS(int u, int p)
{
    for (int x : graph[u])
    {
        if (x != p)
        {
            res[x] += res[u] + abs(arr[u] - arr[x]);
            DFS(x, u);
        }
    }
}

void Exc()
{
    Build();
    for (int x = 2; x < n; x++)
    {
        if (arr[x + 1] - arr[x] < arr[x] - arr[x - 1])
        {
            int l = x + 1, r = n - 1, mid, res = n;
            while (l <= r)
            {
                mid = (l + r) / 2;
                if (Get(x + 1, mid) <= arr[x - 1])
                {
                    res = mid;
                    r = mid - 1;
                }
                else
                {
                    l = mid + 1;
                }
            }
            graph[x].push_back(res);
            graph[res].push_back(x);
        }
    }
    Build2();
    for (int x = 2; x < n; x++)
    {
        if (arr[x + 1] - arr[x] >= arr[x] - arr[x - 1])
        {
            int l = 2, r = x - 1, mid, res = 1;
            while (l <= r)
            {
                mid = (l + r) / 2;
                if (Get2(mid, x - 1) > arr[x + 1])
                {
                    res = mid;
                    l = mid + 1;
                }
                else
                {
                    r = mid - 1;
                }
            }
            graph[x].push_back(res);
            graph[res].push_back(x);
        }
    }
    DFS(1, -1);
    DFS(n, -1);
    map<long long, int> mp;
    for (int x = 1; x <= n; x++)
    {
        mp[arr[x]] = x;
        //cout << res[x] << " ";
    }
    //cout << "\n";
    cin >> q;
    for (int x = 1; x <= q; x++)
    {
        int p;
        cin >> p;
        cout << res[mp[p]] + (arr[n] - arr[1]) << "\n";
    }
}

int main()
{
    //freopen("C.INP", "r", stdin);
    //freopen("C.OUT", "w", stdout);
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    int test = 1;
    //cin >> test;
    for (int x = 1; x <= test; x++)
    {
        Inp();
        Exc();
    }
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 70224 KB Output is correct
2 Incorrect 11 ms 70748 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 70224 KB Output is correct
2 Incorrect 11 ms 70748 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 70392 KB Output is correct
2 Correct 9 ms 70240 KB Output is correct
3 Incorrect 8 ms 70224 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 70224 KB Output is correct
2 Incorrect 11 ms 70748 KB Output isn't correct
3 Halted 0 ms 0 KB -