結果

問題 No.875 Range Mindex Query
ユーザー merom686merom686
提出日時 2019-09-06 21:38:55
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 62 ms / 2,000 ms
コード長 2,096 bytes
コンパイル時間 816 ms
コンパイル使用メモリ 78,360 KB
実行使用メモリ 4,952 KB
最終ジャッジ日時 2023-09-06 23:05:46
合計ジャッジ時間 2,551 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 59 ms
4,556 KB
testcase_12 AC 48 ms
4,376 KB
testcase_13 AC 43 ms
4,952 KB
testcase_14 AC 43 ms
4,576 KB
testcase_15 AC 56 ms
4,888 KB
testcase_16 AC 56 ms
4,908 KB
testcase_17 AC 62 ms
4,888 KB
testcase_18 AC 60 ms
4,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;
using ll = long long;

struct SegmentTree {
    struct Node {
        Node(pair<int, int> p) : v(p) {}
        Node operator*(const Node &o) const {
            return { min(v, o.v) };
        }
        pair<int, int> v;
    };

    SegmentTree(int n) : e({ 1 << 30, -1 }), n(n) {
        for (m = 2; m < n; m *= 2);
        t = vector<Node>(m + n + n % 2, e);
    }
    Node &operator[](int i) {
        return t[m + i];
    }
    const Node &root() const {
        return t[1];
    }
    void build() {
        for (int i = (n - 1 + m) / 2; i > 0; i--) t[i] = t[i * 2] * t[i * 2 + 1];
    }
    void update(int i, const Node &o) {
        for (t[i += m] = o; i /= 2, i > 0;) t[i] = t[i * 2] * t[i * 2 + 1];
    }
    Node query(int l, int r) {
        Node o[2] = { e, e };
        for (l += m, r += m; l < r; l /= 2, r /= 2) {
            if (r & 1) o[1] = t[--r] * o[1];
            if (l & 1) o[0] = o[0] * t[l++];
        }
        return o[0] * o[1];
    }
    void print() {
        for (int j = 1; j <= m; j *= 2) {
            int k = min(j * 2, m + n);
            for (int i = j; i < k; i++) {
                cout << t[i].v.first << " \n"[i == k - 1];
            }
        }
    }
    vector<Node> t;
    const Node e;
    int n, m;
};

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);

    int n, q;
    cin >> n >> q;

    SegmentTree st(n);
    for (int i = 0; i < n; i++) {
        int a;
        cin >> a;
        st[i] = SegmentTree::Node({ a, i });
    }
    st.build();

    for (int h = 0; h < q; h++) {
        int t, l, r;
        cin >> t >> l >> r;

        if (t == 1) {
            l--; r--;
            auto ln = st[l];
            auto rn = st[r];
            swap(ln.v.first, rn.v.first);
            st.update(l, ln);
            st.update(r, rn);

        } else {
            l--;
            auto qn = st.query(l, r);
            cout << qn.v.second + 1 << '\n';
        }
    }

    return 0;
}
0