답안 #948171

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
948171 2024-03-17T18:07:15 Z vladilius Bitaro's travel (JOI23_travel) C++17
15 / 100
3000 ms 79448 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 (i < 2){
            cout<<x[n] - s<<"\n";
            continue;
        }
        if (j >= 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;
        }
        while (i > 1 || j < n){
            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 - 1;
                    }
                }
                if (get1(r + 1, i) > x[j + 1]) l = r;
                l++;
                out += (s - x[l]);
                s = x[l];
                i = l;
            }
            else {
                if (get2(j + 1, n - 1) > x[j + 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 - 1;
                while (l + 1 < r){
                    int m = (l + r) / 2;
                    if (get2(j + 1, m) <= x[j + 1]){
                        r = m;
                    }
                    else {
                        l = m + 1;
                    }
                }
                if (get2(j + 1, l) <= x[j + 1]) r = l;
                out += (x[r] - s);
                s = x[r];
                j = r;
            }
        }
        cout<<out<<"\n";
    }
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 1 ms 856 KB Output is correct
3 Correct 1 ms 860 KB Output is correct
4 Correct 1 ms 904 KB Output is correct
5 Correct 1 ms 860 KB Output is correct
6 Correct 1 ms 860 KB Output is correct
7 Correct 1 ms 860 KB Output is correct
8 Correct 0 ms 344 KB Output is correct
9 Correct 1 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 1 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 1 ms 856 KB Output is correct
15 Correct 1 ms 860 KB Output is correct
16 Correct 1 ms 748 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 1 ms 856 KB Output is correct
3 Correct 1 ms 860 KB Output is correct
4 Correct 1 ms 904 KB Output is correct
5 Correct 1 ms 860 KB Output is correct
6 Correct 1 ms 860 KB Output is correct
7 Correct 1 ms 860 KB Output is correct
8 Correct 0 ms 344 KB Output is correct
9 Correct 1 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 1 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 1 ms 856 KB Output is correct
15 Correct 1 ms 860 KB Output is correct
16 Correct 1 ms 748 KB Output is correct
17 Correct 117 ms 77396 KB Output is correct
18 Correct 149 ms 77392 KB Output is correct
19 Correct 117 ms 77420 KB Output is correct
20 Correct 119 ms 77436 KB Output is correct
21 Correct 131 ms 77396 KB Output is correct
22 Correct 147 ms 77444 KB Output is correct
23 Correct 120 ms 77540 KB Output is correct
24 Correct 130 ms 77396 KB Output is correct
25 Correct 141 ms 77392 KB Output is correct
26 Correct 137 ms 77532 KB Output is correct
27 Correct 137 ms 77792 KB Output is correct
28 Correct 128 ms 77396 KB Output is correct
29 Correct 143 ms 77396 KB Output is correct
30 Correct 139 ms 77396 KB Output is correct
31 Correct 136 ms 77464 KB Output is correct
# 결과 실행 시간 메모리 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 0 ms 452 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 0 ms 344 KB Output is correct
7 Execution timed out 3040 ms 79448 KB Time limit exceeded
8 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 1 ms 856 KB Output is correct
3 Correct 1 ms 860 KB Output is correct
4 Correct 1 ms 904 KB Output is correct
5 Correct 1 ms 860 KB Output is correct
6 Correct 1 ms 860 KB Output is correct
7 Correct 1 ms 860 KB Output is correct
8 Correct 0 ms 344 KB Output is correct
9 Correct 1 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 1 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 1 ms 856 KB Output is correct
15 Correct 1 ms 860 KB Output is correct
16 Correct 1 ms 748 KB Output is correct
17 Correct 117 ms 77396 KB Output is correct
18 Correct 149 ms 77392 KB Output is correct
19 Correct 117 ms 77420 KB Output is correct
20 Correct 119 ms 77436 KB Output is correct
21 Correct 131 ms 77396 KB Output is correct
22 Correct 147 ms 77444 KB Output is correct
23 Correct 120 ms 77540 KB Output is correct
24 Correct 130 ms 77396 KB Output is correct
25 Correct 141 ms 77392 KB Output is correct
26 Correct 137 ms 77532 KB Output is correct
27 Correct 137 ms 77792 KB Output is correct
28 Correct 128 ms 77396 KB Output is correct
29 Correct 143 ms 77396 KB Output is correct
30 Correct 139 ms 77396 KB Output is correct
31 Correct 136 ms 77464 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 0 ms 452 KB Output is correct
36 Correct 1 ms 348 KB Output is correct
37 Correct 0 ms 344 KB Output is correct
38 Execution timed out 3040 ms 79448 KB Time limit exceeded
39 Halted 0 ms 0 KB -