結果

問題 No.875 Range Mindex Query
ユーザー treeonetreeone
提出日時 2019-09-06 21:25:40
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 232 ms / 2,000 ms
コード長 1,980 bytes
コンパイル時間 2,183 ms
コンパイル使用メモリ 206,604 KB
実行使用メモリ 9,152 KB
最終ジャッジ日時 2023-09-06 22:12:55
合計ジャッジ時間 4,442 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 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,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 3 ms
4,380 KB
testcase_11 AC 151 ms
8,044 KB
testcase_12 AC 124 ms
5,660 KB
testcase_13 AC 108 ms
8,252 KB
testcase_14 AC 105 ms
8,580 KB
testcase_15 AC 147 ms
9,152 KB
testcase_16 AC 216 ms
8,216 KB
testcase_17 AC 232 ms
8,252 KB
testcase_18 AC 221 ms
9,112 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(i, a, n) for(int i = a; i < n; i++)
#define repr(i, a, b) for(int i = a; i >= b; i--)
#define int long long
#define all(a) a.begin(), a.end()
using namespace std;
typedef pair<int, int> P;
const int mod = 1000000007;
const int INF = 1e18;

struct Seg{
    int n;
    vector<P> dat;
    Seg(){}
    Seg(int _n){
        n = 1;
        while(n < _n) n *= 2;
        dat.clear();
        rep(i, 0, 2 * n){
            dat.push_back(P(INT_MAX, -1));
        }
    }
    void update(int k, int a){
        dat[k + n - 1].first = a;
        dat[k + n - 1].second = k;
        k += n - 1;        
        while(k > 0){
            k = (k - 1) / 2;
            int l = 2 * k + 1, r = 2 * k + 2;
            int MIN = INT_MAX, id = -1; 
            if(dat[l].first <= dat[r].first){
                MIN = dat[l].first; id = dat[l].second;
            }else{
                MIN = dat[r].first; id = dat[r].second;
            }
            dat[k].first = MIN; dat[k].second = id;
        }
    }
    // min[a, b)
    P query(int a, int b){
        return query(a, b, 0, 0, n);
    }
    P query(int a, int b, int k, int l, int r){
        if(r <= a || b <= l) return P(INT_MAX, -1);
        if(a <= l && r <= b) return dat[k];
        else{
            int mid = (l + r) / 2;
            P vl = query(a, b, 2 * k + 1, l, mid);
            P vr = query(a, b,  2 * k + 2, mid, r);
            return (vl.first <= vr.first ? vl : vr);
        }
    }
};

signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n, q;
    cin >> n >> q;
    vector<int> a(n);
    Seg seg(n);
    rep(i, 0, n){
        cin >> a[i];
        seg.update(i, a[i]);
    }
    while(q--){
        int c, l, r;
        cin >> c >> l >> r;
        l--; r--;
        if(c == 1){
            seg.update(l, a[r]);
            seg.update(r, a[l]);
            swap(a[l], a[r]);
        }else{
            cout << seg.query(l, r + 1).second + 1 << endl;
        }
    }
}
0