結果

問題 No.875 Range Mindex Query
ユーザー okok
提出日時 2019-09-06 21:36:30
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 140 ms / 2,000 ms
コード長 2,094 bytes
コンパイル時間 1,025 ms
コンパイル使用メモリ 94,432 KB
実行使用メモリ 7,264 KB
最終ジャッジ日時 2023-09-06 22:58:23
合計ジャッジ時間 3,547 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,384 KB
testcase_02 AC 2 ms
4,384 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 2 ms
4,384 KB
testcase_05 AC 2 ms
4,384 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 140 ms
7,176 KB
testcase_12 AC 111 ms
5,160 KB
testcase_13 AC 102 ms
7,120 KB
testcase_14 AC 101 ms
7,200 KB
testcase_15 AC 137 ms
7,140 KB
testcase_16 AC 113 ms
7,192 KB
testcase_17 AC 122 ms
7,264 KB
testcase_18 AC 117 ms
7,152 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<string>
#include<iomanip>
#include<cmath>
#include<vector>
#include<algorithm>
#include <functional>
#include <limits>

using namespace std;

#define int long long
#define endl "\n"

const long long INF = (long long)1e18;
const long long MOD = 1'000'000'007; 

string yn(bool f){return f?"Yes":"No";}
string YN(bool f){return f?"YES":"NO";}

template<typename T>
class segment_tree{
	int n;
	T fval;
	function<T(T,T)> operation;
	vector<T> dat;
	
	T _query(int a, int b, int k, int l, int r){
		if(r <= a || b <= l){
			return fval;
		}
		
		if(a <= l && r <= b){
			return dat[k];
		}else {
			T vl = _query(a, b, k * 2 + 1, l, (l + r) / 2 );
			T vr = _query(a, b, k * 2 + 2, (l + r) / 2, r);
			return operation(vl, vr);
		}
	}
	
public:
	
	segment_tree(){
		
	}
	
	segment_tree(int n_, T val, function<T(T,T)> fun){
		init(n_, val, fun);
	}
	
	~segment_tree(){
		
	}
	
	void init(int n_, T val, function<T(T,T)> fun){
		fval = val;
		
		operation = fun;
		
		n = 1;
		
		while(n < n_) n *= 2;
		
		dat.resize(2 * n - 1);
	
		for(int i = 0; i < 2 * n - 1; i++) dat[i] = fval;
	}

	void update(int k, T a){
		k += n - 1;
		dat[k] = a;
		while(k > 0){
			k = (k - 1) / 2;
			dat[k] = operation(dat[k*2 + 1],dat[k*2 + 2]);
		}
	}
	
	T query(int a, int b){
		return _query(a, b, 0, 0, n);
	}
	
};

signed main(){
	cin.tie(0);
	ios::sync_with_stdio(false);
	cout<<fixed<<setprecision(10);
	
	int N, Q;
	segment_tree<pair<int,int>> seg;
	
	cin>>N>>Q;
	
	seg.init(N+2,make_pair(numeric_limits<int>::max(),0), [](pair<int,int> first, pair<int,int> second){ if(first.first > second.first) return second; else return first;});
	
	
	for(int i = 0, a; i < N; i++){
		
		cin>>a;
		
		seg.update(i+1,make_pair(a,i+1));
	}
	
	for(int i = 0; i < Q; i++){
		int c, l, r;
		
		cin>>c>>l>>r;
		
		if(c == 1){
			pair<int,int> a = seg.query(r,r+1);
			pair<int,int> b = seg.query(l,l+1);
			// cout<<a.first<<" "<<b.first<<endl;
			seg.update(r,make_pair(b.first, r));
			seg.update(l,make_pair(a.first, l));
			
		} else {
			cout<<seg.query(l,r+1).second<<endl;
		}
	}
	return 0;
}
0