結果

問題 No.875 Range Mindex Query
ユーザー KarakasaDcFdKarakasaDcFd
提出日時 2019-09-06 22:45:53
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 207 ms / 2,000 ms
コード長 3,458 bytes
コンパイル時間 2,504 ms
コンパイル使用メモリ 208,596 KB
実行使用メモリ 7,328 KB
最終ジャッジ日時 2023-09-07 01:39:31
合計ジャッジ時間 4,885 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 3 ms
4,380 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,384 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,384 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 3 ms
4,380 KB
testcase_11 AC 159 ms
7,260 KB
testcase_12 AC 128 ms
5,076 KB
testcase_13 AC 110 ms
7,264 KB
testcase_14 AC 108 ms
7,320 KB
testcase_15 AC 150 ms
7,156 KB
testcase_16 AC 185 ms
7,328 KB
testcase_17 AC 207 ms
7,256 KB
testcase_18 AC 195 ms
7,232 KB
権限があれば一括ダウンロードができます

ソースコード

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) {
				//cout << l << endl;
				if (vl > dat[(l++) - 1]) {
					//cout << l << endl;
					vl = dat[l - 2];
					al = mid[l - 2];
				}
			}
			if (r & 1) {
				//cout << r << endl;
				if (vr > dat[(--r) - 1]) {
					//cout << r << endl;
					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