結果

問題 No.875 Range Mindex Query
ユーザー betrue12betrue12
提出日時 2019-09-06 21:52:55
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 291 ms / 2,000 ms
コード長 1,677 bytes
コンパイル時間 1,769 ms
コンパイル使用メモリ 175,800 KB
実行使用メモリ 5,552 KB
最終ジャッジ日時 2023-09-06 23:39:47
合計ジャッジ時間 4,825 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 4 ms
4,380 KB
testcase_03 AC 2 ms
4,384 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 3 ms
4,376 KB
testcase_07 AC 3 ms
4,376 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,384 KB
testcase_10 AC 3 ms
4,380 KB
testcase_11 AC 210 ms
5,372 KB
testcase_12 AC 176 ms
4,380 KB
testcase_13 AC 151 ms
5,376 KB
testcase_14 AC 147 ms
5,360 KB
testcase_15 AC 202 ms
5,532 KB
testcase_16 AC 274 ms
5,552 KB
testcase_17 AC 291 ms
5,356 KB
testcase_18 AC 275 ms
5,532 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

template<typename T>
struct Segtree {
    int n;
    T e;
    vector<T> dat;
    typedef function<T(T a, T b)> Func;
    Func f;

    Segtree(){}
    Segtree(int n_input, Func f_input, T e_input){
        initialize(n_input, f_input, e_input);
    }
    void initialize(int n_input, Func f_input, T e_input){
        f = f_input;
        e = e_input;
        n = 1;
        while(n < n_input) n <<= 1;
        dat.resize(2*n-1, e);
    }

    void update(int k, T a){
        k += n - 1;
        dat[k] = a;
        while(k > 0){
            k = (k - 1)/2;
            dat[k] = f(dat[2*k+1], dat[2*k+2]);
        }
    }

    T get(int k){
        return dat[k+n-1];
    }

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

    T query(int a, int b, int k, int l, int r){
        if(r<=a || b<=l) return e;
        if(a<=l && r<=b) return dat[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);
    }
};

int main(){
    int N, Q;
    cin >> N >> Q;
    vector<int> A(N);
    for(int i=0; i<N; i++) cin >> A[i];
    typedef pair<int, int> P;
    const P INFP = {1e9, 1e9};
    Segtree<P> st(N, [](auto a, auto b){ return min(a, b);}, INFP);
    for(int i=0; i<N; i++) st.update(i, {A[i], i+1});
    while(Q--){
        int t, l, r;
        cin >> t >> l >> r;
        l--; r--;
        if(t == 1){
            swap(A[l], A[r]);
            st.update(l, {A[l], l+1});
            st.update(r, {A[r], r+1});
        }else{
            int ans = st.between(l, r).second;
            cout << ans << endl;
        }
    }
    return 0;
}
0