結果

問題 No.875 Range Mindex Query
ユーザー ganariyaganariya
提出日時 2019-09-07 19:43:13
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 327 ms / 2,000 ms
コード長 2,968 bytes
コンパイル時間 1,882 ms
コンパイル使用メモリ 156,492 KB
実行使用メモリ 9,092 KB
最終ジャッジ日時 2023-09-09 09:18:40
合計ジャッジ時間 5,125 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 3 ms
4,380 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 3 ms
4,380 KB
testcase_08 AC 3 ms
4,376 KB
testcase_09 AC 2 ms
4,384 KB
testcase_10 AC 3 ms
4,376 KB
testcase_11 AC 267 ms
8,016 KB
testcase_12 AC 209 ms
6,660 KB
testcase_13 AC 200 ms
8,900 KB
testcase_14 AC 196 ms
8,520 KB
testcase_15 AC 266 ms
9,052 KB
testcase_16 AC 303 ms
8,920 KB
testcase_17 AC 327 ms
9,092 KB
testcase_18 AC 312 ms
9,048 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

//include
//------------------------------------------
#include <vector>
#include <list>
#include <map>
#include <unordered_map>
#include <climits>
#include <set>
#include <unordered_set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <queue>
#include <random>
#include <complex>
#include <regex>
#include <locale>
#include <random>
#include <type_traits>

using namespace std;

#define SHOW_VECTOR(v) {std::cerr << #v << "\t:";for(const auto& xxx : v){std::cerr << xxx << " ";}std::cerr << "\n";}
#define SHOW_MAP(v){std::cerr << #v << endl; for(const auto& xxx: v){std::cerr << xxx.first << " " << xxx.second << "\n";}}

using LL = long long;

//------------------------------------------
//------------------------------------------

template<typename T>
struct SegmentTree {
private:
    using Func = function<T(T, T)>;

    int n;
    vector<T> node;

    T init_v;
    Func func;

public:

    SegmentTree(vector<T> a, Func _func, T _init_v) {
        int sz = a.size();
        n = 1;
        init_v = _init_v;
        func = _func;

        while (n < sz) n *= 2;
        node.resize(2 * n, init_v);
        for (int i = 0; i < sz; i++) node[i + n - 1] = a[i];
        for (int i = n - 2; i >= 0; i--) node[i] = func(node[i * 2 + 1], node[i * 2 + 2]);
    }

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

    T get(int pos) {
        int k = pos + n - 1;
        return node[k];
    }

    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 init_v;
        if (a <= l && r <= b)return node[k];
        T lv = query(a, b, k * 2 + 1, l, (l + r) / 2);
        T rv = query(a, b, k * 2 + 2, (l + r) / 2, r);
        return func(lv, rv);
    }

};


int main() {

    int N;
    cin >> N;

    int Q;
    cin >> Q;

    vector<int> a(N);
    for (int i = 0; i < N; i++) cin >> a[i];

    SegmentTree<int> tree(a, [](const int a, const int b) {
        return min(a, b);
    }, INT_MAX);

    map<int, int> mp;
    for (int i = 0; i < N; i++) mp[a[i]] = i;

    for (int i = 0; i < Q; i++) {
        int which, l, r;
        cin >> which >> l >> r;

        l--, r--;

        if (which == 1) {
            int lv = tree.get(l);
            int rv = tree.get(r);
            tree.update(r, lv);
            tree.update(l, rv);
            mp[lv] = r;
            mp[rv] = l;
        }
        if (which == 2) {
            cout << mp[tree.query(l, r + 1)] + 1 << endl;
        }
    }

}




























































0