結果

問題 No.875 Range Mindex Query
ユーザー けーむけーむ
提出日時 2020-03-25 17:38:34
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 236 ms / 2,000 ms
コード長 2,449 bytes
コンパイル時間 1,881 ms
コンパイル使用メモリ 176,564 KB
実行使用メモリ 8,836 KB
最終ジャッジ日時 2023-08-30 11:45:30
合計ジャッジ時間 5,585 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 4 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 3 ms
4,380 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 197 ms
8,308 KB
testcase_12 AC 161 ms
6,132 KB
testcase_13 AC 136 ms
8,576 KB
testcase_14 AC 128 ms
8,472 KB
testcase_15 AC 180 ms
8,584 KB
testcase_16 AC 220 ms
8,576 KB
testcase_17 AC 236 ms
8,836 KB
testcase_18 AC 228 ms
8,816 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for(ll i = 0, i##_len = (n); i < i##_len; i++)
#define reps(i, s, n) for(ll i = (s), i##_len = (n); i < i##_len; i++)
#define rrep(i, n) for(ll i = (n) - 1; i >= 0; i--)
#define rreps(i, e, n) for(ll i = (n) - 1; i >= (e); i--)
#define all(x) (x).begin(), (x).end()
#define sz(x) ((ll)(x).size())
#define len(x) ((ll)(x).length())

template<class T>
struct SegmentTree {
private:
    int n;
    vector<T> node;
    T def;
    function<T(T,T)> lop;
    function<T(T,T)> out;
    
public:
    SegmentTree() {}
    SegmentTree(const vector<T> &v, T _def, function<T(T,T)> _lop, function<T(T,T)> _out) {
        int vl = (int)v.size();
        n = 1;
        while(n < vl) n *= 2;
        def = _def; lop = _lop; out = _out;
        node = vector<T>(2 * n - 1, def);
        for(int i = 0; i < vl; i++) node[i + n - 1] = v[i];
        for(int i = n - 2; i >= 0; i--) node[i] = out(node[i * 2 + 1], node[i * 2 + 2]);
    }
    
    void update(int idx, T val) {
        idx += (n - 1);
        node[idx] = lop(node[idx], val);
        while(idx > 0) {
            idx = (idx - 1) / 2;
            node[idx] = out(node[idx * 2 + 1], node[idx * 2 + 2]);
        }
    }
    
    T query(int a, int b, int k = 0, int l = 0, int r = -1) {
        if (r < 0) r = n;
        if ((r <= a) || (b <= l)) return def;
        if ((a <= l) && (r <= b)) return node[k];
        return out(query(a, b, 2 * k + 1, l, (l + r) / 2), query(a, b, 2 * k + 2, (l + r) / 2, r));
    }
};

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    // ifstream in("input.txt");
    // cin.rdbuf(in.rdbuf());
    ll n, q;
    cin >> n >> q;
    vector<pair<ll, ll>> a(n);
    rep(i, n) {
        cin >> a[i].first;
        a[i].second = i;
    }
    auto lop = [](pair<ll, ll> a, pair<ll, ll> b){ return b; };
    auto out = [](pair<ll, ll> a, pair<ll, ll> b){ return min(a, b); };
    SegmentTree<pair<ll, ll>> st(a, make_pair(LONG_LONG_MAX, -1), lop, out);
    rep(i, q) {
        ll com, l, r;
        cin >> com >> l >> r;
        l--; r--;
        if (com == 1) {
            pair<ll, ll> v1 = st.query(l, l + 1);
            pair<ll, ll> v2 = st.query(r, r + 1);
            swap(v1.first, v2.first);
            st.update(l, v1);
            st.update(r, v2);
        }
        else {
            cout << (st.query(l, r + 1).second + 1) << endl;
        }
    }
    return 0;
}
0