結果

問題 No.875 Range Mindex Query
ユーザー mencottonmencotton
提出日時 2019-09-07 15:54:03
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 284 ms / 2,000 ms
コード長 1,777 bytes
コンパイル時間 855 ms
コンパイル使用メモリ 72,756 KB
実行使用メモリ 5,420 KB
最終ジャッジ日時 2023-09-09 03:41:47
合計ジャッジ時間 3,677 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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,376 KB
testcase_04 AC 2 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,380 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 3 ms
4,376 KB
testcase_11 AC 198 ms
5,144 KB
testcase_12 AC 160 ms
4,380 KB
testcase_13 AC 137 ms
5,276 KB
testcase_14 AC 137 ms
5,104 KB
testcase_15 AC 187 ms
5,420 KB
testcase_16 AC 264 ms
5,104 KB
testcase_17 AC 284 ms
5,212 KB
testcase_18 AC 275 ms
5,144 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <climits>

using namespace std;

struct vindex {
    int value;
    int index;
};

class segtree {
public:
    int n;//2べき
    vector<vindex> data;

    void init() {
        data = vector<vindex>(n * 2 - 1);
        for (int i = 0; i < n * 2 - 1; i++)
            data[i] = {INT_MAX, -1};
    }

    void update(int i, int x) {
        i += n - 1;
        data[i] = {x, i - (n - 1)};
        while (i > 0) {
            i = (i - 1) / 2;
            vindex lchild = data[i * 2 + 1];
            vindex rchild = data[i * 2 + 2];
            data[i] = lchild.value < rchild.value ? lchild : rchild;
        }
    }

    vindex query(int a, int b, int k, int l, int r) {
        if (r <= a || b <= l)return {INT_MAX, -1};
        if (a <= l && r <= b)return data[k];
        else {
            vindex lchild = query(a, b, k * 2 + 1, l, (l + r) / 2);
            vindex rchild = query(a, b, k * 2 + 2, (l + r) / 2, r);
            return lchild.value < rchild.value ? lchild : rchild;
        }
    }
};

int main() {
    int n, q;
    cin >> n >> q;
    int bin_n = 1;
    while (bin_n < n)bin_n <<= 1;

    segtree segtree;
    segtree.n = bin_n;
    segtree.init();
    for (int i = 0; i < n; i++) {
        int a;
        cin >> a;
        segtree.update(i, a);
    }

    for (int i = 0; i < q; i++) {
        int ope, l, r;
        cin >> ope >> l >> r;
        l--;
        r--;
        if (ope == 1) {
            vindex tmpl = segtree.data[l + bin_n - 1];
            vindex tmpr = segtree.data[r + bin_n - 1];
            segtree.update(l, tmpr.value);
            segtree.update(r, tmpl.value);
        } else {
            cout << segtree.query(l, r + 1, 0, 0, bin_n).index + 1 << endl;
        }
    }
    return 0;
}
0