結果

問題 No.875 Range Mindex Query
ユーザー sbitesbite
提出日時 2019-09-06 21:50:26
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 314 ms / 2,000 ms
コード長 2,485 bytes
コンパイル時間 1,772 ms
コンパイル使用メモリ 158,748 KB
実行使用メモリ 9,132 KB
最終ジャッジ日時 2023-09-06 23:38:03
合計ジャッジ時間 5,292 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 3 ms
4,380 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 3 ms
4,376 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 3 ms
4,376 KB
testcase_10 AC 3 ms
4,376 KB
testcase_11 AC 287 ms
8,072 KB
testcase_12 AC 233 ms
6,736 KB
testcase_13 AC 226 ms
8,868 KB
testcase_14 AC 216 ms
8,524 KB
testcase_15 AC 289 ms
8,872 KB
testcase_16 AC 294 ms
8,888 KB
testcase_17 AC 314 ms
9,132 KB
testcase_18 AC 310 ms
9,048 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define _overload3(_1, _2, _3, name, ...) name
#define _rep(i, n) repi(i, 0, n)
#define repi(i, a, b) for (int i = (int)(a); i < (int)(b); ++i)
#define rep(...) _overload3(__VA_ARGS__, repi, _rep, )(__VA_ARGS__)
#define ll long long
#define lld long double
#ifdef DEBUG
#define line() cout << "[" << __LINE__ << "] ";
#define dump(i) cout << #i ": " << i << " ";
#define dumpl(i) cout << #i ": " << i << endl;
#else
#define line(i)
#define dump(i)
#define dumpl(i)
#endif
using namespace std;

template <typename Monoid>

struct SegmentTree
{
    using F = function<Monoid(Monoid, Monoid)>;
    int sz;
    vector<Monoid> seg;

    const F f;
    const Monoid M1;

    SegmentTree(int n, const F f, const Monoid &M1) : f(f), M1(M1)
    {
        sz = 1;
        while (sz < n)
            sz *= 2;
        seg.assign(2 * sz, M1);
    }
    void set(int k, const Monoid &x)
    {
        seg[k + sz] = x;
    }
    void build()
    {
        for (int k = sz - 1; k > 0; k++)
        {
            seg[k] = f(seg[2 * k + 0], seg[2 * k + 1]);
        }
    }
    void update(int k, const Monoid &x)
    {
        k += sz;
        seg[k] = x;
        while (k >>= 1)
        {
            seg[k] = f(seg[2 * k + 0], seg[2 * k + 1]);
        }
    }
    Monoid query(int a, int b)
    {
        Monoid L = M1, R = M1;
        for (a += sz, b += sz; a < b; a >>= 1, b >>= 1)
        {
            if (a & 1)
                L = f(L, seg[a++]);
            if (b & 1)
                R = f(seg[--b], R);
        }
        return f(L, R);
    }
    Monoid operator[](const int &k) const
    {
        return seg[k + sz];
    }
};

int f(int a, int b)
{
    return min(a, b);
}

int main()
{
    map<int, int> mp;
    int N, Q;
    cin >> N >> Q;
    SegmentTree<int> seg(N, f, INT_MAX);
    int a[N];
    rep(i, N)
    {
        cin >> a[i];
        a[i]--;
        seg.update(i, a[i]);
        mp[a[i]] = i;
    }
    rep(i, Q)
    {
        // rep(j, N) cerr << mp[j] << " ";
        //cerr << endl;
        //rep(j, N) cerr << a[j] << " ";
        //cerr << endl;

        int t, x, y;
        cin >> t >> x >> y;
        x--;
        y--;
        if (t == 1)
        {
            mp[a[x]] = y;
            mp[a[y]] = x;
            int tmp = a[x];
            seg.update(x, a[y]);
            seg.update(y, tmp);
            a[x] = a[y];
            a[y] = tmp;
        }
        else
            cout << mp[seg.query(x, y + 1)] + 1 << endl;
    }
    return 0;
}
0