結果

問題 No.875 Range Mindex Query
ユーザー trineutrontrineutron
提出日時 2020-01-03 15:26:11
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 296 ms / 2,000 ms
コード長 1,658 bytes
コンパイル時間 1,893 ms
コンパイル使用メモリ 176,668 KB
実行使用メモリ 6,348 KB
最終ジャッジ日時 2024-05-02 07:15:33
合計ジャッジ時間 4,748 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 3 ms
5,376 KB
testcase_02 AC 4 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 3 ms
5,376 KB
testcase_05 AC 3 ms
5,376 KB
testcase_06 AC 3 ms
5,376 KB
testcase_07 AC 3 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 3 ms
5,376 KB
testcase_10 AC 3 ms
5,376 KB
testcase_11 AC 213 ms
6,188 KB
testcase_12 AC 179 ms
5,376 KB
testcase_13 AC 151 ms
6,176 KB
testcase_14 AC 150 ms
6,268 KB
testcase_15 AC 208 ms
6,196 KB
testcase_16 AC 275 ms
6,064 KB
testcase_17 AC 296 ms
6,348 KB
testcase_18 AC 284 ms
6,232 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

const int inf = 1000000000;

class segtree {
    vector<pair<int, int>> t;
    int n;
    
    void update(int to, pair<int, int> from) {
        to += n - 1;
        t.at(to) = make_pair(from.first, to - n + 2);
        while (to) {
            to = (to - 1) / 2;
            t.at(to) = min(t.at(2 * to + 1), t.at(2 * to + 2));
        }
    }
    
    pair<int, int> query(int a, int b, int k, int l, int r) {
        if (r <= a || b <= l) return make_pair(inf, inf);
        if (a <= l && r <= b) return t.at(k);
        return min(query(a, b, 2 * k + 1, l, (l + r) / 2), query(a, b, 2 * k + 2, (l + r) / 2, r));
    }
    
    public:
    segtree(vector<int> a) {
        n = 1;
        while (n < a.size()) n *= 2;
        for (int i = 0; i < n - 1; i++) t.emplace_back(inf, inf);
        for (int i = 0; i < a.size(); i++) t.emplace_back(a.at(i), i + 1);
        for (int i = a.size(); i < n; i++) t.emplace_back(inf, inf);
        for (int i = n - 2; i >= 0; i--) t.at(i) = min(t.at(2 * i + 1), t.at(2 * i + 2));
    }
    
    void swap(int l, int r) {
        l--; r--;
        auto w = t.at(l + n - 1);
        update(l, t.at(r + n - 1));
        update(r, w);
    }
    
    int find(int l, int r) {
        return query(l - 1, r, 0, 0, n).second;
    }
};

int main() {
    int n, q;
    cin >> n >> q;
    vector<int> a(n);
    for (int i = 0; i < n; i++) cin >> a.at(i);
    segtree t(a);
    for (int i = 0; i < q; i++) {
        int k, l, r;
        cin >> k >> l >> r;
        if (k == 1) {
            t.swap(l, r);
        } else {
            cout << t.find(l, r) << endl;
        }
    }
}
0