結果

問題 No.875 Range Mindex Query
ユーザー furafura
提出日時 2020-08-06 01:53:24
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 72 ms / 2,000 ms
コード長 1,686 bytes
コンパイル時間 2,298 ms
コンパイル使用メモリ 207,196 KB
実行使用メモリ 6,216 KB
最終ジャッジ日時 2023-10-19 01:33:57
合計ジャッジ時間 5,007 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 2 ms
4,348 KB
testcase_08 AC 3 ms
4,348 KB
testcase_09 AC 2 ms
4,348 KB
testcase_10 AC 3 ms
4,348 KB
testcase_11 AC 66 ms
5,952 KB
testcase_12 AC 54 ms
4,896 KB
testcase_13 AC 50 ms
6,216 KB
testcase_14 AC 48 ms
5,952 KB
testcase_15 AC 65 ms
6,216 KB
testcase_16 AC 66 ms
6,216 KB
testcase_17 AC 72 ms
6,216 KB
testcase_18 AC 69 ms
6,216 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

#define rep(i,n) for(int i=0;i<(n);i++)

using namespace std;

template<class M>
class segment_tree{
	int sz;
	vector<M> seg;
public:
	segment_tree(){}
	segment_tree(int n){ build(n); }
	segment_tree(const vector<M>& a){ build(a); }
	void build(int n){
		for(sz=1;sz<n;sz<<=1);
		seg.assign(2*sz,M());
	}
	void build(const vector<M>& a){
		int n=a.size();
		build(n);
		rep(i,n) seg[sz+i]=a[i];
		for(int i=sz-1;i>0;i--){
			seg[i]=seg[2*i]*seg[2*i+1];
		}
	}
	const M& operator[](int i)const{
		return seg[sz+i];
	}
	void update(int i,const M& x){
		i+=sz;
		seg[i]=x;
		for(i>>=1;i>0;i>>=1) seg[i]=seg[2*i]*seg[2*i+1];
	}
	M get(int l,int r)const{
		M lcum,rcum;
		for(int a=l+sz,b=r+sz;a<b;a>>=1,b>>=1){
			if(a&1) lcum=lcum*seg[a++];
			if(b&1) rcum=seg[--b]*rcum;
		}
		return lcum*rcum;
	}
};

template<class T> class min_monoid;
template<class T1,class T2>
class min_monoid<pair<T1,T2>>{
	pair<T1,T2> a;
public:
	min_monoid():a(numeric_limits<T1>::max(),numeric_limits<T2>::max()){}
	min_monoid(const pair<T1,T2>& val):a(val){}
	min_monoid operator*(const min_monoid& x)const{
		return min(a,x.a);
	}
	pair<T1,T2>& get(){ return a; }
	const pair<T1,T2>& get()const{ return a; }
};

int main(){
	int n,q; scanf("%d%d",&n,&q);

	vector<min_monoid<pair<int,int>>> a(n);
	rep(i,n){
		int b; scanf("%d",&b); b--;
		a[i]=make_pair(b,i);
	}
	segment_tree S(a);

	rep(_,q){
		int type,l,r; scanf("%d%d%d",&type,&l,&r);
		if(type==1){
			l--; r--;
			int tmpl=S[l].get().first;
			int tmpr=S[r].get().first;
			S.update(l,make_pair(tmpr,l));
			S.update(r,make_pair(tmpl,r));
		}
		else{
			l--;
			printf("%d\n",S.get(l,r).get().second+1);
		}
	}

	return 0;
}
0