結果

問題 No.875 Range Mindex Query
ユーザー lowrlowr
提出日時 2019-09-07 16:23:37
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 280 ms / 2,000 ms
コード長 2,446 bytes
コンパイル時間 2,166 ms
コンパイル使用メモリ 206,856 KB
実行使用メモリ 6,188 KB
最終ジャッジ日時 2023-09-09 04:18:40
合計ジャッジ時間 5,038 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,376 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,376 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 3 ms
4,376 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 3 ms
4,380 KB
testcase_11 AC 198 ms
6,020 KB
testcase_12 AC 164 ms
4,648 KB
testcase_13 AC 140 ms
6,188 KB
testcase_14 AC 136 ms
5,864 KB
testcase_15 AC 189 ms
6,184 KB
testcase_16 AC 258 ms
5,912 KB
testcase_17 AC 280 ms
6,024 KB
testcase_18 AC 269 ms
5,884 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using i64 = long long;

template <typename T, typename F>
class SegmentTree {
    int size;
    T id;
    T *dat;
    F f;

    T query_impl(const int a, const int b, const int k, const int l, const int r) const {
        if (r <= a || b <= l) return id;
        if (a <= l && r <= b) return dat[k];
        return f(query_impl(a, b, k * 2 + 1, l, (l + r) / 2),
                 query_impl(a, b, k * 2 + 2, (l + r) / 2, r));
    }

public:
    SegmentTree(const int n, const T &id, const F f) : id(id), f(f) {
        size = 2;
        while (size < n) size *= 2;
        dat = new T[2 * size - 1];
        for (int i = 0; i < 2 * size - 1; i++) dat[i] = id;
    }

    template <template <class> class Container>
    SegmentTree(const Container<T> &v, const T &id, const F f) : id(id), f(f) {
        size = 2;
        int n = v.size();
        while (size < n) size *= 2;
        dat = new T[2 * size - 1];
        for (int i = size - 1; i < size - 1 + n; i++) dat[i] = v[i - size + 1];
        for (int i = size - 1 + n; i < 2 * size - 1; i++) dat[i] = id;
        dig(0);
    }

    void dig(int v) {
        if (v >= size - 1) return;
        dig(2 * v + 1);
        dig(2 * v + 2);
        dat[v] = f(dat[2 * v + 1], dat[2 * v + 2]);
    }

    ~SegmentTree() {
        delete[] dat;
    }

    void update(int i, const T x) {
        i += size - 1;
        dat[i] = x;
        while (i > 0) {
            i = (i - 1) / 2;
            dat[i] = f(dat[i * 2 + 1], dat[i * 2 + 2]);
        }
    }

    T query(const int a, const int b) const {
        return query_impl(a, b, 0, 0, size);
    }

    T operator[](const int i) const {
        return dat[i + size - 1];
    }
};

int main() {
    int n, q;
    std::cin >> n >> q;
    std::vector<std::pair<int, int>> a;
    for (int i = 0; i < n; i++) {
        int in;
        std::cin >> in;
        a.emplace_back(in, i);
    }

    auto st = SegmentTree(a, std::make_pair(1000000, 0), [](const auto &lhs, const auto &rhs) {
        return std::min(lhs, rhs);
    });

    while (q--) {
        int t, l, r;
        std::cin >> t >> l >> r;
        if (t == 1) {
            l--; r--;
            std::swap(a[l].first, a[r].first);
            st.update(l, std::make_pair(a[l].first, l));
            st.update(r, std::make_pair(a[r].first, r));
        } else {
            std::cout << st.query(l - 1, r).second + 1 << std::endl;
        }
    }

    return 0;
}
0