結果

問題 No.875 Range Mindex Query
ユーザー suika_psuika_p
提出日時 2019-09-06 23:35:02
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 290 ms / 2,000 ms
コード長 2,912 bytes
コンパイル時間 2,570 ms
コンパイル使用メモリ 172,408 KB
実行使用メモリ 6,876 KB
最終ジャッジ日時 2023-09-07 02:45:42
合計ジャッジ時間 4,995 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 3 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 3 ms
4,380 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 3 ms
4,380 KB
testcase_10 AC 3 ms
4,380 KB
testcase_11 AC 212 ms
6,280 KB
testcase_12 AC 172 ms
4,968 KB
testcase_13 AC 147 ms
6,768 KB
testcase_14 AC 145 ms
6,404 KB
testcase_15 AC 195 ms
6,528 KB
testcase_16 AC 264 ms
6,544 KB
testcase_17 AC 290 ms
6,876 KB
testcase_18 AC 280 ms
6,876 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

#define REP(i, m, n) for (int i = (m); i < (int)(n); i++)
#define rep(i, n) REP(i, 0, n)

int const inf = INT_MAX;

struct SegmentTree {
private:
    int n;
    vector<pair<int, int>> node;
public:
    SegmentTree(vector<pair<int, int>> v) {
        int sz = v.size();
        n = 1;
        // 2の累乗の大きさにする
        while (n < sz) n *= 2;
        // 余りをinfで埋める
        //node.resize(2*n-1, inf);
        node.resize(2*n-1);
        rep(i, 2*n-1) {
            node[i].first = inf;
            node[i].second = -1;
        }
        // 葉を構築
        for (int i = 0; i < sz; i++) {
            node[i+n-1].first = v[i].first;
            node[i+n-1].second = v[i].second;
        }
        // 親を構築
        for (int i = n - 2; i >= 0; i--) {
            if (node[2*i+1].first < node[2*i+2].first) {
                node[i].first = node[2*i+1].first;
                node[i].second = node[2*i+1].second;
            } else {
                node[i].first = node[2*i+2].first;
                node[i].second = node[2*i+2].second;
            }
            //node[i].first = min(node[2*i+1].first, node[2*i+2].first);
        }
    }

    void update(int x, int val, int ind) {
        x += (n - 1);
        node[x].first = val;
        node[x].second = ind;
        while (x > 0) {
            x = (x - 1) / 2;
            if (node[2*x+1].first < node[2*x+2].first) {
                node[x].first = node[2*x+1].first;
                node[x].second = node[2*x+1].second;
            } else {
                node[x].first = node[2*x+2].first;
                node[x].second = node[2*x+2].second;
            }
            //node[x].first = min(node[2*x+1].first, node[2*x+2].first);
        }
    }

    pair<int, int> getmin(int a, int b, int k = 0, int l = 0, int r = -1) {
        if (r < 0) r = n;
        if (r <= a || b <= l) {
            pair<int, int> c = make_pair(inf, -1);
            return c;
        }
        if (a <= l && r <= b) return node[k];
        pair<int, int> vl = getmin(a, b, 2*k+1, l, (l+r) / 2);
        pair<int, int> vr = getmin(a, b, 2*k+2, (l+r) / 2, r);
        if (vl.first > vr.first) {
            return vr;
        } else {
            return vl;
        }
    }
};

int main() {
    int N, Q; cin >> N >> Q;
    vector<pair<int, int>> a(N);
    rep(i, N) {
        int x; cin >> x;
        a[i] = make_pair(x, i+1);
    }
    SegmentTree seg(a);
    rep(_, Q) {
        int k, l, r; cin >> k >> l >> r;
        if (k == 1) {
            seg.update(l-1, a[r-1].first, l);
            seg.update(r-1, a[l-1].first, r);
            pair<int, int> x = a[r-1];
            pair<int, int> y = a[l-1];
            a[r-1] = y;
            a[l-1] = x;
        } else {
            pair<int, int> z = seg.getmin(l-1, r);
            cout << z.second << endl;
        }
    }
    return 0;
}
0