結果

問題 No.2248 max(C)-min(C)
ユーザー ruthenruthen
提出日時 2023-03-17 23:38:49
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 207 ms / 3,000 ms
コード長 4,535 bytes
コンパイル時間 2,990 ms
コンパイル使用メモリ 215,712 KB
実行使用メモリ 25,412 KB
最終ジャッジ日時 2023-10-18 16:34:57
合計ジャッジ時間 10,152 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 3 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 3 ms
4,348 KB
testcase_06 AC 3 ms
4,348 KB
testcase_07 AC 3 ms
4,348 KB
testcase_08 AC 3 ms
4,348 KB
testcase_09 AC 3 ms
4,348 KB
testcase_10 AC 2 ms
4,348 KB
testcase_11 AC 3 ms
4,348 KB
testcase_12 AC 3 ms
4,348 KB
testcase_13 AC 3 ms
4,348 KB
testcase_14 AC 3 ms
4,348 KB
testcase_15 AC 3 ms
4,348 KB
testcase_16 AC 3 ms
4,348 KB
testcase_17 AC 3 ms
4,348 KB
testcase_18 AC 171 ms
20,996 KB
testcase_19 AC 19 ms
5,896 KB
testcase_20 AC 204 ms
25,412 KB
testcase_21 AC 191 ms
24,932 KB
testcase_22 AC 127 ms
19,204 KB
testcase_23 AC 78 ms
11,888 KB
testcase_24 AC 28 ms
6,924 KB
testcase_25 AC 177 ms
24,548 KB
testcase_26 AC 154 ms
19,820 KB
testcase_27 AC 173 ms
24,452 KB
testcase_28 AC 16 ms
5,248 KB
testcase_29 AC 156 ms
20,348 KB
testcase_30 AC 60 ms
11,560 KB
testcase_31 AC 207 ms
25,412 KB
testcase_32 AC 32 ms
7,296 KB
testcase_33 AC 53 ms
10,960 KB
testcase_34 AC 196 ms
25,412 KB
testcase_35 AC 196 ms
25,412 KB
testcase_36 AC 191 ms
25,412 KB
testcase_37 AC 99 ms
14,780 KB
testcase_38 AC 183 ms
25,412 KB
testcase_39 AC 191 ms
25,412 KB
testcase_40 AC 189 ms
25,412 KB
testcase_41 AC 158 ms
20,804 KB
testcase_42 AC 186 ms
25,412 KB
testcase_43 AC 170 ms
24,332 KB
testcase_44 AC 191 ms
25,412 KB
testcase_45 AC 147 ms
20,284 KB
testcase_46 AC 70 ms
11,744 KB
testcase_47 AC 192 ms
25,412 KB
testcase_48 AC 133 ms
25,412 KB
testcase_49 AC 187 ms
25,412 KB
testcase_50 AC 193 ms
25,412 KB
testcase_51 AC 200 ms
25,412 KB
testcase_52 AC 189 ms
25,412 KB
testcase_53 AC 26 ms
6,912 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

#ifdef _RUTHEN
#include <debug.hpp>
#else
#define show(...) true
#endif

using ll = long long;
#define rep(i, n) for (int i = 0; i < (n); i++)
template <class T> using V = vector<T>;

template <class Monoid> struct SegmentTree {
   public:
    using S = typename Monoid::value_type;
    SegmentTree() : SegmentTree(0) {}
    SegmentTree(int n) : SegmentTree(std::vector<S>(n, Monoid::e())) {}
    SegmentTree(const std::vector<S>& v) : _n((int)v.size()) {
        log = 0;
        while ((1U << log) < (unsigned int)(_n)) log++;
        size = 1 << log;
        d = std::vector<S>(size << 1, Monoid::e());
        for (int i = 0; i < _n; i++) d[i + size] = v[i];
        for (int i = size - 1; i >= 1; i--) {
            update(i);
        }
    }

    void set(int p, const S& x) {
        assert(0 <= p and p < _n);
        p += size;
        d[p] = x;
        for (int i = 1; i <= log; i++) update(p >> i);
    }

    void chset(int p, const S& x) {
        assert(0 <= p and p < _n);
        p += size;
        d[p] = Monoid::op(d[p], x);
        for (int i = 1; i <= log; i++) update(p >> i);
    }

    S operator[](int p) const {
        assert(0 <= p and p < _n);
        return d[p + size];
    }

    S get(int p) const {
        assert(0 <= p && p < _n);
        return d[p + size];
    }

    S prod(int l, int r) const {
        assert(0 <= l and l <= r and r <= _n);
        S sml = Monoid::e(), smr = Monoid::e();
        l += size;
        r += size;

        while (l < r) {
            if (l & 1) sml = Monoid::op(sml, d[l++]);
            if (r & 1) smr = Monoid::op(d[--r], smr);
            l >>= 1;
            r >>= 1;
        }
        return Monoid::op(sml, smr);
    }

    S all_prod() const { return d[1]; }

    template <class F> int max_right(int l, F& f) const {
        assert(0 <= l and l <= _n);
        assert(f(Monoid::e()));
        if (l == _n) return _n;
        l += size;
        S sm = Monoid::e();
        do {
            while ((l & 1) == 0) l >>= 1;
            if (!f(Monoid::op(sm, d[l]))) {
                while (l < size) {
                    l <<= 1;
                    if (f(Monoid::op(sm, d[l]))) {
                        sm = Monoid::op(sm, d[l]);
                        l++;
                    }
                }
                return l - size;
            }
            sm = Monoid::op(sm, d[l]);
            l++;
        } while ((l & -l) != l);  // 2べきまたは0のときfalse
        return _n;
    }

    template <class F> int min_left(int r, F& f) const {
        assert(0 <= r and r <= _n);
        assert(f(Monoid::e()));
        if (r == 0) return 0;
        r += size;
        S sm = Monoid::e();
        do {
            r--;
            while (r > 1 and (r & 1)) r >>= 1;
            if (!f(Monoid::op(d[r], sm))) {
                while (r < size) {
                    r = (r << 1) | 1;
                    if (f(Monoid::op(d[r], sm))) {
                        sm = Monoid::op(d[r], sm);
                        r--;
                    }
                }
                return r + 1 - size;
            }
            sm = Monoid::op(d[r], sm);
        } while ((r & -r) != r);  // 2べきまたは0のときfalse
        return 0;
    }

   private:
    int _n, log, size;
    std::vector<S> d;
    inline void update(int k) { d[k] = Monoid::op(d[k << 1], d[(k << 1) | 1]); }
};

template <class S> struct MonoidMax {
    using value_type = S;
    static constexpr S op(S a, S b) { return std::max(a, b); }
    static constexpr S e() { return std::numeric_limits<S>::lowest(); }
};

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    int N;
    cin >> N;
    V<long long> A(N), B(N);
    rep(i, N) cin >> A[i];
    rep(i, N) cin >> B[i];
    V<long long> C(N);
    rep(i, N) {
        if (A[i] > B[i]) swap(A[i], B[i]);
        C[i] = (A[i] + B[i]) / 2;
    }
    V<tuple<long long, int, int>> G;
    rep(i, N) {
        G.push_back({A[i], 0, i});
        G.push_back({C[i], 1, i});
        G.push_back({B[i], 2, i});
    }
    SegmentTree<MonoidMax<long long>> seg(N);
    rep(i, N) seg.set(i, A[i]);
    sort(G.begin(), G.end());
    constexpr long long INF = 1LL << 60;
    long long ans = INF;
    for (auto [v, t, i] : G) {
        ans = min(ans, seg.all_prod() - v);
        if (t == 0) {
            seg.set(i, C[i]);
        } else if (t == 1) {
            seg.set(i, B[i]);
        } else {
            seg.set(i, INF);
        }
    }
    cout << ans << '\n';
    return 0;
}
0