답안 #748465

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
748465 2023-05-26T10:24:14 Z Desh03 Bitaro's travel (JOI23_travel) C++17
100 / 100
221 ms 9792 KB
#include <bits/stdc++.h>
using namespace std;

struct segtree {
    vector<int> tree;
    int sz, b, t;
    int merge(int a, int b) {
        return t ? min(a, b) : max(a, b);
    }
    segtree(vector<int> a, int tp) {
        t = tp;
        b = t ? 2e9 : -2e9;
        sz = a.size();
        while (sz & sz - 1) ++sz;
        tree.resize(sz << 1, b);
        for (int i = 0; i < a.size(); i++)
            tree[i + sz] = a[i];
        for (int i = sz - 1; i >= 1; i--)
            tree[i] = merge(tree[i << 1], tree[i << 1 | 1]);
    }
    int find_first(int v, int l, int r, int ql, int qr, int x) {
        if (l > qr || r < ql) return -1;
        if (l >= ql && r <= qr) {
            if (tree[v] > x) return -1;
            while (l < r) {
                int m = (l + r) >> 1;
                if (tree[v << 1] <= x) v <<= 1, r = m;
                else v = v << 1 | 1, l = m + 1;
            }
            return l;
        }
        int m = (l + r) >> 1;
        int left = find_first(v << 1, l, m, ql, qr, x);
        if (left != -1) return left;
        return find_first(v << 1 | 1, m + 1, r, ql, qr, x);
    }
    int find_last(int v, int l, int r, int ql, int qr, int x) {
        if (l > qr || r < ql) return -1;
        if (l >= ql && r <= qr) {
            if (tree[v] <= x) return -1;
            while (l < r) {
                int m = (l + r) >> 1;
                if (tree[v << 1 | 1] > x) v = v << 1 | 1, l = m + 1;
                else v <<= 1, r = m;
            }
            return l;
        }
        int m = (l + r) >> 1;
        int right = find_last(v << 1 | 1, m + 1, r, ql, qr, x);
        if (right != -1) return right;
        return find_last(v << 1, l, m, ql, qr, x);
    }
    int find_first(int l, int r, int x) {
        return find_first(1, 0, sz - 1, l, r, x);
    }
    int find_last(int l, int r, int x) {
        return find_last(1, 0, sz - 1, l, r, x);
    }
};

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
	int n;
    cin >> n;
    vector<int> diff(n), x(n), diff2(n);
    for (int i = 0; i < n; i++)
        cin >> x[i];
    for (int i = 1; i < n; i++)
        diff[i] = 2 * x[i] - x[i - 1];
    for (int i = 0; i < n - 1; i++)
        diff2[i] = 2 * x[i] - x[i + 1];
    diff[0] = 2e9, diff2[n - 1] = -2e9;
    segtree tree1(diff, 0), tree2(diff2, 1);
    int q;
    cin >> q;
    while (q--) {
        int p;
        cin >> p;
        auto it = lower_bound(x.begin(), x.end(), p);
        int k;
        if (it != x.end()) {
            k = it - x.begin();
            if (it != x.begin()) {
                it--;
                if (p - *it <= x[k] - p)
                    k = it - x.begin();
            }
        } else k = n - 1;
        int l = k, r = k, ans = abs(p - x[k]), cur = k;
        while (l != 0 || r != n - 1) {
            if (r == n - 1) {
                ans += x[cur] - x[0];
                break;
            }
            if (l == 0) {
                ans += x[n - 1] - x[cur];
                break;
            }
            if (cur ^ r) {
                int left = tree1.find_last(0, l, x[r + 1]);
                ans += x[cur] - (x[left] << 1) + x[r + 1];
                l = left, cur = ++r;
            } else {
                int right = tree2.find_first(r, n - 1, x[l - 1]);
                ans += (x[right] << 1) - x[cur] - x[l - 1];
                r = right, cur = --l;
            }
        }
        cout << ans << '\n';
    }
}

Compilation message

travel.cpp: In constructor 'segtree::segtree(std::vector<int>, int)':
travel.cpp:14:24: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
   14 |         while (sz & sz - 1) ++sz;
      |                     ~~~^~~
travel.cpp:16:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   16 |         for (int i = 0; i < a.size(); i++)
      |                         ~~^~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 23 ms 7532 KB Output is correct
18 Correct 31 ms 7524 KB Output is correct
19 Correct 23 ms 7492 KB Output is correct
20 Correct 24 ms 7484 KB Output is correct
21 Correct 29 ms 7484 KB Output is correct
22 Correct 23 ms 7536 KB Output is correct
23 Correct 23 ms 7664 KB Output is correct
24 Correct 23 ms 7532 KB Output is correct
25 Correct 26 ms 7536 KB Output is correct
26 Correct 24 ms 7536 KB Output is correct
27 Correct 26 ms 7536 KB Output is correct
28 Correct 24 ms 7532 KB Output is correct
29 Correct 22 ms 7540 KB Output is correct
30 Correct 23 ms 7560 KB Output is correct
31 Correct 23 ms 7532 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 132 ms 8964 KB Output is correct
8 Correct 136 ms 8840 KB Output is correct
9 Correct 137 ms 8948 KB Output is correct
10 Correct 133 ms 8864 KB Output is correct
11 Correct 128 ms 8904 KB Output is correct
12 Correct 127 ms 8864 KB Output is correct
13 Correct 38 ms 2252 KB Output is correct
14 Correct 30 ms 868 KB Output is correct
15 Correct 118 ms 9316 KB Output is correct
16 Correct 106 ms 9324 KB Output is correct
17 Correct 110 ms 9348 KB Output is correct
18 Correct 40 ms 2120 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 23 ms 7532 KB Output is correct
18 Correct 31 ms 7524 KB Output is correct
19 Correct 23 ms 7492 KB Output is correct
20 Correct 24 ms 7484 KB Output is correct
21 Correct 29 ms 7484 KB Output is correct
22 Correct 23 ms 7536 KB Output is correct
23 Correct 23 ms 7664 KB Output is correct
24 Correct 23 ms 7532 KB Output is correct
25 Correct 26 ms 7536 KB Output is correct
26 Correct 24 ms 7536 KB Output is correct
27 Correct 26 ms 7536 KB Output is correct
28 Correct 24 ms 7532 KB Output is correct
29 Correct 22 ms 7540 KB Output is correct
30 Correct 23 ms 7560 KB Output is correct
31 Correct 23 ms 7532 KB Output is correct
32 Correct 0 ms 212 KB Output is correct
33 Correct 1 ms 212 KB Output is correct
34 Correct 0 ms 212 KB Output is correct
35 Correct 0 ms 212 KB Output is correct
36 Correct 0 ms 212 KB Output is correct
37 Correct 1 ms 212 KB Output is correct
38 Correct 132 ms 8964 KB Output is correct
39 Correct 136 ms 8840 KB Output is correct
40 Correct 137 ms 8948 KB Output is correct
41 Correct 133 ms 8864 KB Output is correct
42 Correct 128 ms 8904 KB Output is correct
43 Correct 127 ms 8864 KB Output is correct
44 Correct 38 ms 2252 KB Output is correct
45 Correct 30 ms 868 KB Output is correct
46 Correct 118 ms 9316 KB Output is correct
47 Correct 106 ms 9324 KB Output is correct
48 Correct 110 ms 9348 KB Output is correct
49 Correct 40 ms 2120 KB Output is correct
50 Correct 215 ms 9660 KB Output is correct
51 Correct 221 ms 9684 KB Output is correct
52 Correct 193 ms 9792 KB Output is correct
53 Correct 155 ms 9724 KB Output is correct
54 Correct 159 ms 9704 KB Output is correct
55 Correct 161 ms 9736 KB Output is correct
56 Correct 104 ms 9588 KB Output is correct
57 Correct 101 ms 9760 KB Output is correct
58 Correct 98 ms 9684 KB Output is correct