結果

問題 No.875 Range Mindex Query
ユーザー 🍮かんプリン🍮かんプリン
提出日時 2019-09-06 21:46:56
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 283 ms / 2,000 ms
コード長 2,650 bytes
コンパイル時間 1,675 ms
コンパイル使用メモリ 174,332 KB
実行使用メモリ 5,696 KB
最終ジャッジ日時 2023-09-06 23:27:30
合計ジャッジ時間 4,684 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 3 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,380 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,380 KB
testcase_10 AC 3 ms
4,376 KB
testcase_11 AC 205 ms
5,420 KB
testcase_12 AC 167 ms
4,488 KB
testcase_13 AC 140 ms
5,364 KB
testcase_14 AC 138 ms
5,488 KB
testcase_15 AC 193 ms
5,500 KB
testcase_16 AC 261 ms
5,696 KB
testcase_17 AC 283 ms
5,544 KB
testcase_18 AC 274 ms
5,360 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include "bits/stdc++.h"
#define ALL(obj) (obj).begin(),(obj).end()
#define RALL(obj) (obj).rbegin(),(obj).rend()
#define REP(i, n) for(int i = 0; i < (int)(n); i++)
#define REPR(i, n) for(int i = (int)(n); i >= 0; i--)
#define FOR(i,n,m) for(int i = (int)(n); i < int(m); i++)
using namespace std;
typedef long long ll;
const int MOD = 1e9 + 7;
const int INF = MOD - 1;
const ll LLINF = 4e18;

template<class Monoid> struct SegmentTree {
private:
    using Func = function<Monoid(Monoid, Monoid)>;
    const Func F;
    const Monoid UNITY;
    int n;
    vector<pair<Monoid,int>> node;
public:
    //m=size, f=[](M a,M b){return ;}
    SegmentTree(int m, const Func f, const Monoid &unity) : F(f), UNITY(unity) {
        n = 1; while (n < m) n <<= 1;
        node.resize(n * 2 - 1, {UNITY,-1});
    }

    SegmentTree(const vector<Monoid>& v, const Func f, const Monoid &unity) : F(f), UNITY(unity) {
        int sz = v.size();
        n = 1; while (n < sz) n <<= 1;
        node.resize(n * 2 - 1, {UNITY,-1});
        REP(i, sz) node[i + n - 1] = {v[i],i};
        REPR(i, n - 2) {
            if (node[2 * i + 1].first < node[2 * i + 2].first) {
                node[i] = node[2 * i + 1];
            }
            else {
                node[i] = node[2 * i + 2];
            }
        }
    }

    void update(int x, int val) {
        if (x >= n || x < 0) return;
        x += n - 1;
        node[x].first = val;
        while (x > 0) {
            x = (x - 1) >> 1;
            if (node[2 * x + 1].first < node[2 * x + 2].first) {
                node[x] = node[2 * x + 1];
            }
            else {
                node[x] = node[2 * x + 2];
            }
        }
    }
    // [a,b)
    pair<Monoid, int> get(int a, int b, int k = 0, int l = 0, int r = -1) {
        if (r < 0) r = n;
        if (r <= a || b <= l) return {UNITY,-1};
        if (a <= l && r <= b) return node[k];
        pair<Monoid, int> vl = get(a, b, 2 * k + 1, l, (r - l) / 2 + l);
        pair<Monoid, int> vr = get(a, b, 2 * k + 2, (r - l) / 2 + l, r);
        if (vl.first < vr.first) {
            return vl;
        }
        else {
            return vr;
        }
    }
};

int main() {
    int n, q; cin >> n >> q;
    vector<int> a(n);
    REP(i, n) cin >> a[i];
    SegmentTree<int> seg(a, [](int a, int b) {return min(a, b); }, INF);
    REP(i, q) {
        int t, l, r; cin >> t >> l >> r;
        l--; r--;
        if (t == 1) {
            seg.update(l, a[r]);
            seg.update(r, a[l]);
            swap(a[l], a[r]);
        }
        else {
            cout << seg.get(l, r + 1).second+1 << endl;
        }
    }
    getchar(); getchar();
}
0