結果

問題 No.875 Range Mindex Query
ユーザー OKCH3COOHOKCH3COOH
提出日時 2019-10-27 14:15:34
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 204 ms / 2,000 ms
コード長 3,525 bytes
コンパイル時間 2,346 ms
コンパイル使用メモリ 118,032 KB
実行使用メモリ 7,696 KB
最終ジャッジ日時 2023-10-12 22:51:15
合計ジャッジ時間 4,142 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 3 ms
4,352 KB
testcase_03 AC 2 ms
4,352 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 2 ms
4,352 KB
testcase_07 AC 2 ms
4,348 KB
testcase_08 AC 2 ms
4,352 KB
testcase_09 AC 2 ms
4,352 KB
testcase_10 AC 3 ms
4,352 KB
testcase_11 AC 129 ms
6,892 KB
testcase_12 AC 103 ms
5,624 KB
testcase_13 AC 88 ms
6,996 KB
testcase_14 AC 86 ms
6,844 KB
testcase_15 AC 119 ms
7,160 KB
testcase_16 AC 189 ms
7,688 KB
testcase_17 AC 204 ms
7,608 KB
testcase_18 AC 199 ms
7,696 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <map>
#include <set>
#include <vector>
#include <algorithm>
#include <iostream>
#include <bitset>
#include <cassert>
#include <queue>
#include <random>
#include <stack>
#include <iomanip>

using namespace std;
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; }

using Int = unsigned int;
using llong = long long;
using Llong = unsigned long long;
using ldouble = long double;
using intV = vector<int>;
using llongV = vector<long long>;
using llongVV = vector<vector<long long>>;
using Graph = vector<vector<long long>>;
using costGraph = vector<vector<pair<long long, long long>>>;

struct Edge {
    long long from;
    long long to;
    long long cost;
};

template<typename T>
using asc = less<T>();
template<typename T>
using desc = greater<T>();

const llong MOD = 1e9 + 7;
const llong INF = 1e17;

#define FOR(i, n) for (llong i = 0; i < n; i++)
#define FORS(i, a, b) for (llong i = a; i < b; i++)
#define FORR(i, n) for (llong i = n; i > 0; i++)
#define sup(vec, a) upper_bound(vec.begin(), vec.end(), a) - vec.begin()
#define inf(vec, a) lower_bound(vec.begin(), vec.end(), a) - vec.begin()

class SegmentTree {
    public :
        llongV __data;
        llong __size;

        SegmentTree (llong size) {
            this->init(size);
        };

        void init (llong size) {
            // 完全二分木にする
            llong binSize = 0;
            while((1 << binSize) < size) {
                binSize++;
            }
            this->__size = (1 << binSize);

            this->__data.resize(this->__size * 2);
            for(llong i = 0; i < this->__size * 2; i++) {
                this->__data[i] = INF;
            }
        };

        /* メソッド */
        void update(llong index, llong value) {
            index += this->__size - 1;
            this->__data[index] = value;
            while(index > 0) {
                index = (index - 1) / 2;
                this->__data[index] = min(this->__data[index * 2 + 1], this->__data[index * 2 + 2]);
            }
        };

        llong __query(llong qLeft, llong qRight, llong node, llong left, llong right) {
            if(right <= qLeft || qRight <= left) return INF;
            if(qLeft <= left && right <= qRight) return this->__data[node];

            llong leftVal = this->__query(qLeft, qRight, node * 2 + 1, left, (left + right) / 2);
            llong rightVal = this->__query(qLeft, qRight, node * 2 + 2, (left + right) / 2, right);
            return min(leftVal, rightVal);
        }

        // wrapper
        llong query(llong left, llong right) {
            return this->__query(left, right, 0, 0, this->__size);
        };
};

int main(void) {
    cin.tie(0);
    ios::sync_with_stdio(false);

    llong N, Q;
    cin >> N >> Q;

    SegmentTree tree(N);
    llongV A(N);
    llongV aToI(N + 1, -1);

    FOR(i, N) {
        cin >> A[i];
        aToI[A[i]] = i;
        tree.update(i, A[i]);
    }

    llongV ans;
    FOR(i, Q) {
        llong q, l, r;
        cin >> q >> l >> r;
        l--;
        r--;
        if(q == 1) {
            tree.update(l, A[r]);
            tree.update(r, A[l]);
            aToI[A[r]] = l;
            aToI[A[l]] = r;
            swap(A[l], A[r]);
        } else {
            ans.push_back(aToI[tree.query(l, r + 1)]);
        }
    }

    for(llong a : ans) {
        cout << (a + 1) << endl;
    }

    return 0;
}
0