結果

問題 No.875 Range Mindex Query
ユーザー noisy_noiminnoisy_noimin
提出日時 2019-09-06 22:36:20
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 220 ms / 2,000 ms
コード長 2,822 bytes
コンパイル時間 929 ms
コンパイル使用メモリ 98,112 KB
実行使用メモリ 8,772 KB
最終ジャッジ日時 2023-09-07 01:27:49
合計ジャッジ時間 3,560 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,424 KB
testcase_01 AC 3 ms
5,592 KB
testcase_02 AC 4 ms
5,436 KB
testcase_03 AC 2 ms
5,480 KB
testcase_04 AC 2 ms
5,524 KB
testcase_05 AC 3 ms
5,424 KB
testcase_06 AC 3 ms
5,448 KB
testcase_07 AC 3 ms
5,600 KB
testcase_08 AC 2 ms
5,432 KB
testcase_09 AC 3 ms
5,420 KB
testcase_10 AC 3 ms
5,456 KB
testcase_11 AC 166 ms
8,572 KB
testcase_12 AC 139 ms
6,216 KB
testcase_13 AC 118 ms
8,772 KB
testcase_14 AC 114 ms
8,048 KB
testcase_15 AC 162 ms
7,612 KB
testcase_16 AC 202 ms
8,492 KB
testcase_17 AC 220 ms
7,936 KB
testcase_18 AC 211 ms
6,488 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cassert>
#include <cstdint>
#include <iostream>
#include <iomanip>
#include <string>
#include <stack>
#include <queue>
#include <vector>
#include <map>
#include <set>
#include <algorithm>
#include <numeric>
#include <bitset>

using namespace std;

using ll =  long long;
using Pll = pair<ll, ll>;
using Pii = pair<int, int>;

constexpr ll MOD = 1000000007;
constexpr long double EPS = 1e-10;
constexpr int dyx[4][2] = {
    { 0, 1}, {-1, 0}, {0,-1}, {1, 0}
};
const int MAX_N = 200000;

template<typename T> class RMQ {
    public:
    int n;
    T inf = INT_MAX;
    T data[4*MAX_N];
    T data_idx[4*MAX_N];

    RMQ(int m, T init_value=INT_MAX){
        // 2のべき乗にする
        n = 1;
        while(n < m) n <<= 1;
        inf = init_value;
        for(int i=0;i<2*n-1;++i){
            data[i] = init_value;
            data_idx[i] = i;
        }
    }

    void update(int i, T x){
        i += n-1;
        data[i] = x;
        data_idx[i] = i;
        while(i){
            i = (i-1)/2;
            if(data[2*i+1] < data[2*i+2]) {
                data[i] = data[2*i+1];
                data_idx[i] = data_idx[2*i+1];
            } else {
                data[i] = data[2*i+2];
                data_idx[i] = data_idx[2*i+2];
            }
        }
        // for(int i=0;i<2*n-1;++i) {
        //     printf("%4d", (data[i] == inf)?-1:data[i]);
        // }
        // printf("\n");
        // for(int i=0;i<2*n-1;++i) {
        //     printf("%4d", data_idx[i]);
        // }
        // printf("\n\n");
    }

    T find(int s, int t, int k, int kl, int kr){
        if(kr <= s || t <= kl) return 2*n-2;
        if(s <= kl && kr <= t) return data_idx[k];
        int kc = (kl+kr)/2;
        T vl = find(s, t, 2*k+1, kl, kc);
        T vr = find(s, t, 2*k+2, kc, kr);
        if(data[vl] < data[vr]) {
            // cerr << "find(" << kl << ", " << kr << ") = " << vl << endl;
            return vl;
        } else {
            // cerr << "find(" << kl << ", " << kr << ") = " << vr << endl;
            return vr;
        }
    }

    // [s, t) の最小値
    T find(int s, int t) {
        return find(s, t, 0, 0, n);
    }
};

int main() {
    std::ios::sync_with_stdio(false); cin.tie(0);
    int n, q, a, query, l, r;
    cin >> n >> q;
    RMQ<int> tree(n+3);
    for(int i=0;i<n;++i) {
        cin >> a;
        tree.update(i, a);
    }
    tree.update(n, INT_MAX);

    for(int i=0;i<q;++i) {
        cin >> query >> l >> r;
        --l; --r;
        if(query == 1) {
            int tmp = tree.data[tree.find(l, l+1)];
            tree.update(l, tree.data[tree.find(r, r+1)]);
            tree.update(r, tmp);
        } else {
            cout << tree.find(l, r+1) - tree.n + 1 + 1 << endl;
        }
    }
}
0