結果

問題 No.875 Range Mindex Query
ユーザー WarToksWarToks
提出日時 2019-09-28 01:15:47
言語 C++17(clang)
(17.0.6 + boost 1.83.0)
結果
AC  
実行時間 112 ms / 2,000 ms
コード長 2,920 bytes
コンパイル時間 1,253 ms
コンパイル使用メモリ 109,532 KB
実行使用メモリ 6,128 KB
最終ジャッジ日時 2023-08-20 12:59:28
合計ジャッジ時間 3,182 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,388 KB
testcase_02 AC 3 ms
4,376 KB
testcase_03 AC 2 ms
4,384 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 2 ms
4,504 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 91 ms
5,792 KB
testcase_12 AC 75 ms
4,752 KB
testcase_13 AC 65 ms
6,060 KB
testcase_14 AC 65 ms
5,820 KB
testcase_15 AC 88 ms
6,048 KB
testcase_16 AC 105 ms
6,008 KB
testcase_17 AC 112 ms
6,128 KB
testcase_18 AC 108 ms
6,056 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <array>
#include <bitset>
#include <cstring>
#include <map>
#include <stack>
#include <set>
#include <tuple>
#include <queue>
#include <vector>
#include <cmath>
#include <random>
#include <math.h>
#include <random>
#include <functional>



#define REP(i, n) for(int (i) = 0; (i) < (n); ++(i))
#define rREP(i, n) for(int (i) = (n) - 1; (i) >= 0; --(i))
#define ALL(TheArray) TheArray.begin(), TheArray.end()

using lli = long long int;
using pii = std::pair<int, int>;

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

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

template<typename T>
class SegTree{
    #define rep(i, n) for(int (i) = 0; (i) < (n); ++(i))
    const int N;
    const int n;

    const std::function<T(T, T)> F;
    const T identityValue;

    std::vector<T> theTree;

    int computeLength(int m){
        int res = 1; while(res < m) res <<= 1;
        return res;
    }

    T preGetValue(int a, int b, int k, int l, int r){
        if(r <= a or b <= l) return identityValue;

        if(a <= l and r <= b) return theTree[k];

        T vl = preGetValue(a, b, 2*k+1, l, (l+r)/2);
        T vr = preGetValue(a, b, 2*k+2, (l+r)/2, r);
        return F(vl, vr);
    }

public:
    SegTree(std::vector<T>& V, std::function<T(T, T)> f1, T idV) :n(V.size()), N(computeLength(V.size())), F(f1), identityValue(idV)
    {
        theTree.resize(2 * N - 1);
        int i;
        for(i = 0; i < n; ++i) theTree[i + N - 1] = V[i];
        for(i = n; i < N; ++i) theTree[i + N - 1] = identityValue;
        for(i = N - 2;i >= 0; --i) theTree[i] = F(theTree[2*i+1], theTree[2*i+2]);
    }

    bool update(int x, T val){
        if(x < 0 or x >= n) return false;
        int y = x + N - 1;
        theTree[y] = val;
        while(y > 0){
            (--y) >>= 1;
            theTree[y] =  F(theTree[2*y+1], theTree[2*y+2]);
        }
        return true;
    }
    T at(int idx){return theTree[idx + N - 1];}

    T getValue(int a, int b){
        return preGetValue(a, b, 0, 0, N);
    }

    #undef rep
};


constexpr int inf = 2e9;
constexpr pii infP = {inf, inf};
const std::function<pii(pii, pii)> minfunc = [](pii x, pii y){return x > y ? y : x;};
std::vector<pii> A;

int main(void){
    int n, Q; scanf("%d%d", &n, &Q);
    A.resize(n); REP(i, n) {scanf("%d", &A[i].first); A[i].second = i;}
    SegTree<pii> ST(A, minfunc, infP);
    while(Q--){
        int q, l, r; scanf("%d%d%d", &q, &l, &r); l--; r--;
        if(q == 1){
            pii vL = ST.at(l);
            pii vR = ST.at(r);
            ST.update(l, {vR.first, l});
            ST.update(r, {vL.first, r});
        }
        else{
            printf("%d\n", ST.getValue(l, r+1).second + 1);
        }
    }
    return 0;
}
0