結果

問題 No.875 Range Mindex Query
ユーザー mencottonmencotton
提出日時 2019-09-07 15:39:26
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
MLE  
実行時間 -
コード長 1,582 bytes
コンパイル時間 558 ms
コンパイル使用メモリ 71,276 KB
実行使用メモリ 814,628 KB
最終ジャッジ日時 2023-09-09 03:22:38
合計ジャッジ時間 3,773 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

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

using namespace std;

struct vindex {
    int value;
    int index;
};

int n=1;//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 wantl, int wantr, int k, int nowl, int nowr) {
    if (nowr < wantl || wantr <= nowl)return {INT_MAX, -1};
    if (wantl <= nowl && nowr <= wantr)return data[k];
    else {
        vindex lchild = query(wantl, wantr, k * 2 + 1, nowl, (nowl + nowr) / 2);
        vindex rchild = query(wantl, wantr, k * 2 + 2, (nowl + nowr) / 2, nowr);
        return lchild.value < rchild.value ? lchild : rchild;
    }
}

int main() {
    int arrsize, q;
    cin >> arrsize >> q;
    while (n < arrsize)n <<= 1;
    
    init();
    for (int i = 0; i < n; i++) {
        int a;
        cin >> a;
        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 = data[l];
            vindex tmpr = data[r];
            update(l, tmpr.value);
            update(r, tmpl.value);
        } else {
            cout << query(l, r + 1, 0, 0, n).index + 1 << endl;
        }
    }
    return 0;
}
0