結果

問題 No.875 Range Mindex Query
ユーザー niboshi_wakainiboshi_wakai
提出日時 2020-03-12 14:02:41
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 295 ms / 2,000 ms
コード長 1,857 bytes
コンパイル時間 1,867 ms
コンパイル使用メモリ 176,080 KB
実行使用メモリ 6,912 KB
最終ジャッジ日時 2024-04-29 00:08:17
合計ジャッジ時間 6,237 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 3 ms
5,376 KB
testcase_02 AC 4 ms
5,376 KB
testcase_03 AC 3 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 3 ms
5,376 KB
testcase_07 AC 3 ms
5,376 KB
testcase_08 AC 3 ms
5,376 KB
testcase_09 AC 3 ms
5,376 KB
testcase_10 AC 3 ms
5,376 KB
testcase_11 AC 237 ms
6,656 KB
testcase_12 AC 194 ms
5,376 KB
testcase_13 AC 164 ms
6,656 KB
testcase_14 AC 161 ms
6,656 KB
testcase_15 AC 223 ms
6,784 KB
testcase_16 AC 272 ms
6,912 KB
testcase_17 AC 295 ms
6,784 KB
testcase_18 AC 284 ms
6,784 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
#define fs first
#define sc second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define ALL(a) a.begin(),a.end()
#define RALL(a) a.rbegin(),a.rend()
typedef long long LL;
typedef pair<int,int> P;
const LL mod=1000000007;
const LL LINF=1LL<<62;
const int INF=1<<30;

template<class T>
struct SegmentTree{
private:
	int n;
	vector<T> node;

public:
	SegmentTree(vector<T> v){
		int sz = v.size();
		n = 1;
		while(n < sz) n *= 2;
		node.resize(2*n-1, mp(INF,INF));
		for (int i = 0; i < sz; i++) node[i+n-1] = v[i]; //vの段階でvector<pair<int,int>> として引数で渡す
		for (int i = n-2; i >= 0; i--) node[i] = min(node[2*i+1],node[2*i+2]);
	}

	void update(int x, T val) { // T は class なので pair<int,int> かな
		x += n - 1;
		node[x] = val;
		while (x > 0) {
			x = (x - 1) / 2;
			node[x] = min(node[2*x+1],node[2*x+2]);
		}
	}
	T getmin (int a, int b, int k=0, int l=0, int r=-1) {
		if(r < 0) r = n;
		if(r <= a || b <= l) return mp(INF,INF);
		if(a <= l && r <= b) return node[k];
		T vl = getmin(a, b, 2 * k + 1, l, (l + r) / 2);
		T vr = getmin(a, b, 2 * k + 2, (l + r) / 2, r);
		return min(vl,vr);
	}
};

int main(){
	int n, q;
	cin >> n >> q;
	vector<P> v(n);
	for (int i = 0; i < n; i++) {
		cin >> v[i].fs;
		v[i].sc = i;
	}
	SegmentTree<P> seg(v);
	while (q--) {
		int query_type, l, r;
		cin >> query_type >> l >> r;
		if (query_type == 1) { // 値を交換する
			-- l, -- r; // 1index だからデクリメント
			auto p = seg.getmin(l, l+1);
			// l を一時的に p に保存. → l に r を渡す → r に p を渡す
			seg.update(l, mp(seg.getmin(r, r+1).fs, l));
			seg.update(r, mp(p.fs, r));
			//TODO この辺って(l, node[r+n-1])でも行けるのでは??
		}
		else {
			--l;
			cout << seg.getmin(l, r).sc + 1 << endl;
		}
	}

}
0