結果

問題 No.875 Range Mindex Query
ユーザー veqccveqcc
提出日時 2019-09-06 21:35:14
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 74 ms / 2,000 ms
コード長 2,076 bytes
コンパイル時間 1,058 ms
コンパイル使用メモリ 110,000 KB
実行使用メモリ 6,012 KB
最終ジャッジ日時 2023-09-06 22:53:01
合計ジャッジ時間 2,813 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 2 ms
4,384 KB
testcase_11 AC 72 ms
5,740 KB
testcase_12 AC 59 ms
4,612 KB
testcase_13 AC 52 ms
6,012 KB
testcase_14 AC 50 ms
5,740 KB
testcase_15 AC 70 ms
5,892 KB
testcase_16 AC 68 ms
5,992 KB
testcase_17 AC 74 ms
5,984 KB
testcase_18 AC 71 ms
5,992 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <functional>
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <cstring>
#include <string>
#include <vector>
#include <random>
#include <bitset>
#include <queue>
#include <cmath>
#include <stack>
#include <set>
#include <map>
typedef long long ll;
using namespace std;
const ll MOD = 1000000007LL;
typedef pair <int, int> P;

template <typename T>
class SegmentTree {
    using F = function<T(T, T)>;
    int n;
    F f;
    T t;
    vector <T> dat;

public:
    SegmentTree() {};
    SegmentTree(F f, T t) : f(f), t(t) {}

    void init(int n_) {
        n = 1;
        while (n < n_) n <<= 1;
        dat.assign(n << 1, t);
    }

    void build(const vector <T> &v) {
        auto n_ = (int)v.size();
        init(n_);
        for (int i = 0; i < n_; i++) dat[n + i] = v[i];
        for (int i = n - 1; i > 0; i--) dat[i] = f(dat[(i << 1) | 0], dat[(i << 1) | 1]);
    }

    void set_val(int k, T x) {
        dat[k += n] = x;
        while (k >>= 1) dat[k] = f(dat[(k << 1) | 0], dat[(k << 1) | 1]);
    }

    T query(int a, int b) {
        T vl = t, vr = t;
        for (int l = a + n, r = b + n; l < r; l >>= 1, r >>= 1) {
            if (l & 1) vl = f(vl, dat[l++]);
            if (r & 1) vr = f(dat[--r], vr);
        }
        return f(vl, vr);
    }
};

int main() {
    cin.sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    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 + 1;
    }

    auto f = [](P a, P b) {
        return a.first < b.first ? a : b;
    };

    SegmentTree<P> seg(f, P(n + 1, n + 1));
    seg.build(a);

    while (q--) {
        int c, l, r;
        cin >> c >> l >> r;

        if (c == 1) {
            P l_val = seg.query(l - 1, l);
            P r_val = seg.query(r - 1, r);
            seg.set_val(l - 1, P(r_val.first, l));
            seg.set_val(r - 1, P(l_val.first, r));
        } else {
            P ret = seg.query(l - 1, r);
            cout << ret.second << '\n';
        }
    }

    return 0;
}
0