結果

問題 No.875 Range Mindex Query
ユーザー taklimonetaklimone
提出日時 2019-11-21 16:49:31
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 262 ms / 2,000 ms
コード長 1,979 bytes
コンパイル時間 2,036 ms
コンパイル使用メモリ 201,100 KB
実行使用メモリ 6,144 KB
最終ジャッジ日時 2024-04-18 12:15:01
合計ジャッジ時間 5,495 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 3 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 3 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 3 ms
5,376 KB
testcase_07 AC 3 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 AC 3 ms
5,376 KB
testcase_11 AC 192 ms
5,888 KB
testcase_12 AC 161 ms
5,376 KB
testcase_13 AC 135 ms
6,016 KB
testcase_14 AC 131 ms
6,144 KB
testcase_15 AC 185 ms
6,016 KB
testcase_16 AC 241 ms
6,144 KB
testcase_17 AC 262 ms
6,144 KB
testcase_18 AC 254 ms
6,016 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

template <typename T, typename Op = plus<T>>
class segtree {
private:
    size_t N;
    vector<T> data;
    T idelem;
    const Op &op;

public:
    segtree(size_t n, T idelem, const Op &op = Op())
    : idelem(idelem), op(op) {
        for(N = 1; N < n; N <<= 1);
        data = vector<T>(2 * N, idelem);
    }

    segtree(const vector<T> &init, T idelem, const Op &op = Op())
    : idelem(idelem), op(op) {
        for(N = 1; N < init.size(); N <<= 1);
        data = vector<T>(2 * N, idelem);
        for(int i = 0; i < init.size(); ++i) data[i + N] = init[i];
        for(int i = N - 1; i >= 0; --i) data[i] = op(data[2 * i], data[2 * i + 1]);
    }

    void update(size_t pos, T val) {
        data[pos + N] = val;
        for(pos = (pos + N) / 2; pos > 0; pos >>= 1) {
            data[pos] = op(data[2 * pos], data[2 * pos + 1]);
        }
    }

    T query(size_t left, size_t right) {
        left += N;
        right += N;

        T vl = idelem, vr = idelem;
        while(left < right) {
            if(left & 1) vl = op(vl, data[left++]);
            if(right & 1) vr = op(data[right - 1], vr);
            left >>= 1;
            right >>= 1;
        }
        return op(vl, vr);
    }

    T operator[](size_t k) {
        return data[k + N];
    }
};


int main() {
    using P = pair<int, int>;

    int N, Q; cin >> N >> Q;
    vector<P> A(N);
    for(int i=0; i<N; ++i) {
        cin >> A[i].first;
        A[i].second = i;
    }
    
    auto op = [](P const& a, P const& b) {
        return a.first < b.first ? a : b;
    };
    segtree<P, decltype(op)> tree(A, P(100100, -1), op);

    while(Q--) {
        int k,l,r; cin >> k >> l >> r;
        if(k == 1) {
            auto s = tree[l - 1], t = tree[r - 1];
            tree.update(l - 1, P(t.first, l - 1));
            tree.update(r - 1, P(s.first, r - 1));
        } else {
            cout << tree.query(l - 1, r).second + 1 << endl;
        }
    }
}
0