結果

問題 No.875 Range Mindex Query
ユーザー Konton7Konton7
提出日時 2020-02-08 23:38:34
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 312 ms / 2,000 ms
コード長 2,426 bytes
コンパイル時間 2,608 ms
コンパイル使用メモリ 211,244 KB
実行使用メモリ 6,676 KB
最終ジャッジ日時 2024-04-08 11:40:15
合計ジャッジ時間 7,237 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,676 KB
testcase_01 AC 3 ms
6,676 KB
testcase_02 AC 3 ms
6,676 KB
testcase_03 AC 2 ms
6,676 KB
testcase_04 AC 3 ms
6,676 KB
testcase_05 AC 2 ms
6,676 KB
testcase_06 AC 3 ms
6,676 KB
testcase_07 AC 3 ms
6,676 KB
testcase_08 AC 3 ms
6,676 KB
testcase_09 AC 3 ms
6,676 KB
testcase_10 AC 4 ms
6,676 KB
testcase_11 AC 259 ms
6,676 KB
testcase_12 AC 209 ms
6,676 KB
testcase_13 AC 195 ms
6,676 KB
testcase_14 AC 176 ms
6,676 KB
testcase_15 AC 240 ms
6,676 KB
testcase_16 AC 286 ms
6,676 KB
testcase_17 AC 312 ms
6,676 KB
testcase_18 AC 304 ms
6,676 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using PII = std::pair<int, int>;
using PLL = std::pair<ll, ll>;

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)


class RangeMinorMaxorSumQuery // 0-index
{
    int const intmax = 2147483647;
    int const intmin = 0;
    vector<PII> sgt;
    int n;
    int k;

public:
    RangeMinorMaxorSumQuery(int n1, int f = -1)
    {
        if (f == -1)
            f = intmax;
        else if (f == 0)
            f = intmin;
        int na = 1;
        int ka = 0;
        while (na < n1)
        {
            na *= 2;
            ka++;
        }
        for (int i = 0; i < 2 * na; i++)
            sgt.push_back(make_pair(f, i));
        n = na;
        k = ka;
    }

    void update_min(int i, int x)
    {
        i += n;
        sgt[i] = make_pair(x, i - n + 1);
        while (i > 1)
        {
            i /= 2;
            sgt[i] = min(sgt[2 * i], sgt[2 * i + 1]);
        }
    }

    PII getmin(int a, int b, int k = 1, int l = 0, int r = -1) //閉区間 l <= x < r とする
    {
        if (r == -1)
            r = n;
        if (r <= a || b <= l)
            return make_pair(intmax, -1);
        if (a == l && b == r)
            return sgt[k];
        else
            return min(getmin(a, min(b, (l + r) / 2), 2 * k, l, (l + r) / 2), getmin(max(a, (l + r) / 2), b, 2 * k + 1, (l + r) / 2, r));
    }

    void printsegtree()
    {
        for (int i = 0; i < 2 * n; i++)
        {
            cout << "(" << sgt[i].first << " " << sgt[i].second << ")"
                 << " ";
        }
        cout << endl;
    }
};

int main()
{

#ifdef DEBUG
    cout << "DEBUG MODE" << endl;
    ifstream in("input.txt"); //for debug
    cin.rdbuf(in.rdbuf());    //for debug
#endif

    int n, q, w, op, l, r, ln, rn;
    cin >> n >> q;
    RangeMinorMaxorSumQuery rmq(n);
    rep(i, n)
    {
        cin >> w;
        rmq.update_min(i, w);
    }
    

    rep(i, q)
    {
        cin >> op >> l >> r;

        if (op == 1)
        {
            ln = rmq.getmin(l-1, l).first;
            rn = rmq.getmin(r-1, r).first;
            rmq.update_min(l-1, rn);
            rmq.update_min(r-1, ln);
            // rmq.update_min(l, w);
            // rmq.update_min(l, w);
        }
        else
        {
            cout << rmq.getmin(l-1, r).second << endl;
        }

    }

    return 0;
}
0