답안 #1103846

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1103846 2024-10-22T02:27:33 Z cpismylifeOwO Bitaro's travel (JOI23_travel) C++17
100 / 100
255 ms 133192 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++)
    {
        long long p;
        cin >> p;
        auto q = mp.lower_bound(p);
        if (q == mp.begin())
        {
            cout << res[q->second] + (arr[n] - arr[1]) + abs(q->first - p) << "\n";
        }
        else if (q == mp.end())
        {
            --q;
            cout << res[q->second] + (arr[n] - arr[1]) + abs(q->first - p) << "\n";
        }
        else
        {
            auto k = q;
            --k;
            if (abs(k->first - p) > abs(q->first - p))
            {
                cout << res[q->second] + (arr[n] - arr[1]) + abs(q->first - p) << "\n";
            }
            else
            {
                cout << res[k->second] + (arr[n] - arr[1]) + abs(k->first - p) << "\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 10 ms 70396 KB Output is correct
2 Correct 10 ms 70224 KB Output is correct
3 Correct 9 ms 70480 KB Output is correct
4 Correct 11 ms 70436 KB Output is correct
5 Correct 9 ms 70420 KB Output is correct
6 Correct 11 ms 70480 KB Output is correct
7 Correct 11 ms 64348 KB Output is correct
8 Correct 10 ms 64080 KB Output is correct
9 Correct 10 ms 64080 KB Output is correct
10 Correct 7 ms 51792 KB Output is correct
11 Correct 8 ms 47696 KB Output is correct
12 Correct 9 ms 58108 KB Output is correct
13 Correct 8 ms 51792 KB Output is correct
14 Correct 9 ms 44112 KB Output is correct
15 Correct 8 ms 47952 KB Output is correct
16 Correct 8 ms 44112 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 70396 KB Output is correct
2 Correct 10 ms 70224 KB Output is correct
3 Correct 9 ms 70480 KB Output is correct
4 Correct 11 ms 70436 KB Output is correct
5 Correct 9 ms 70420 KB Output is correct
6 Correct 11 ms 70480 KB Output is correct
7 Correct 11 ms 64348 KB Output is correct
8 Correct 10 ms 64080 KB Output is correct
9 Correct 10 ms 64080 KB Output is correct
10 Correct 7 ms 51792 KB Output is correct
11 Correct 8 ms 47696 KB Output is correct
12 Correct 9 ms 58108 KB Output is correct
13 Correct 8 ms 51792 KB Output is correct
14 Correct 9 ms 44112 KB Output is correct
15 Correct 8 ms 47952 KB Output is correct
16 Correct 8 ms 44112 KB Output is correct
17 Correct 128 ms 95816 KB Output is correct
18 Correct 109 ms 111288 KB Output is correct
19 Correct 113 ms 119804 KB Output is correct
20 Correct 118 ms 105932 KB Output is correct
21 Correct 110 ms 109588 KB Output is correct
22 Correct 117 ms 89552 KB Output is correct
23 Correct 115 ms 94264 KB Output is correct
24 Correct 108 ms 95940 KB Output is correct
25 Correct 108 ms 93648 KB Output is correct
26 Correct 110 ms 98500 KB Output is correct
27 Correct 104 ms 118712 KB Output is correct
28 Correct 98 ms 126148 KB Output is correct
29 Correct 109 ms 128032 KB Output is correct
30 Correct 105 ms 128052 KB Output is correct
31 Correct 100 ms 128800 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 70224 KB Output is correct
2 Correct 9 ms 70224 KB Output is correct
3 Correct 10 ms 70224 KB Output is correct
4 Correct 10 ms 70124 KB Output is correct
5 Correct 10 ms 70224 KB Output is correct
6 Correct 9 ms 70224 KB Output is correct
7 Correct 236 ms 125116 KB Output is correct
8 Correct 223 ms 125372 KB Output is correct
9 Correct 216 ms 124092 KB Output is correct
10 Correct 226 ms 124092 KB Output is correct
11 Correct 225 ms 130704 KB Output is correct
12 Correct 192 ms 130236 KB Output is correct
13 Correct 39 ms 73800 KB Output is correct
14 Correct 29 ms 71248 KB Output is correct
15 Correct 174 ms 131784 KB Output is correct
16 Correct 219 ms 131264 KB Output is correct
17 Correct 210 ms 128448 KB Output is correct
18 Correct 51 ms 73800 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 70396 KB Output is correct
2 Correct 10 ms 70224 KB Output is correct
3 Correct 9 ms 70480 KB Output is correct
4 Correct 11 ms 70436 KB Output is correct
5 Correct 9 ms 70420 KB Output is correct
6 Correct 11 ms 70480 KB Output is correct
7 Correct 11 ms 64348 KB Output is correct
8 Correct 10 ms 64080 KB Output is correct
9 Correct 10 ms 64080 KB Output is correct
10 Correct 7 ms 51792 KB Output is correct
11 Correct 8 ms 47696 KB Output is correct
12 Correct 9 ms 58108 KB Output is correct
13 Correct 8 ms 51792 KB Output is correct
14 Correct 9 ms 44112 KB Output is correct
15 Correct 8 ms 47952 KB Output is correct
16 Correct 8 ms 44112 KB Output is correct
17 Correct 128 ms 95816 KB Output is correct
18 Correct 109 ms 111288 KB Output is correct
19 Correct 113 ms 119804 KB Output is correct
20 Correct 118 ms 105932 KB Output is correct
21 Correct 110 ms 109588 KB Output is correct
22 Correct 117 ms 89552 KB Output is correct
23 Correct 115 ms 94264 KB Output is correct
24 Correct 108 ms 95940 KB Output is correct
25 Correct 108 ms 93648 KB Output is correct
26 Correct 110 ms 98500 KB Output is correct
27 Correct 104 ms 118712 KB Output is correct
28 Correct 98 ms 126148 KB Output is correct
29 Correct 109 ms 128032 KB Output is correct
30 Correct 105 ms 128052 KB Output is correct
31 Correct 100 ms 128800 KB Output is correct
32 Correct 9 ms 70224 KB Output is correct
33 Correct 9 ms 70224 KB Output is correct
34 Correct 10 ms 70224 KB Output is correct
35 Correct 10 ms 70124 KB Output is correct
36 Correct 10 ms 70224 KB Output is correct
37 Correct 9 ms 70224 KB Output is correct
38 Correct 236 ms 125116 KB Output is correct
39 Correct 223 ms 125372 KB Output is correct
40 Correct 216 ms 124092 KB Output is correct
41 Correct 226 ms 124092 KB Output is correct
42 Correct 225 ms 130704 KB Output is correct
43 Correct 192 ms 130236 KB Output is correct
44 Correct 39 ms 73800 KB Output is correct
45 Correct 29 ms 71248 KB Output is correct
46 Correct 174 ms 131784 KB Output is correct
47 Correct 219 ms 131264 KB Output is correct
48 Correct 210 ms 128448 KB Output is correct
49 Correct 51 ms 73800 KB Output is correct
50 Correct 248 ms 126280 KB Output is correct
51 Correct 210 ms 133192 KB Output is correct
52 Correct 217 ms 108908 KB Output is correct
53 Correct 238 ms 113348 KB Output is correct
54 Correct 228 ms 102084 KB Output is correct
55 Correct 255 ms 107980 KB Output is correct
56 Correct 170 ms 107848 KB Output is correct
57 Correct 172 ms 106864 KB Output is correct
58 Correct 168 ms 121064 KB Output is correct