Submission #920655

# Submission time Handle Problem Language Result Execution time Memory
920655 2024-02-02T20:47:26 Z andrei_iorgulescu Bitaro's travel (JOI23_travel) C++14
100 / 100
488 ms 70740 KB
#include <bits/stdc++.h>

using namespace std;

#define int long long

int n,q;
int a[200005];
int v1[200005],v2[200005];
int lg[200005];
int rmq1[20][200005],rmq2[20][200005];

void build()
{
    for (int i = 2; i <= n; i++)
        lg[i] = 1 + lg[i / 2];
    for (int i = 1; i <= n; i++)
        rmq1[0][i] = v1[i],rmq2[0][i] = v2[i];
    for (int j = 1; j <= lg[n]; j++)
    {
        for (int i = 1; i <= n - (1 << j) + 1; i++)
        {
            rmq1[j][i] = min(rmq1[j - 1][i],rmq1[j - 1][i + (1 << (j - 1))]);
            rmq2[j][i] = max(rmq2[j - 1][i],rmq2[j - 1][i + (1 << (j - 1))]);
        }
    }
}

int query1(int l,int r)
{
    int lgg = lg[r - l + 1];
    return min(rmq1[lgg][l],rmq1[lgg][r - (1 << lgg) + 1]);
}

int query2(int l,int r)
{
    int lgg = lg[r - l + 1];
    return max(rmq2[lgg][l],rmq2[lgg][r - (1 << lgg) + 1]);
}

int moveleft(int &l,int &r)
{
    ///se presupune ca sunt in r
    if (l == 1)
        return 0;
    if (r == n)
    {
        l = 1;
        return a[n] - a[1];
    }
    ///daca a[r + 1] - a[r] < a[r] - a[l - 1], atunci clar nu fac nimic
    if (a[r + 1] - a[r] < a[r] - a[l - 1])
        return 0;
    ///daca pentru fiecare 2 <= y <= l - 1, a[y] - a[y - 1] <= a[r + 1] - a[y], adica 2 * a[y] - a[y - 1] <= a[r + 1], merg pana la 1
    if (query2(2,l - 1) <= a[r + 1])
    {
        l = 1;
        return a[r] - a[1];
    }
    ///altfel vreau sa gasesc cel mai mare y <= l - 1 pentru care v2[y] > a[r + 1]
    int st = 1,pas = 1 << 17;
    while (pas != 0)
    {
        if (st + pas < l and query2(st + pas,l - 1) > a[r + 1])
            st += pas;
        pas /= 2;
    }
    l = st;
    return a[r] - a[l];
}

int moveright(int &l,int &r)
{
    ///se presupune ca sunt in l
    if (r == n)
        return 0;
    if (l == 1)
    {
        r = n;
        return a[n] - a[1];
    }
    ///daca a[r + 1] - a[l] >= a[l] - a[l - 1], nu fac nimic
    if (a[r + 1] - a[l] >= a[l] - a[l - 1])
        return 0;
    ///daca pentru fiecare r + 1 <= y <= n - 1, a[y + 1] - a[y] < a[y] - a[l - 1] <=> a[y + 1] - 2 * a[y] < -a[l - 1] <=> 2 * a[y] - a[y + 1] > a[l - 1], merg pana la final
    if (query1(r + 1,n - 1) > a[l - 1])
    {
        r = n;
        return a[n] - a[l];
    }
    ///altfel vreau sa gasesc y-ul minim >= r + 1 pentru care a[y + 1] - a[y] >= a[y] - a[l - 1] adica v1[y] <= a[l - 1]
    int st = r,pas = 1 << 17;
    while (pas != 0)
    {
        if (st + pas < n and query1(r + 1,st + pas) > a[l - 1])
            st += pas;
        pas /= 2;
    }
    st++;
    r = st;
    return a[r] - a[l];
}

signed main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    cin >> n;
    for (int i = 1; i <= n; i++)
        cin >> a[i];
    a[n + 1] = 1e10;
    a[0] = -1e10;
    for (int i = 1; i <= n; i++)
        v1[i] = 2 * a[i] - a[i + 1];
    for (int i = 1; i <= n; i++)
        v2[i] = 2 * a[i] - a[i - 1];
    build();
    cin >> q;
    for (int i = 1; i <= q; i++)
    {
        int ans = 0;
        int p;
        cin >> p;
        int l,r;
        int st = 0,pas = 1 << 17;
        while (pas != 0)
        {
            if (st + pas <= n and a[st + pas] <= p)
                st += pas;
            pas /= 2;
        }
        if (st == 0)
            ans = a[1] - p,l = 1,r = 1;
        else if (st == n)
            ans = p - a[n],l = n,r = n;
        else
        {
            if (p - a[st] <= a[st + 1] - p)
                ans = p - a[st],l = st,r = st;
            else
                ans = a[st + 1] - p,l = st + 1,r = st + 1;
        }
        int idx = 0;
        while (l != 1 or r != n)
        {
            idx++;
            if (idx % 2 == 1)
                ans += moveright(l,r);
            else
                ans += moveleft(l,r);
        }
        cout << ans << '\n';
    }
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 12632 KB Output is correct
2 Correct 6 ms 37212 KB Output is correct
3 Correct 5 ms 37456 KB Output is correct
4 Correct 5 ms 37212 KB Output is correct
5 Correct 5 ms 37408 KB Output is correct
6 Correct 5 ms 37208 KB Output is correct
7 Correct 6 ms 37212 KB Output is correct
8 Correct 1 ms 6492 KB Output is correct
9 Correct 1 ms 6612 KB Output is correct
10 Correct 1 ms 6492 KB Output is correct
11 Correct 1 ms 6488 KB Output is correct
12 Correct 2 ms 8540 KB Output is correct
13 Correct 3 ms 16988 KB Output is correct
14 Correct 6 ms 37212 KB Output is correct
15 Correct 5 ms 37212 KB Output is correct
16 Correct 5 ms 37356 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 12632 KB Output is correct
2 Correct 6 ms 37212 KB Output is correct
3 Correct 5 ms 37456 KB Output is correct
4 Correct 5 ms 37212 KB Output is correct
5 Correct 5 ms 37408 KB Output is correct
6 Correct 5 ms 37208 KB Output is correct
7 Correct 6 ms 37212 KB Output is correct
8 Correct 1 ms 6492 KB Output is correct
9 Correct 1 ms 6612 KB Output is correct
10 Correct 1 ms 6492 KB Output is correct
11 Correct 1 ms 6488 KB Output is correct
12 Correct 2 ms 8540 KB Output is correct
13 Correct 3 ms 16988 KB Output is correct
14 Correct 6 ms 37212 KB Output is correct
15 Correct 5 ms 37212 KB Output is correct
16 Correct 5 ms 37356 KB Output is correct
17 Correct 29 ms 66132 KB Output is correct
18 Correct 29 ms 66360 KB Output is correct
19 Correct 28 ms 66128 KB Output is correct
20 Correct 28 ms 66136 KB Output is correct
21 Correct 28 ms 66128 KB Output is correct
22 Correct 33 ms 66128 KB Output is correct
23 Correct 29 ms 66132 KB Output is correct
24 Correct 28 ms 66112 KB Output is correct
25 Correct 28 ms 66128 KB Output is correct
26 Correct 27 ms 66132 KB Output is correct
27 Correct 28 ms 66140 KB Output is correct
28 Correct 28 ms 66124 KB Output is correct
29 Correct 27 ms 66132 KB Output is correct
30 Correct 28 ms 66132 KB Output is correct
31 Correct 27 ms 66124 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 12636 KB Output is correct
2 Correct 3 ms 16732 KB Output is correct
3 Correct 1 ms 6492 KB Output is correct
4 Correct 1 ms 6492 KB Output is correct
5 Correct 1 ms 6492 KB Output is correct
6 Correct 1 ms 6492 KB Output is correct
7 Correct 99 ms 68036 KB Output is correct
8 Correct 89 ms 68076 KB Output is correct
9 Correct 86 ms 68240 KB Output is correct
10 Correct 88 ms 68236 KB Output is correct
11 Correct 83 ms 68176 KB Output is correct
12 Correct 88 ms 68180 KB Output is correct
13 Correct 37 ms 10324 KB Output is correct
14 Correct 27 ms 7760 KB Output is correct
15 Correct 106 ms 69288 KB Output is correct
16 Correct 111 ms 69124 KB Output is correct
17 Correct 113 ms 69248 KB Output is correct
18 Correct 37 ms 10324 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 12632 KB Output is correct
2 Correct 6 ms 37212 KB Output is correct
3 Correct 5 ms 37456 KB Output is correct
4 Correct 5 ms 37212 KB Output is correct
5 Correct 5 ms 37408 KB Output is correct
6 Correct 5 ms 37208 KB Output is correct
7 Correct 6 ms 37212 KB Output is correct
8 Correct 1 ms 6492 KB Output is correct
9 Correct 1 ms 6612 KB Output is correct
10 Correct 1 ms 6492 KB Output is correct
11 Correct 1 ms 6488 KB Output is correct
12 Correct 2 ms 8540 KB Output is correct
13 Correct 3 ms 16988 KB Output is correct
14 Correct 6 ms 37212 KB Output is correct
15 Correct 5 ms 37212 KB Output is correct
16 Correct 5 ms 37356 KB Output is correct
17 Correct 29 ms 66132 KB Output is correct
18 Correct 29 ms 66360 KB Output is correct
19 Correct 28 ms 66128 KB Output is correct
20 Correct 28 ms 66136 KB Output is correct
21 Correct 28 ms 66128 KB Output is correct
22 Correct 33 ms 66128 KB Output is correct
23 Correct 29 ms 66132 KB Output is correct
24 Correct 28 ms 66112 KB Output is correct
25 Correct 28 ms 66128 KB Output is correct
26 Correct 27 ms 66132 KB Output is correct
27 Correct 28 ms 66140 KB Output is correct
28 Correct 28 ms 66124 KB Output is correct
29 Correct 27 ms 66132 KB Output is correct
30 Correct 28 ms 66132 KB Output is correct
31 Correct 27 ms 66124 KB Output is correct
32 Correct 3 ms 12636 KB Output is correct
33 Correct 3 ms 16732 KB Output is correct
34 Correct 1 ms 6492 KB Output is correct
35 Correct 1 ms 6492 KB Output is correct
36 Correct 1 ms 6492 KB Output is correct
37 Correct 1 ms 6492 KB Output is correct
38 Correct 99 ms 68036 KB Output is correct
39 Correct 89 ms 68076 KB Output is correct
40 Correct 86 ms 68240 KB Output is correct
41 Correct 88 ms 68236 KB Output is correct
42 Correct 83 ms 68176 KB Output is correct
43 Correct 88 ms 68180 KB Output is correct
44 Correct 37 ms 10324 KB Output is correct
45 Correct 27 ms 7760 KB Output is correct
46 Correct 106 ms 69288 KB Output is correct
47 Correct 111 ms 69124 KB Output is correct
48 Correct 113 ms 69248 KB Output is correct
49 Correct 37 ms 10324 KB Output is correct
50 Correct 488 ms 70228 KB Output is correct
51 Correct 384 ms 70188 KB Output is correct
52 Correct 362 ms 70740 KB Output is correct
53 Correct 207 ms 70228 KB Output is correct
54 Correct 200 ms 70392 KB Output is correct
55 Correct 210 ms 70388 KB Output is correct
56 Correct 99 ms 70188 KB Output is correct
57 Correct 101 ms 70228 KB Output is correct
58 Correct 104 ms 70256 KB Output is correct