結果

問題 No.875 Range Mindex Query
ユーザー toyamatoyama
提出日時 2019-09-06 21:32:59
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 271 ms / 2,000 ms
コード長 2,469 bytes
コンパイル時間 1,028 ms
コンパイル使用メモリ 87,376 KB
実行使用メモリ 5,228 KB
最終ジャッジ日時 2023-09-06 22:43:51
合計ジャッジ時間 3,728 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
5,064 KB
testcase_01 AC 4 ms
5,212 KB
testcase_02 AC 5 ms
5,128 KB
testcase_03 AC 3 ms
5,048 KB
testcase_04 AC 3 ms
4,932 KB
testcase_05 AC 3 ms
4,924 KB
testcase_06 AC 3 ms
5,032 KB
testcase_07 AC 3 ms
4,964 KB
testcase_08 AC 3 ms
5,132 KB
testcase_09 AC 3 ms
5,068 KB
testcase_10 AC 5 ms
5,068 KB
testcase_11 AC 205 ms
4,924 KB
testcase_12 AC 171 ms
5,128 KB
testcase_13 AC 147 ms
5,228 KB
testcase_14 AC 143 ms
5,112 KB
testcase_15 AC 192 ms
5,048 KB
testcase_16 AC 249 ms
4,920 KB
testcase_17 AC 271 ms
5,024 KB
testcase_18 AC 263 ms
5,032 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <functional>
#include <algorithm>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <stack>
using namespace std;
template<class T, class Compare = less<T> >
using MaxHeap = priority_queue<T, vector<T>, Compare>;
template<class T, class Compare = greater<T> >
using MinHeap = priority_queue<T, vector<T>, Compare>;
using llong = long long;

//===
template<typename Monoid>
struct SegmentTree {
    using OP = function<Monoid(Monoid, Monoid)>;
    
    vector<Monoid> tree;
    int size;
    const OP f; // bin' operation
    const Monoid e; // neutral element
                                           
    SegmentTree(int nmemb, const Monoid &e, const OP &f):
        f(f),
        e(e)
    {
        size = 1;
        while (size < nmemb) {
            size *= 2;
        }

        tree.assign(2 * size - 1, e);
    }

    void update(int k, Monoid dat) {
        k += size - 1;
        tree[k] = dat;
        
        while(k > 0) {
            k = (k - 1) / 2;
            tree[k] = f(tree[2 * k + 1], tree[2 * k + 2]);
        }
    }

    // [l, r)
    Monoid query(int l, int r) {
        l += size - 1;
        r += size - 1;

        Monoid d = e;
        while (l < r) {
            if (l % 2 == 0) {
                d = f(d, tree[l]);
                l++;
            }
            if (r % 2 == 0) {
                r--;
                d = f(d, tree[r]);
            }

            l = (l - 1) / 2;
            r = (r - 1) / 2;
        }

        return d;
    }

    Monoid operator[] (const int k) { return query(k, k + 1); }
};
//===

struct P {
    int v;
    int idx;

    P(int v, int idx): v(v), idx(idx) {};

    bool operator < (const P &r) const { return this->v < r.v; };
};

int n, q;
int com, l, r;
SegmentTree<P> seg(100005, P(1 << 29, 0), [](auto l, auto r){ return min(l, r); });

int main() {
    cin >> n >> q;
    for (int i = 0; i < n; i++) {
        int v;
        cin >> v;
        seg.update(i, P(v, i + 1));
    }

    for (int i = 0; i < q; i++) {
        cin >> com >> l >> r;

        r--; l--;

        if (com == 1) {
            P a = seg[l];
            P b = seg[r];

            seg.update(l, P(b.v, a.idx));
            seg.update(r, P(a.v, b.idx));
        }
        else if (com == 2) {
            P a = seg.query(l, r + 1);

            cout << a.idx << endl;
        }
    }

    return 0;
}
0