結果

問題 No.875 Range Mindex Query
ユーザー granddaifukugranddaifuku
提出日時 2020-07-19 19:32:08
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 223 ms / 2,000 ms
コード長 2,477 bytes
コンパイル時間 1,848 ms
コンパイル使用メモリ 174,844 KB
実行使用メモリ 5,204 KB
最終ジャッジ日時 2023-08-22 11:28:46
合計ジャッジ時間 5,237 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,384 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 3 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 3 ms
4,380 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 3 ms
4,384 KB
testcase_11 AC 147 ms
5,064 KB
testcase_12 AC 121 ms
4,376 KB
testcase_13 AC 100 ms
5,112 KB
testcase_14 AC 98 ms
5,068 KB
testcase_15 AC 137 ms
5,064 KB
testcase_16 AC 205 ms
5,148 KB
testcase_17 AC 223 ms
5,144 KB
testcase_18 AC 214 ms
5,204 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

#define rep(i, n) for(int i = 0; i < (int)n; ++i)
#define FOR(i, a, b) for(int i = a; i < (int)b; ++i)
#define rrep(i, n) for(int i = ((int)n - 1); i >= 0; --i)

using ll = long long;
using ld = long double;

const int Inf = 1e9;
const double EPS = 1e-9;
const int MOD = 1e9 + 7;

template <typename T>
class SegmentTree {
    public:
        int n;
        T e;
        vector<T> node;
        function<T(T, T)> operation;
        function<T(T, T)> process;

        SegmentTree() {}
        SegmentTree(int n_, T e_, function<T(T, T)> operation_, 
            function<T(T, T)> process_) : e(e_), operation(operation_), process(process_) {
            n = 1;
            while (n < n_) n <<= 1;
            node.assign(2 * n, e);
        }

        void build() {
            for (int i = n - 1; i > 0; --i) {
                node[i] = operation(node[i * 2 + 0], node[i * 2 + 1]);
            }
        }

        void set(int idx, T v) {
            node[idx + n] = process(node[idx + n], v);
        }

        void update(int idx, T v) {
            idx += n;
            node[idx] = process(node[idx], v);
            while (idx >>= 1) {
                node[idx] = operation(node[idx * 2 + 0], node[idx * 2 + 1]);
            }
        }

        T query(int a, int b) {
            return query(a, b + 1, 1, 0, n);
        }

        T query(int a, int b, int k, int l, int r) {
            if (a >= r || b <= l) return e;
            if (a <= l && b >= r) return node[k];
            T c = query(a, b, 2 * k + 0, l, (l + r) / 2);
            T d = query(a, b, 2 * k + 1, (l + r) / 2, r);

            return operation(c, d);
        }

        T operator[](int idx) {
            return node[idx + n];
        }
};

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(0);
	int n, q;
	cin >> n >> q;
	SegmentTree<pair<int, int> > seg = SegmentTree<pair<int, int> >(n, make_pair(Inf, -1), [](pair<int, int>  a, pair<int, int>  b) { return (a.first < b.first ? a : b); }, [](pair<int, int> a, pair<int, int> b) { return b; });
	rep(i, n) {
	  int a;
	  cin >> a;
	  seg.set(i, make_pair(a, i + 1));
	}
	seg.build();
	rep(i, q) {
	  int t, l, r;
	  cin >> t >> l >> r;
	  if (t == 1) {
		auto left = seg[l - 1], right = seg[r - 1];
		left.second = r, right.second = l;
		seg.update(l - 1, right);
		seg.update(r - 1, left);
	  } else {
		cout << seg.query(l - 1, r - 1).second << endl;
	  }
	}
    
    return 0;
}

0