結果

問題 No.875 Range Mindex Query
ユーザー 50m_regent50m_regent
提出日時 2019-09-11 14:48:38
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 292 ms / 2,000 ms
コード長 1,934 bytes
コンパイル時間 1,448 ms
コンパイル使用メモリ 159,440 KB
実行使用メモリ 6,884 KB
最終ジャッジ日時 2023-09-15 13:48:18
合計ジャッジ時間 4,389 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 3 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,380 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 3 ms
4,380 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 3 ms
4,376 KB
testcase_10 AC 3 ms
4,380 KB
testcase_11 AC 206 ms
6,484 KB
testcase_12 AC 168 ms
4,884 KB
testcase_13 AC 141 ms
6,612 KB
testcase_14 AC 140 ms
6,572 KB
testcase_15 AC 191 ms
6,468 KB
testcase_16 AC 269 ms
6,480 KB
testcase_17 AC 292 ms
6,876 KB
testcase_18 AC 285 ms
6,884 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define _overload(_1, _2, _3, name, ...) name
#define _rep(i, n) repi(i, 0, n)
#define repi(i, a, b) for(int i = (int)(a); i < (int)(b); i++)
#define rep(...) _overload(__VA_ARGS__, repi, _rep)(__VA_ARGS__)
#define _rev(i, n) revi(i, n, 0)
#define revi(i, a, b) for(int i = (int)(a - 1); i >= (int)(b); i--)
#define rev(...) _overload(__VA_ARGS__, revi, _rev)(__VA_ARGS__)
#define each(i, n) for(auto&& i: n)
int INF = 2147483647, MOD = 1e9 + 7;
struct SegmentTree{
    int n = 2;
    vector<pair<int, int>> t;
    SegmentTree(vector<pair<int, int>> v){
        while(n < v.size()) n *= 2;
        t.resize(2 * n - 1, make_pair(INF, -1));
        rep(i, v.size()) t[i + n - 1] = v[i];
        rev(i, n - 1) t[i] = min(t[2 * i + 1], t[2 * i + 2]); // 区間最小
    }
    void update(int l, int r){
        l += n - 1;
        r += n - 1;
        swap(t[l].first, t[r].first);
        while(l > 0){
            l--; l /= 2;
            t[l] = min(t[2 * l + 1], t[2 * l + 2]); // 区間最小
        }
        while(r > 0){
            r--; r /= 2;
            t[r] = min(t[2 * r + 1], t[2 * r + 2]); // 区間最小
        }
    }
    pair<int, int> query(int a, int b, int now = 0, int l = 0, int r = -1) {
        if(r < 0) r = n;
        if(r <= a || b <= l) return make_pair(INF, -1); // minの単位元
        if(a <= l && r <= b) return t[now];
        return min(
            query(a, b, 2 * now + 1, l, (l + r) / 2),
            query(a, b, 2 * now + 2, (l + r) / 2, r)
        );
    }
};
// main
signed main() {
    int n, q, res = 0, a;
    cin>>n>>q;
    vector<pair<int, int>> v(n + 1);
    rep(i, n){
        cin>>a;
        v[i] = {a, i};
    }
    SegmentTree t(v);
    int l, r;
    rep(i, q){
        cin>>a>>l>>r;
        l--, r--;
        if(a==1){
            t.update(l, r);
        }else{
            cout<<t.query(l, r + 1).second + 1<<endl;
        }
    }
}
0