結果

問題 No.875 Range Mindex Query
ユーザー pes_magicpes_magic
提出日時 2020-01-28 00:33:34
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 289 ms / 2,000 ms
コード長 1,650 bytes
コンパイル時間 703 ms
コンパイル使用メモリ 75,400 KB
実行使用メモリ 5,284 KB
最終ジャッジ日時 2023-10-12 18:15:47
合計ジャッジ時間 4,136 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,352 KB
testcase_01 AC 2 ms
4,352 KB
testcase_02 AC 4 ms
4,348 KB
testcase_03 AC 2 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,352 KB
testcase_08 AC 2 ms
4,352 KB
testcase_09 AC 2 ms
4,356 KB
testcase_10 AC 3 ms
4,348 KB
testcase_11 AC 242 ms
5,092 KB
testcase_12 AC 196 ms
4,348 KB
testcase_13 AC 170 ms
5,284 KB
testcase_14 AC 169 ms
5,092 KB
testcase_15 AC 233 ms
5,196 KB
testcase_16 AC 272 ms
5,232 KB
testcase_17 AC 289 ms
5,188 KB
testcase_18 AC 286 ms
5,136 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>

using namespace std;

template<typename T>
class SegTree {
public:
    explicit SegTree(int n, T def) : N(calcN_(n)), def(def), mVal(2*calcN_(n)-1, def) {}
    void update(int idx, T value){
        int i = N + idx - 1;
        update_(mVal[i], value);
        while(i > 0){
            i = (i-1)/2;
            mVal[i] = operate(mVal[2*i+1], mVal[2*i+2]);
        }
    }
    T get(int l, int r){
        return getImpl_(l, r, 0, 0, N);
    }
private:
    int calcN_(int n){
        int res = 1;
        while(res < n) res *= 2;
        return res;
    }
    T getImpl_(int l, int r, int idx, int rangeL, int rangeR){
        if(r <= rangeL || rangeR <= l) return def;
        if(l <= rangeL && rangeR <= r) return mVal[idx];
        int rangeM = (rangeL+rangeR)/2;
        T a = getImpl_(l, r, 2*idx+1, rangeL, rangeM);
        T b = getImpl_(l, r, 2*idx+2, rangeM, rangeR);
        return operate(a, b);
    }
    void update_(T& data, T val) { data = val; }
    T operate(T a, T b) { return min(a, b); }
    const int N;
    const T def;
    vector<T> mVal; 
};

int main(){
    int N, Q; cin >> N >> Q;
    SegTree<pair<int,int>> seg(N+1, make_pair(N+1, N+1));
    for(int i=1;i<=N;i++){
        int a; cin >> a;
        seg.update(i, make_pair(a, i));
    }
    for(int i=0;i<Q;i++){
        int q, l, r; cin >> q >> l >> r;
        if(q == 1){
            int a = seg.get(l, l+1).first;
            int b = seg.get(r, r+1).first;
            seg.update(l, make_pair(b, l));
            seg.update(r, make_pair(a, r));
        } else {
            cout << seg.get(l, r+1).second << endl;
        }
    }
}
0