結果

問題 No.875 Range Mindex Query
ユーザー mencottonmencotton
提出日時 2019-09-07 16:25:27
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,745 bytes
コンパイル時間 577 ms
コンパイル使用メモリ 72,164 KB
実行使用メモリ 5,412 KB
最終ジャッジ日時 2023-09-09 04:20:34
合計ジャッジ時間 3,250 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
権限があれば一括ダウンロードができます

ソースコード

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);
        for (int i = 0; i < n * 2; i++)
            data[i] = {INT_MAX, -1};
    }

    void update(int i, int x) {
        i += n;
        data[i] = {x, i - n};
        while (i > 0) {
            i = i / 2;
            vindex lchild = data[i * 2];
            vindex rchild = data[i * 2 + 1];
            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, l, (l + r) / 2);
            vindex rchild = query(a, b, k * 2 + 1, (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