結果

問題 No.875 Range Mindex Query
ユーザー kya_skikya_ski
提出日時 2020-03-10 20:34:48
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 289 ms / 2,000 ms
コード長 2,203 bytes
コンパイル時間 1,465 ms
コンパイル使用メモリ 175,360 KB
実行使用メモリ 6,992 KB
最終ジャッジ日時 2024-04-27 18:50:50
合計ジャッジ時間 4,069 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,812 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 2 ms
6,940 KB
testcase_04 AC 2 ms
6,944 KB
testcase_05 AC 2 ms
6,940 KB
testcase_06 AC 3 ms
6,940 KB
testcase_07 AC 2 ms
6,940 KB
testcase_08 AC 2 ms
6,940 KB
testcase_09 AC 2 ms
6,940 KB
testcase_10 AC 3 ms
6,940 KB
testcase_11 AC 186 ms
6,944 KB
testcase_12 AC 156 ms
6,944 KB
testcase_13 AC 122 ms
6,944 KB
testcase_14 AC 127 ms
6,940 KB
testcase_15 AC 163 ms
6,992 KB
testcase_16 AC 222 ms
6,940 KB
testcase_17 AC 289 ms
6,988 KB
testcase_18 AC 235 ms
6,984 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

template<typename T, typename F, typename G>
struct SegmentTree {
private :
    int n;
    T id;
    vector<T> node;
    F f;
    G g;
public :

    SegmentTree () { }

    SegmentTree (int sz, F f, G g, T id) : n(1), f(f), g(g), id(id) {
        while (n < sz) n *= 2;
        node = vector<T>(2*n-1, id);
    }

    SegmentTree (vector<T> v, F f, G g, T id) : n(1), f(f), g(g), id(id) {
        while (n < (int)v.size()) n *= 2;
        node = vector<T>(2*n-1, id);
        for (int i = 0; i < (int)v.size(); i++) node[i+n-1] = v[i];
        for (int i = n-2; i >=0; i--) node[i] = f(node[i*2+1], node[i*2+2]);
    }

    void update (int i, T x) {
        i += n - 1;
        assert(i < node.size());
        node[i] = g(node[i], x);
        while (i > 0) {
            i = (i-1)/2;
            node[i] = f(node[2*i+1], node[2*i+2]);
        }
    }

    T query (int a, int b, int k, int l, int r) {
        if (r <= a || b <= l) return id;
        if (a <= l && r <= b) return node[k];
        T vl = query(a, b, 2*k+1, l, (l+r)/2);
        T vr = query(a, b, 2*k+2, (l+r)/2, r);
        return f(vl, vr);
    }

    T query (int a, int b) { return query(a, b, 0, 0, n); }

    const T &operator[] (int i) const { return node[i+n-1]; } 

};


int main() {
    using P = pair<int, int>;
    const P id = make_pair(INT_MAX, INT_MAX);
    
    int n, q;
    cin >> n >> q;
    vector<P> vec;
    for (int i = 0; i < n; i++) {
        int e;
        cin >> e;
        vec.emplace_back(e, i+1);
    }
    
    auto f = [](const P &a, const P &b) { return min(a, b); };
    auto g = [](const P &a, const P &b) { return b; };
    SegmentTree<P, decltype(f), decltype(g)> seg(vec, f, g, id);
    
    auto swap_val = [&] (int l, int r) -> void {
        P vl = seg[--l], vr = seg[--r];
        swap(vl.first, vr.first);
        seg.update(l, vl);
        seg.update(r, vr);
    };
    
    auto min_idx = [&] (int l, int r) -> int {
        return (seg.query(--l, r).second);
    };
    
    while (q--) {
        int op, l, r;
        cin >> op >> l >> r;
        if (op == 1) swap_val(l, r);
        else cout << min_idx(l, r) << '\n';
    }
    return 0;
}
0