結果

問題 No.875 Range Mindex Query
ユーザー rokahikou1rokahikou1
提出日時 2020-08-18 12:52:38
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 255 ms / 2,000 ms
コード長 2,741 bytes
コンパイル時間 965 ms
コンパイル使用メモリ 100,684 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-20 07:30:22
合計ジャッジ時間 4,395 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 2 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 188 ms
5,376 KB
testcase_12 AC 153 ms
5,376 KB
testcase_13 AC 128 ms
5,376 KB
testcase_14 AC 129 ms
5,376 KB
testcase_15 AC 178 ms
5,376 KB
testcase_16 AC 229 ms
5,376 KB
testcase_17 AC 255 ms
5,376 KB
testcase_18 AC 246 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define rep(i, n) for(int(i) = 0; (i) < (n); (i)++)
#define FOR(i, m, n) for(int(i) = (m); (i) < (n); (i)++)
#define All(v) (v).begin(), (v).end()
#define pb push_back
#define MP(a, b) make_pair((a), (b))
template <class T> vector<T> make_vec(size_t a) { return vector<T>(a); }
template <class T, class... Ts> auto make_vec(size_t a, Ts... ts) {
    return vector<decltype(make_vec<T>(ts...))>(a, make_vec<T>(ts...));
}
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
const int INF = 1 << 30;
const ll LINF = 1LL << 60;
const int MOD = 1e9 + 7;

/*RMAXQ by SegmentTree */
struct SegmentTree {
    int n;
    vector<int> dat;
    SegmentTree(int _n) {
        n = 1;
        while(n < _n)
            n *= 2;
        dat.resize(2 * n - 1);
        for(int i = 0; i < 2 * n - 1; i++)
            dat[i] = INF;
    }

    SegmentTree(vector<int> v) {
        int sz = v.size();
        n = 1;
        while(n < sz)
            n *= 2;
        dat.resize(2 * n - 1);
        for(int i = 0; i < sz; i++)
            dat[i + n - 1] = v[i];
        for(int i = n - 2; i >= 0; i--) {
            dat[i] = min(dat[2 * i + 1], dat[2 * i + 2]);
        }
    }

    void update(int k, int a) {
        k += n - 1;
        dat[k] = a;
        while(k > 0) {
            k = (k - 1) / 2;
            dat[k] = min(dat[k * 2 + 1], dat[k * 2 + 2]);
        }
    }

    int _query(int a, int b, int k, int l, int r) {
        if(r <= a || b <= l)
            return INF; //交差しない
        if(a <= l && r <= b)
            return dat[k]; //完全に含んでいる
        else {
            int vl = _query(a, b, k * 2 + 1, l, (l + r) / 2);
            int vr = _query(a, b, k * 2 + 2, (l + r) / 2, r);
            return min(vl, vr);
        }
    }
    int query(int i, int j) { return _query(i, j, 0, 0, n); }
};

int main() {
    int N, Q;
    cin >> N >> Q;
    vector<int> A(N);
    rep(i, N) cin >> A[i];
    rep(i, N) A[i]--;
    SegmentTree segtree(A);
    vector<int> indices(N);
    rep(i, N) { indices[A[i]] = i; }
    rep(i, Q) {
        int q, l, r;
        cin >> q >> l >> r;
        l--, r--;
        if(q == 1) {
            int tmp1 = segtree.query(l, l + 1), tmp2 = segtree.query(r, r + 1);
            segtree.update(l, tmp2);
            segtree.update(r, tmp1);
            indices[tmp1] = r;
            indices[tmp2] = l;
        } else {
            int mi = segtree.query(l, r + 1);
            cout << indices[mi] + 1 << endl;
        }
    }
    return 0;
}
0