結果

問題 No.875 Range Mindex Query
ユーザー KarakasaDcFdKarakasaDcFd
提出日時 2019-09-06 22:41:26
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 3,356 bytes
コンパイル時間 2,131 ms
コンパイル使用メモリ 207,264 KB
実行使用メモリ 7,516 KB
最終ジャッジ日時 2023-09-07 01:35:21
合計ジャッジ時間 4,597 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 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;
using ll = long long;
const double pi = acos(-1);
#define FOR(i,a,b) for (ll i=(a),__last_##i=(b);i<__last_##i;i++)
#define RFOR(i,a,b) for (ll i=(b)-1,__last_##i=(a);i>=__last_##i;i--)
#define REP(i,n) FOR(i,0,n)
#define RREP(i,n) RFOR(i,0,n)
#define __GET_MACRO3(_1, _2, _3, NAME, ...) NAME
#define rep(...) __GET_MACRO3(__VA_ARGS__, FOR, REP)(__VA_ARGS__)
#define rrep(...) __GET_MACRO3(__VA_ARGS__, RFOR, RREP)(__VA_ARGS__)
template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) {
	REP(i, v.size()) { if (i)os << " "; os << v[i]; }return os;
}
template<typename T> ostream& operator<<(ostream& os, const vector<vector<T>>& v) {
	REP(i, v.size()) { if (i)os << endl; os << v[i]; }return os;
}

const ll INF = LLONG_MAX;
ll MOD = 1000000007;
ll _MOD = 1000000009;
double EPS = 1e-10;

#define int long long

inline void my_io() {

	std::ios::sync_with_stdio(false);

	std::cin.tie(0);

	cout << fixed << setprecision(16);

	//cout << setprecision(10) << scientific << ans << endl;
}

template <typename T, typename E>
struct SegmentTree {
	typedef function<T(T, T)> F;
	typedef function<T(T, E)> G;
	int n;
	F f;
	G g;
	T d1;
	E d0;
	vector<T> dat;
	vector<int> mid;
	SegmentTree() {};
	SegmentTree(int n_, F f, G g, T d1,
		vector<T> v = vector<T>()) :
		f(f), g(g), d1(d1) {
		init(n_);
		if (n_ == (int)v.size()) build(n_, v);
	}
	void init(int n_) {
		n = 1;
		while (n < n_) n *= 2;
		dat.clear();
		dat.resize(2 * n - 1, d1);
		mid.clear();
		mid.resize(2 * n - 1, d1);
		for (int i = 0; i < n; i++) {
			mid[n + i - 1] = i;
		}
	}
	void build(int n_, vector<T> v) {
		for (int i = 0; i < n_; i++) dat[i + n - 1] = v[i];
		for (int i = n - 2; i >= 0; i--)
			dat[i] = f(dat[i * 2 + 1], dat[i * 2 + 2]);
	}
	void update(int k, E a) {
		k += n - 1;
		dat[k] = g(dat[k], a);
		while (k > 0) {
			k = (k - 1) / 2;
			if (dat[k * 2 + 1] <= dat[k * 2 + 2]) {
				mid[k] = mid[k * 2 + 1];
			}
			else {
				mid[k] = mid[k * 2 + 2];
			}
			dat[k] = f(dat[k * 2 + 1], dat[k * 2 + 2]);
		}
		//cout << dat << endl;
		//cout << mid << endl;
	}
	inline T query(int a, int b) {
		T vl = d1, vr = d1;
		for (int l = a + n, r = b + n; l<r; l >>= 1, r >>= 1) {
			if (l & 1) vl = f(vl, dat[(l++) - 1]);
			if (r & 1) vr = f(dat[(--r) - 1], vr);
		}
		return f(vl, vr);
	}
	inline T query2(int a, int b) {
		T vl = d1, vr = d1;
		int al = a, ar = b;
		for (int l = a + n, r = b + n; l < r; l >>= 1, r >>= 1) {
			if (l & 1) {
				if (vl > dat[(l++) - 1]) {
					vl = dat[l - 1];
					al = mid[l - 1];
				}
			}
			if (r & 1) {
				if (vr > dat[(--r) - 1]) {
					vr = dat[r - 1];
					ar = mid[r - 1];
				}
			}
			//cout << vl << " " << vr << endl;
		}
		if (vl <= vr) {
			return ++al;
		}
		else {
			return ++ar;
		}
	}

};

signed main() {
	cin.tie(0);
	ios::sync_with_stdio(0);
	int n, q;
	cin >> n >> q;
	SegmentTree<int, int> rmq(n,
		[](int a, int b) {return min(a, b); },
		[](int a, int b) {return b; },
		INT_MAX);
	for (int i = 0; i < n; i++) {
		int a;
		cin >> a;
		rmq.update(i, a);
	}
	for (int i = 0; i<q; i++) {
		int c, x, y;
		cin >> c >> x >> y;
		if (c - 1) {
			cout << rmq.query2(x - 1, y) << endl;
		}
		else {
			int a = rmq.query(x - 1, x);
			int b = rmq.query(y - 1, y);
			rmq.update(x - 1, b);
			rmq.update(y - 1, a);
			rmq.update(x - 1, b);
		}
	}
	return 0;
}
0