結果

問題 No.875 Range Mindex Query
ユーザー tomatoma
提出日時 2019-09-06 21:42:15
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 302 ms / 2,000 ms
コード長 1,792 bytes
コンパイル時間 1,683 ms
コンパイル使用メモリ 177,540 KB
実行使用メモリ 9,160 KB
最終ジャッジ日時 2023-09-06 23:17:17
合計ジャッジ時間 4,907 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 3 ms
4,376 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 3 ms
4,376 KB
testcase_07 AC 3 ms
4,376 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 3 ms
4,376 KB
testcase_11 AC 265 ms
8,104 KB
testcase_12 AC 209 ms
6,532 KB
testcase_13 AC 207 ms
8,800 KB
testcase_14 AC 201 ms
8,584 KB
testcase_15 AC 266 ms
8,792 KB
testcase_16 AC 279 ms
8,852 KB
testcase_17 AC 302 ms
9,160 KB
testcase_18 AC 296 ms
9,104 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include"bits/stdc++.h"
using namespace std;
#define REP(k,m,n) for(int (k)=(m);(k)<(n);(k)++)
#define rep(i,n) REP((i),0,(n))


//http://beet-aizu.hatenablog.com/entry/2019/03/12/171221
template<typename T>
class SegmentTree {
private:
	using F = function<T(T, T)>; // モノイド型
	int n; // 横幅
	F f;   // モノイド
	T e;   // モノイド単位元
	vector<T> data;

public:
	// init忘れに注意
	SegmentTree() {}
	SegmentTree(F f, T e) :f(f), e(e) {}
	void init(int n_) {
		n = 1;
		while (n < n_)n <<= 1;
		data.assign(n << 1, e);
	}
	void build(const vector<T>& v) {
		int n_ = v.size();
		init(n_);
		rep(i, n_)data[n + i] = v[i];
		for (int i = n - 1; i >= 0; i--) {
			data[i] = f(data[(i << 1) | 0], data[(i << 1) | 1]);
		}
	}
	void set_val(int idx, T val) {
		idx += n;
		data[idx] = val;
		while (idx >>= 1) {
			data[idx] = f(data[(idx << 1) | 0], data[(idx << 1) | 1]);
		}
	}
	T query(int a, int b) {
		// [a,b)
		T vl = e, vr = e;
		for (int l = a + n, r = b + n; l < r; l >>= 1, r >>= 1) {
			if (l & 1)vl = f(vl, data[l++]); // unknown
			if (r & 1)vr = f(data[--r], vr); // unknown
		}
		return f(vl, vr);
	}
};

template<typename T>
using ST = SegmentTree<T>;


int main()
{
	int N, Q;
	cin >> N >> Q;
	vector<int> a(N);
	rep(i, N)cin >> a[i];

	constexpr int INF = 1 << 28;
	function<int(int, int)> f = [](int a, int b) {
		return min(a, b);
	};
	SegmentTree<int> st(f, INF);
	st.build(a);

	map<int, int> mp;
	rep(i, N)mp[a[i]] = i;

	rep(i, Q) {
		int com, l, r;
		cin >> com >> l >> r;
		l--;
		r--;
		if (com == 1) {
			int al = st.query(l, l + 1);
			int ar = st.query(r, r + 1);
			st.set_val(l, ar);
			st.set_val(r, al);
			mp[al] = r;
			mp[ar] = l;
		}
		else {
			int tgt = st.query(l, r + 1);
			cout << mp[tgt] + 1 << endl;
		}
	}

	return 0;
}
0