結果

問題 No.875 Range Mindex Query
ユーザー face4face4
提出日時 2019-09-06 21:35:21
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 270 ms / 2,000 ms
コード長 1,548 bytes
コンパイル時間 723 ms
コンパイル使用メモリ 74,032 KB
実行使用メモリ 5,468 KB
最終ジャッジ日時 2023-09-06 22:53:11
合計ジャッジ時間 3,698 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 3 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 3 ms
4,376 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 3 ms
4,376 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 4 ms
4,376 KB
testcase_11 AC 186 ms
4,832 KB
testcase_12 AC 151 ms
4,380 KB
testcase_13 AC 131 ms
5,108 KB
testcase_14 AC 129 ms
5,104 KB
testcase_15 AC 176 ms
5,108 KB
testcase_16 AC 251 ms
5,468 KB
testcase_17 AC 270 ms
5,100 KB
testcase_18 AC 258 ms
5,220 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<vector>
using namespace std;
const int INF = 1<<30;

struct STmin{
private:
    int n;
    vector<int> dat;
public:
    STmin(vector<int> ini){
        int siz = ini.size();
        n = 1;
        while(n < siz)   n *= 2;
        dat.resize(2*n-1, INF);
        for(int i = 0; i < siz; i++)    dat[n - 1 + i] = ini[i];
        for(int i = n-2; i >= 0; i--)   dat[i] = min(dat[2*i+1], dat[2*i+2]);
    }

    void update(int x, int val){
        x += (n-1);
        dat[x] = val;
        while(x > 0){
            x = (x-1)/2;
            dat[x] = min(dat[2*x+1], dat[2*x+2]);
        }
    }

    // focus on k-th node, who controls [l, r)
    int query(int a, int b, int k = 0, int l = 0, int r = -1){
        if(r < 0)   r = n;
        if(r <= a || b <= l)    return INF;
        if(a <= l && r <= b)    return dat[k];

        int lx = query(a, b, 2*k+1, l, (l+r)/2);
        int rx = query(a, b, 2*k+2, (l+r)/2, r);
        return min(lx, rx);
    }
};

int main(){
    int n, q;
    cin >> n >> q;
    vector<int> a(n), rev(n);
    for(int i = 0; i < n; i++)  cin >> a[i], rev[--a[i]] = i;
    STmin seg(a);
    while(q-- > 0){
        int op, l, r;
        cin >> op >> l >> r;
        l--, r--;
        if(op == 1){
            int al = a[l], ar = a[r];
            seg.update(l, ar);
            seg.update(r, al);
            rev[ar] = l; rev[al] = r;
            a[r] = al, a[l] = ar;
        }else{
            int mi = seg.query(l, r+1);
            cout << rev[mi]+1 << endl;
        }
    }
    return 0;
}
0