結果

問題 No.875 Range Mindex Query
ユーザー uchiiiiuchiiii
提出日時 2020-07-25 10:30:24
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 226 ms / 2,000 ms
コード長 2,252 bytes
コンパイル時間 3,830 ms
コンパイル使用メモリ 228,936 KB
実行使用メモリ 5,568 KB
最終ジャッジ日時 2023-09-09 06:38:08
合計ジャッジ時間 6,516 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,384 KB
testcase_02 AC 3 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,384 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 2 ms
4,384 KB
testcase_09 AC 2 ms
4,384 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 146 ms
5,484 KB
testcase_12 AC 122 ms
4,508 KB
testcase_13 AC 97 ms
5,404 KB
testcase_14 AC 97 ms
5,568 KB
testcase_15 AC 138 ms
5,524 KB
testcase_16 AC 208 ms
5,484 KB
testcase_17 AC 226 ms
5,444 KB
testcase_18 AC 217 ms
5,480 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma region
#pragma GCC target("avx2")
#pragma GCC optimize("03")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
using namespace std; typedef long double ld; typedef long long ll;
typedef unsigned long long ull;
#define endl "\n"
#define FOR(i,a,b) for(int i=(a);i<=(b);i++)
#define rep(i,a,b) for(int i=(a);i<(b);i++)
#define PII pair<int, int>
#define PLL pair<ll, ll>
#define ALL(x) (x).begin(), (x).end()
constexpr int INF=1<<30; constexpr ll LINF=1LL<<60; constexpr ll mod=1e9+7; constexpr int NIL = -1;
template<class T>inline bool chmax(T &a, const T &b) { if (a<b) { a = b; return 1; } return 0; }
template<class T>inline bool chmin(T &a, const T &b) { if (b<a) { a = b; return 1; } return 0; }
#pragma endregion
//-------------------

struct SegmentTree {
private:
    int n;
    vector<PII> node;
public:
    SegmentTree(vector<int>& v) {
        int sz = v.size();
        n = 1;
        while(n < sz) n <<= 1;
        node.resize(2*n-1, {INF, INF});
        for(int i=0; i<sz; i++) node[i+n-1] = {v[i], i};
        for(int i=n-2;i>=0;i--) node[i] = node[2*i+1] <= node[2*i+2]? node[2*i+1]: node[2*i+2];
    }
    void update(int i, int val) {
        i += n-1;
        node[i].first = val;
        while(i>0) {
            i = (i-1)/2;
            node[i] = node[2*i+1] <= node[2*i+2]? node[2*i+1]: node[2*i+2]; 
        }
    }
    void swap(int i, int j) {
        int val_i = node[i+n-1].first;
        int val_j = node[j+n-1].first;
        update(i, val_j);
        update(j, val_i);
    }

    PII get(int a, int b, int k=0, int l=0, int r=-1) {
        if(r < 0) r = n;
        if(r <= a or b <= l) return {INF, INF};
        if(a <= l and r <= b) return node[k];
        auto vl = get(a,b, 2*k+1, l, (l+r)/2);
        auto vr = get(a,b, 2*k+2, (l+r)/2, r);
        return vl <= vr? vl: vr;
    }
};

int main(){
    ios::sync_with_stdio(false); //cout << fixed << setprecision(15);
    int n,Q; cin >> n >> Q;
    vector<int> v(n);
    rep(i,0,n) cin >> v[i];
    SegmentTree seg(v);
    while(Q--) {
        int num, l, r; cin >> num >> l >> r;
        l--, r--;
        if(num==1) {
            seg.swap(l,r);
        } else {
            cout << seg.get(l,++r).second+1 << endl;
        }
    }
    return 0;
}
0