結果

問題 No.875 Range Mindex Query
ユーザー yudedakoyudedako
提出日時 2019-09-06 23:18:30
言語 C++11
(gcc 11.4.0)
結果
RE  
実行時間 -
コード長 2,483 bytes
コンパイル時間 2,478 ms
コンパイル使用メモリ 112,740 KB
実行使用メモリ 4,976 KB
最終ジャッジ日時 2023-09-07 02:34:13
合計ジャッジ時間 6,497 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <vector>
#include <string>
#include <stack>
#include <queue>
#include <deque>
#include <map>
#include <set>
#include <tuple>
#include <bitset>
#include <memory>
#include <cmath>
#include <algorithm>
#include <functional>
#include <iomanip>
#include <numeric>
#include <climits>
#include <cfloat>




class MinSegmentTree {
	std::vector<std::vector<int>> vec;
	int inner_min(int depth, int from, int until) const {
		if (from == until) return INT_MAX;
		int length = 1 << depth;
		int mid = (from + length - 1) / length * length;
		if (from <= mid && mid + length <= until) {
			return std::min({ vec[depth][mid / length], inner_min(depth, from, mid), inner_min(depth, mid + length, until) });
		}
		else {
			return std::min(inner_min(depth - 1, from, mid), inner_min(depth - 1, mid, until));
		}
	}
public:
	MinSegmentTree(const std::vector<int>& initial) : vec{ initial } {
		while (vec.back().size() != 1) {
			vec.emplace_back((vec.back().size() + 1) / 2);
		}
		for (auto i = 1; i < vec.size(); ++i) {
			for (auto j = 0; j < vec[i].size(); ++j) {
				if (j * 2 + 1 < vec[i - 1].size()) {
					vec[i][j] = std::min(vec[i - 1][j * 2 + 1], vec[i - 1][j * 2]);
				}
				else {
					vec[i][j] = vec[i - 1][j * 2];
				}
			}
		}
	}
	int min_of(int from, int until) const {
		return inner_min(vec.size() - 1, from, until);
	}
	void update(int position, int value) {
		vec[0][position] = value;
		for (auto i = 1; i < vec.size(); ++i) {
			position >>= 1;
			if (position * 2 + 1 < vec[i - 1].size()) {
				vec[i][position] = std::min(vec[i - 1][position * 2], vec[i - 1][position * 2 + 1]);
			}
			else {
				vec[i][position] = vec[i - 1][position * 2];
			}
		}
	}
};
int main() {
	int n, q; std::cin >> n >> q;
	std::vector<int> initial(n); for (auto& a : initial) std::cin >> a;
	std::vector<int> indices(n); for (auto i = 0; i < n; ++i) indices[i] = i;
	std::sort(indices.begin(), indices.end(), [&initial](const int a, const int b) {return initial[a] < initial[b]; });
	MinSegmentTree seg(initial);
	for (auto i = 0; i < q; ++i) {
		int t, l, r; std::cin >> t >> l >> r; --l; --r;
		if (t == 1) {
			seg.update(l, initial[r]);
			seg.update(r, initial[l]);
			auto temp = indices[initial[r] - 1];
			indices[initial[r] - 1] = indices[initial[l] - 1];
			indices[initial[l] - 1] = temp;
			temp = initial[r];
			initial[r] = initial[l];
			initial[l] = temp;
		}
		else {
			std::cout << indices[seg.min_of(l, r + 1) - 1] + 1 << std::endl;
		}
	}
}
0