結果

問題 No.875 Range Mindex Query
ユーザー 里旬里旬
提出日時 2019-09-08 00:02:22
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 259 ms / 2,000 ms
コード長 2,122 bytes
コンパイル時間 1,908 ms
コンパイル使用メモリ 201,576 KB
実行使用メモリ 5,412 KB
最終ジャッジ日時 2023-09-09 21:25:20
合計ジャッジ時間 4,762 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 3 ms
4,380 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 3 ms
4,376 KB
testcase_07 AC 3 ms
4,376 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 3 ms
4,376 KB
testcase_10 AC 3 ms
4,376 KB
testcase_11 AC 189 ms
5,232 KB
testcase_12 AC 157 ms
4,376 KB
testcase_13 AC 133 ms
5,284 KB
testcase_14 AC 131 ms
5,284 KB
testcase_15 AC 180 ms
5,112 KB
testcase_16 AC 241 ms
5,184 KB
testcase_17 AC 259 ms
5,284 KB
testcase_18 AC 250 ms
5,412 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

template<typename Tp>
class segtree{
    Tp *tree;
    Tp (*operation)(const Tp&, const Tp&);
    Tp ident;
    size_t len, depth;
public:
    segtree(size_t n, Tp (*op)(const Tp&, const Tp&), Tp id=0)
    : operation(op), ident(id){
        for(depth=1;(1ULL<<depth)<n;++depth);
        len = 1ULL<<depth;
        ++depth;
        tree = new Tp[2*len];
        for(size_t i=0;i<2*len;++i) tree[i] = id;
    }
    ~segtree(){
        delete[] tree;
    }
    void update(size_t pos, Tp val){
        tree[len+pos] = val;
        for(size_t p=(len+pos)>>1;p>0;p>>=1){
            tree[p] = operation(tree[2*p], tree[2*p+1]);
        }
    }
    void add(size_t pos, Tp val){
        tree[len+pos] += val;
        for(size_t p=(len+pos)>>1;p>0;p>>=1){
            tree[p] = operation(tree[2*p], tree[2*p+1]);
        }
    }
    Tp queue(size_t left, size_t right){
        Tp lval = ident, rval = ident;
        left += len;
        right += len;
        while(true){
            if(left >= right) return operation(lval, rval);
            if(right - left == 1) return operation(lval, operation(tree[left], rval));
            if(left & 1) lval = operation(lval, tree[left++]);
            left >>= 1;
            if(right & 1) rval = operation(tree[right-1], rval);
            right >>= 1;
        }
    }
    Tp &operator[](size_t pos){
        return tree[len+pos];
    }
    void refresh(){
        for(size_t p=len-1;p>0;--p){
            tree[p] = operation(tree[2*p], tree[2*p+1]);
        }
    }
};

int main(){
    int N, Q;
    int t, l, r;
    cin >> N >> Q;
    segtree<pair<int,int>> st(N, [](auto x, auto y){ return min(x, y); }, {N+1, -1});

    for(int i=0;i<N;++i){
        int a;
        cin >> a;
        st[i] = {a, i+1};
    }
    st.refresh();

    for(int i=0;i<Q;++i){
        cin >> t >> l >> r;
        if(t==1){
            int tmp = st[l-1].first;
            st.update(l-1, {st[r-1].first, st[l-1].second});
            st.update(r-1, {tmp, st[r-1].second});
        }
        else cout << st.queue(l-1, r).second << endl;
    }

    return 0;
}
0