結果

問題 No.875 Range Mindex Query
ユーザー ldsybldsyb
提出日時 2019-09-07 16:14:49
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,857 bytes
コンパイル時間 1,648 ms
コンパイル使用メモリ 173,132 KB
実行使用メモリ 6,332 KB
最終ジャッジ日時 2023-09-09 04:08:59
合計ジャッジ時間 4,501 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

template <typename T>
struct monoid
{
	using F = function<T(T, T)>;

	T e;
	F f;

	monoid(T e, F f) : e(e), f(f)
	{
	}
};

template <typename T>
struct segment_tree : monoid<T>
{
	int n;
	vector<T> tree;

	segment_tree(int n_, T e, function<T(T, T)> f) : monoid<T>(e, f)
	{
		for (n = 1; n <= n_;)
		{
			n *= 2;
		}
		tree.assign(2 * n, monoid<T>::e);
	}

	segment_tree(int n_, vector<T> &init, T e, function<T(T, T)> f) : monoid<T>(e, f)
	{
		for (n = 1; n <= n_;)
		{
			n *= 2;
		}
		tree.assign(2 * n, monoid<T>::e);
		for (size_t i = 0; i < init.size(); i++)
		{
			update(i + 1, init[i]);
		}
	}

	void update(int index, T x)
	{
		index += n;
		tree[index] = x;
		for (index /= 2; 0 < index; index /= 2)
		{
			tree[index] = monoid<T>::f(tree[2 * index + 0], tree[2 * index + 1]);
		}
	}

	T query(int index, int il, int ir, int l, int r)
	{
		if (ir <= l || r <= il)
		{
			return monoid<T>::e;
		}

		if (l <= il && ir <= r)
		{
			return tree[index];
		}
		else
		{
			T left = query(2 * index + 0, il, (il + ir) / 2, l, r);
			T right = query(2 * index + 1, (il + ir) / 2, ir, l, r);
			return monoid<T>::f(left, right);
		}

		return monoid<T>::e;
	}

	T query(int l, int r)
	{
		return query(1, 0, n, l, r);
	}
};

int main()
{
	int n, q;
	cin >> n >> q;
	vector<int64_t> as(n);
	for (auto &&a : as)
	{
		cin >> a;
		a--;
	}

	vector<int> mp(n);
	for (int i = 0; i < n; i++)
	{
		mp[as[i]] = i;
	}

	segment_tree<int64_t> seg(n, as, (1LL << 60), [](int64_t l, int64_t r) { return min(l, r); });

	for (int _ = 0; _ < q; _++)
	{
		int t, l, r;
		cin >> t >> l >> r;
		l--;
		r--;

		if (t == 1)
		{
			seg.update(l, as[r]);
			seg.update(r, as[l]);
			swap(mp[as[l]], mp[as[r]]);
			swap(as[l], as[r]);
		}
		else if (t == 2)
		{
			cout << mp[seg.query(l, r + 1)] + 1 << endl;
		}
	}

	return 0;
}
0