結果

問題 No.875 Range Mindex Query
ユーザー kura197kura197
提出日時 2019-11-04 18:12:12
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 338 ms / 2,000 ms
コード長 2,005 bytes
コンパイル時間 3,570 ms
コンパイル使用メモリ 155,668 KB
実行使用メモリ 9,572 KB
最終ジャッジ日時 2023-10-13 02:03:42
合計ジャッジ時間 7,573 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,352 KB
testcase_01 AC 3 ms
4,352 KB
testcase_02 AC 3 ms
4,356 KB
testcase_03 AC 2 ms
4,352 KB
testcase_04 AC 2 ms
4,372 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 3 ms
4,352 KB
testcase_07 AC 3 ms
4,352 KB
testcase_08 AC 2 ms
4,356 KB
testcase_09 AC 2 ms
4,356 KB
testcase_10 AC 3 ms
4,356 KB
testcase_11 AC 294 ms
8,540 KB
testcase_12 AC 228 ms
6,696 KB
testcase_13 AC 218 ms
9,156 KB
testcase_14 AC 215 ms
8,896 KB
testcase_15 AC 287 ms
9,152 KB
testcase_16 AC 310 ms
9,064 KB
testcase_17 AC 338 ms
9,292 KB
testcase_18 AC 324 ms
9,572 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
#define REP(i, n) for(int i=0; i<n; i++)
#define REPi(i, a, b) for(int i=int(a); i<int(b); i++)
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
const ll MOD = 1e9+7;
const ll INF = 1e5;

class SegmentTree{
    int n;
    vector<int> node;

    public:
    SegmentTree(vector<int> v){
        n = 1;
        while(n < v.size()) n *= 2;
        node.resize(2*n-1);

        for(int i = 0; i < v.size(); i++)
            node[i+n-1] = v[i];
        for(int i = n-2; i >= 0; i--)
            node[i] = min(node[2*i+1], node[2*i+2]);
    }

    void update(int x, int val){
        x += (n - 1);

        node[x] = val;
        while(x > 0){
            x = (x - 1) / 2;
            node[x] = min(node[2*x+1], node[2*x+2]);
        }
    }

    int getmin(int a, int b, int k=0, int l=0, int r=-1){
        if(r < 0) r = n;

        if(b <= l || r <= a)
            return INF;

        if(a <= l && r <= b)
            return node[k];

        int vl = getmin(a, b, 2*k+1, l, (l+r)/2);
        int vr = getmin(a, b, 2*k+2, (l+r)/2, r);
        return min(vl, vr);
    }
};

int main(){
    int N, Q;
    cin >> N >> Q;
    vector<int> A(N);
    map<int, int> table;
    REP(i,N){
        int a;
        cin >> a;
        A[i] = a;
        table[a] = i;
    }

    SegmentTree tree(A);
    REP(q, Q){
        int a, l, r;
        cin >> a >> l >> r;
        l--, r--;

        if(a == 1){
            tree.update(l, A[r]);
            tree.update(r, A[l]);
            swap(A[r], A[l]);
            int x = table[A[r]];
            int y = table[A[l]];
            table[A[r]] = y;
            table[A[l]] = x;
        }
        else if(a == 2){
            int ans;
            ans = tree.getmin(l, r+1);
            cout << table[ans]+1 << endl;
        }
    }
    return 0;
}
0