結果

問題 No.875 Range Mindex Query
ユーザー first_vilfirst_vil
提出日時 2019-09-06 22:33:56
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 91 ms / 2,000 ms
コード長 3,694 bytes
コンパイル時間 1,746 ms
コンパイル使用メモリ 173,576 KB
実行使用メモリ 6,040 KB
最終ジャッジ日時 2023-09-07 01:21:36
合計ジャッジ時間 3,620 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 76 ms
5,572 KB
testcase_12 AC 61 ms
4,560 KB
testcase_13 AC 53 ms
5,944 KB
testcase_14 AC 52 ms
5,680 KB
testcase_15 AC 71 ms
5,868 KB
testcase_16 AC 82 ms
5,896 KB
testcase_17 AC 91 ms
5,900 KB
testcase_18 AC 85 ms
6,040 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using VI = vector<int>;
using VL = vector<ll>;
#define FOR(i,a,n) for(int (i)=(a);(i)<(n);(i)++)
#define eFOR(i,a,n) for(int (i)=(a);(i)<=(n);(i)++)
#define rFOR(i,a,n) for(int (i)=(n)-1;(i)>=(a);(i)--)
#define erFOR(i,a,n) for(int (i)=(n);(i)>=(a);(i)--)
#define SORT(i) sort((i).begin(),(i).end())
#define rSORT(i,a) sort((i).begin(),(i).end(),(a))
#define all(i) (i).begin(),(i).end()
constexpr ll INF = 1000000000;
constexpr ll LLINF = 1LL << 60;
constexpr ll mod = 1000000007;
constexpr ll MOD = 998244353;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; }return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; }return 0; }
inline void init() { cin.tie(nullptr); cout.tie(nullptr); cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(15); }

bool p(pair<int,int> a, pair<int, int> b) {
    return a.first <= b.first;
}

struct SegTree {
    int n;
    vector<pair<int, int>> node;

public:
    // 元配列 v をセグメント木で表現する
    SegTree(vector<int> v) {
        // 最下段のノード数は元配列のサイズ以上になる最小の 2 冪 -> これを n とおく
        // セグメント木全体で必要なノード数は 2n-1 個である
        int sz = v.size();
        n = 1; while (n < sz) n *= 2;
        node.resize(2 * n - 1, { INF,INF });

        // 最下段に値を入れたあとに、下の段から順番に値を入れる
        // 値を入れるには、自分の子の 2 値を参照すれば良い
        for (int i = 0; i < sz; i++) node[i + n - 1] = { v[i],i };
        for (int i = n - 2; i >= 0; i--) node[i] = min(node[2 * i + 1], node[2 * i + 2], p);
    }
    void update(int x, int val) {
        // 最下段のノードにアクセスする
        x += (n - 1);

        // 最下段のノードを更新したら、あとは親に上って更新していく
        node[x].first = val;
        while (x > 0) {
            x = (x - 1) / 2;
            node[x] = min(node[2 * x + 1], node[2 * x + 2], p);
        }
    }
    pair<int,int> getmin(int a, int b, int k = 0, int l = 0, int r = -1) {
        // 最初に呼び出されたときの対象区間は [0, n)
        if (r < 0) r = n;

        // 要求区間と対象区間が交わらない -> 適当に返す
        if (r <= a || b <= l) return { INF,INF };

        // 要求区間が対象区間を完全に被覆 -> 対象区間を答えの計算に使う
        if (a <= l && r <= b) return node[k];

        // 要求区間が対象区間の一部を被覆 -> 子について探索を行う
        // 左側の子を vl ・ 右側の子を vr としている
        // 新しい対象区間は、現在の対象区間を半分に割ったもの
        pair<int,int> vl = getmin(a, b, 2 * k + 1, l, (l + r) / 2);
        pair<int,int> vr = getmin(a, b, 2 * k + 2, (l + r) / 2, r);
        return min(vl, vr, p);
    }
    void Swap(int a, int b) {
        int x = a + n - 1, y = b + n - 1;
        swap(node[x].first, node[y].first);
        while (x > 0) {
            x = (x - 1) / 2;
            node[x] = min(node[2 * x + 1], node[2 * x + 2], p);
        }
        while (y > 0) {
            y = (y - 1) / 2;
            node[y] = min(node[2 * y + 1], node[2 * y + 2], p);
        }
    }
};

int main() {
    init();

    int n, q;
    cin >> n >> q;
    VI a(n);
    FOR(i, 0, n)cin >> a[i];
    SegTree seg(a);

    while (q--) {
        int o, l, r;
        cin >> o >> l >> r;
        if (o == 1)seg.Swap(--l, --r);
        else cout << seg.getmin(--l, r).second + 1 << "\n";
    }
}
0