Submission #948984

# Submission time Handle Problem Language Result Execution time Memory
948984 2024-03-18T18:08:18 Z vladilius Bitaro's travel (JOI23_travel) C++17
100 / 100
857 ms 82056 KB
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define int ll
 
vector<vector<int>> sp1, sp2;
vector<int> lg;
 
int get1(int l, int r){
    int k = lg[r - l + 1];
    return max(sp1[l][k], sp1[r - (1 << k) + 1][k]);
}
 
int get2(int l, int r){
    int k = lg[r - l + 1];
    return min(sp2[l][k], sp2[r - (1 << k) + 1][k]);
}
 
signed main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    
    int n; cin>>n;
    vector<int> x(n + 2);
    for (int i = 1; i <= n; i++){
        cin>>x[i];
    }
    x[0] = x[1];
    x[n + 1] = x[n];
    int q; cin>>q;
    vector<int> :: iterator it;
    auto left = [&](int k) -> int{
        if (k == x[n]) return n;
        it = upper_bound(x.begin() + 1, x.end(), k);
        return (int) (prev(it) - x.begin());
    };
    auto right = [&](int k) -> int{
        it = lower_bound(x.begin() + 1, x.end(), k);
        return (int) (it - x.begin());
    };
    
    lg.resize(n + 1);
    for (int i = 2; i <= n; i++){
        lg[i] = lg[i / 2] + 1;
    }
    
    sp1.resize(n + 1);
    sp2.resize(n + 1);
    for (int i = 1; i <= n; i++){
        sp1[i].resize(lg[n] + 1);
        sp2[i].resize(lg[n] + 1);
    }
    for (int i = 1; i <= n; i++){
        sp1[i][0] = 2 * x[i] - x[i - 1];
        sp2[i][0] = 2 * x[i] - x[i + 1];
    }
    for (int j = 1; j <= lg[n]; j++){
        for (int i = 1; i + (1 << j) <= n + 1; i++){
            sp1[i][j] = max(sp1[i][j - 1], sp1[i + (1 << (j - 1))][j - 1]);
            sp2[i][j] = min(sp2[i][j - 1], sp2[i + (1 << (j - 1))][j - 1]);
        }
    }
    
    while (q--){
        int s; cin>>s;
        ll out = 0;
        int i = left(s), j = right(s);
        if (s <= x[1]){
            cout<<x[n] - s<<"\n";
            continue;
        }
        if (s >= x[n]){
            cout<<(s - x[1])<<"\n";
            continue;
        }
        if (s - x[i] <= x[j] - s){
            out += (s - x[i]);
            s = x[i];
            j = i;
        }
        else {
            out += (x[j] - s);
            s = x[j];
            i = j;
        }
        int T = 50;
        while ((i > 1 || j < n) && T--){
            if (i == 1){
                out += (x[n] - s);
                break;
            }
            if (j == n){
                out += (s - x[1]);
                break;
            }
            int d1 = s - x[i - 1], d2 = x[j + 1] - s;
            if (d1 <= d2){
                if (get1(2, i) <= x[j + 1]){
                    out += (s - x[1]);
                    s = x[1];
                    i = 1;
                    continue;
                }
                
                // max(k < i): x[k + 1] - x[k] > x[j + 1] - x[k + 1]
                // <-> 2 * x[k + 1] - x[k] > x[j + 1]
                // <-> sp1[k + 1] > x[j + 1]
                // sp1[k + 1][0], ... sp1[i][0]
                
                int l = 1, r = i - 1;
                while (l + 1 < r){
                    int m = (l + r) / 2;
                    if (get1(m + 1, i) > x[j + 1]){
                        l = m;
                    }
                    else {
                        r = m;
                    }
                }
                while (r > 1 && get1(r + 1, i) <= x[j + 1]) r--;
                r++;
                out += (s - x[r]);
                s = x[r];
                i = r;
            }
            else {
                if (get2(j + 1, n) > x[i - 1]){
                    out += (x[n] - s);
                    s = x[n];
                    j = n;
                    continue;
                }
                
                // min(k > j): x[k + 1] - x[k] >= x[k] - x[i - 1]
                // <-> 2 * x[k] - x[k + 1] <= x[i - 1]
                // <-> sp2[k][0] <= x[i - 1]
                // sp2[j + 1][0], ... sp2[k][0]
                
                int l = j + 1, r = n;
                while (l + 1 < r){
                    int m = (l + r) / 2;
                    if (get2(j + 1, m) <= x[i - 1]){
                        r = m;
                    }
                    else {
                        l = m;
                    }
                }
                while (l <= n && get2(j + 1, l) > x[i - 1]) l++;
                out += (x[l] - s);
                s = x[l];
                j = l;
            }
        }
        cout<<out<<"\n";
    }
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 1 ms 860 KB Output is correct
3 Correct 1 ms 860 KB Output is correct
4 Correct 1 ms 860 KB Output is correct
5 Correct 1 ms 860 KB Output is correct
6 Correct 1 ms 856 KB Output is correct
7 Correct 1 ms 860 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 344 KB Output is correct
13 Correct 1 ms 344 KB Output is correct
14 Correct 1 ms 732 KB Output is correct
15 Correct 1 ms 860 KB Output is correct
16 Correct 1 ms 860 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 1 ms 860 KB Output is correct
3 Correct 1 ms 860 KB Output is correct
4 Correct 1 ms 860 KB Output is correct
5 Correct 1 ms 860 KB Output is correct
6 Correct 1 ms 856 KB Output is correct
7 Correct 1 ms 860 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 344 KB Output is correct
13 Correct 1 ms 344 KB Output is correct
14 Correct 1 ms 732 KB Output is correct
15 Correct 1 ms 860 KB Output is correct
16 Correct 1 ms 860 KB Output is correct
17 Correct 131 ms 75772 KB Output is correct
18 Correct 127 ms 75400 KB Output is correct
19 Correct 123 ms 75348 KB Output is correct
20 Correct 138 ms 75344 KB Output is correct
21 Correct 132 ms 75496 KB Output is correct
22 Correct 126 ms 75588 KB Output is correct
23 Correct 124 ms 75600 KB Output is correct
24 Correct 123 ms 75600 KB Output is correct
25 Correct 124 ms 75476 KB Output is correct
26 Correct 123 ms 75348 KB Output is correct
27 Correct 123 ms 75604 KB Output is correct
28 Correct 131 ms 75760 KB Output is correct
29 Correct 132 ms 75348 KB Output is correct
30 Correct 121 ms 75348 KB Output is correct
31 Correct 122 ms 75356 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 206 ms 76884 KB Output is correct
8 Correct 206 ms 79452 KB Output is correct
9 Correct 220 ms 79436 KB Output is correct
10 Correct 215 ms 79344 KB Output is correct
11 Correct 216 ms 79292 KB Output is correct
12 Correct 222 ms 79444 KB Output is correct
13 Correct 36 ms 4096 KB Output is correct
14 Correct 31 ms 1868 KB Output is correct
15 Correct 229 ms 80352 KB Output is correct
16 Correct 219 ms 80468 KB Output is correct
17 Correct 221 ms 80472 KB Output is correct
18 Correct 32 ms 4176 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 1 ms 860 KB Output is correct
3 Correct 1 ms 860 KB Output is correct
4 Correct 1 ms 860 KB Output is correct
5 Correct 1 ms 860 KB Output is correct
6 Correct 1 ms 856 KB Output is correct
7 Correct 1 ms 860 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 344 KB Output is correct
13 Correct 1 ms 344 KB Output is correct
14 Correct 1 ms 732 KB Output is correct
15 Correct 1 ms 860 KB Output is correct
16 Correct 1 ms 860 KB Output is correct
17 Correct 131 ms 75772 KB Output is correct
18 Correct 127 ms 75400 KB Output is correct
19 Correct 123 ms 75348 KB Output is correct
20 Correct 138 ms 75344 KB Output is correct
21 Correct 132 ms 75496 KB Output is correct
22 Correct 126 ms 75588 KB Output is correct
23 Correct 124 ms 75600 KB Output is correct
24 Correct 123 ms 75600 KB Output is correct
25 Correct 124 ms 75476 KB Output is correct
26 Correct 123 ms 75348 KB Output is correct
27 Correct 123 ms 75604 KB Output is correct
28 Correct 131 ms 75760 KB Output is correct
29 Correct 132 ms 75348 KB Output is correct
30 Correct 121 ms 75348 KB Output is correct
31 Correct 122 ms 75356 KB Output is correct
32 Correct 0 ms 348 KB Output is correct
33 Correct 0 ms 348 KB Output is correct
34 Correct 0 ms 348 KB Output is correct
35 Correct 1 ms 348 KB Output is correct
36 Correct 1 ms 348 KB Output is correct
37 Correct 0 ms 348 KB Output is correct
38 Correct 206 ms 76884 KB Output is correct
39 Correct 206 ms 79452 KB Output is correct
40 Correct 220 ms 79436 KB Output is correct
41 Correct 215 ms 79344 KB Output is correct
42 Correct 216 ms 79292 KB Output is correct
43 Correct 222 ms 79444 KB Output is correct
44 Correct 36 ms 4096 KB Output is correct
45 Correct 31 ms 1868 KB Output is correct
46 Correct 229 ms 80352 KB Output is correct
47 Correct 219 ms 80468 KB Output is correct
48 Correct 221 ms 80472 KB Output is correct
49 Correct 32 ms 4176 KB Output is correct
50 Correct 857 ms 81504 KB Output is correct
51 Correct 790 ms 81740 KB Output is correct
52 Correct 694 ms 81500 KB Output is correct
53 Correct 371 ms 82056 KB Output is correct
54 Correct 379 ms 81648 KB Output is correct
55 Correct 365 ms 81468 KB Output is correct
56 Correct 196 ms 81488 KB Output is correct
57 Correct 190 ms 81368 KB Output is correct
58 Correct 217 ms 81488 KB Output is correct