結果

問題 No.875 Range Mindex Query
ユーザー hipopohipopo
提出日時 2020-04-13 18:13:00
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,898 bytes
コンパイル時間 1,523 ms
コンパイル使用メモリ 142,368 KB
実行使用メモリ 4,704 KB
最終ジャッジ日時 2023-10-25 10:30:03
合計ジャッジ時間 3,775 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,356 KB
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 AC 169 ms
4,676 KB
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <cmath>
#include <complex>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>
#include <functional>
#include <cstring>
#include <regex>
#include <random>
#include <cassert>
#include <stack>
using namespace std;

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repr(i, s, n) for (int i = (s); i < (int)(n); i++)
#define revrep(i, n) for (int i = (n) - 1; i >= 0; i--)
#define revrepr(i, s, n) for (int i = (n) - 1; i >= s; i--)
#define debug(x) cerr << #x << ": " << x << "\n"
#define popcnt(x) __builtin_popcount(x)

using ll = long long;
using P = pair<int, int>;

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

template<class T>
istream& operator >>(istream &is, vector<T> &v) {
    for (int i = 0; i < (int)v.size(); i++) cin >> v.at(i);
    return is;
}

template<class T, class U>
ostream& operator <<(ostream &os, pair<T, U> p) {
    cout << '(' << p.first << ", " << p.second << ')';
    return os;
}

template<class T>
void print(const vector<T> &v, const string &delimiter) { rep(i, v.size()) cout << (0 < i ? delimiter : "") << v.at(i); cout << endl; }

template<class T>
void print(const vector<vector<T>> &vv, const string &delimiter) { for (const auto &v: vv) print(v, delimiter); }

template<class T>
struct SegmentTree {
    const int MAX_N = 1 << 17;
    int n;
    T init_v;
    vector<T> dat;

    SegmentTree(int _n, T _init_v) : init_v(_init_v) {
        n = 1;
        while (n < _n) n *= 2;
        dat.assign(2 * MAX_N - 1, init_v);
    }

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

    T sub_query(int a, int b, int k, int l, int r) const {
        if (r <= a || b <= l) return init_v;
        if (a <= l && r <= b) return dat[k];

        T vl = sub_query(a, b, k * 2 + 1, l, (l + r) / 2);
        T vr = sub_query(a, b, k * 2 + 2, (l + r) / 2, r);
        return min(vl, vr);
    }
    //[a, b)の最小値    
    T query(int a, int b) const { return sub_query(a, b, 0, 0, n); }
};

int main() {
    int n, q;
    cin >> n >> q;
    SegmentTree<int> st(n, n + 1);
    vector<int> idx(n + 1);
    rep(i, n) {
        int a;
        cin >> a;
        st.update(i, a);
        idx[a] = i;
    }
    
    auto change = [&](int i, int j) {
        int a = st.query(i, i + 1);
        int b = st.query(j, j + 1);
        swap(idx[a], idx[b]);
        st.update(i, b);
        st.update(j, a);
    };
    
    rep(i, n) {
        int k, l, r;
        cin >> k >> l >> r;
        l--; r--;
        if (k == 1) change(l, r);
        else cout << idx[st.query(l, r + 1)] + 1 << endl;
    }
}
0