結果

問題 No.875 Range Mindex Query
ユーザー sashimingsashiming
提出日時 2020-04-16 19:43:44
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 65 ms / 2,000 ms
コード長 2,837 bytes
コンパイル時間 2,014 ms
コンパイル使用メモリ 198,760 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-10 05:51:23
合計ジャッジ時間 4,357 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 2 ms
6,812 KB
testcase_02 AC 2 ms
6,816 KB
testcase_03 AC 2 ms
6,940 KB
testcase_04 AC 2 ms
6,944 KB
testcase_05 AC 2 ms
6,940 KB
testcase_06 AC 2 ms
6,940 KB
testcase_07 AC 2 ms
6,940 KB
testcase_08 AC 2 ms
6,940 KB
testcase_09 AC 2 ms
6,944 KB
testcase_10 AC 3 ms
6,944 KB
testcase_11 AC 63 ms
6,944 KB
testcase_12 AC 52 ms
6,944 KB
testcase_13 AC 47 ms
6,944 KB
testcase_14 AC 45 ms
6,944 KB
testcase_15 AC 61 ms
6,940 KB
testcase_16 AC 61 ms
6,944 KB
testcase_17 AC 65 ms
6,940 KB
testcase_18 AC 64 ms
6,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;

using lnt = long long;
using Real = long double;

#define ALL(obj) (obj).begin(),(obj).end()
#define RALL(obj) (obj).rbegin(),(obj).rend()
#define REP(i, n) for(lnt i=0;i<(n);++i)
#define RANGE(i, a, b) for(lnt i=(a);i<(b);++i)
#define RREP(i, n) for(lnt i=(n)-1;i>= 0;--i)

#define ln '\n'
#define pb push_back
#define eb emplace_back
#define pque priority_queue
#define umap unordered_map
#define uset unordered_set
#define dcout cout<<fixed<<setprecision(20)
#define summon_tourist cin.tie(0);ios::sync_with_stdio(false)

constexpr int dx[]={1,0,-1,0,1,1,-1,-1}, dy[]={0,-1,0,1,1,-1,1,-1};
constexpr Real eps = 1e-9; const Real pi = acosl(-1);
constexpr lnt BIG = INT_MAX/10, BBIG = LLONG_MAX/10;

template<class T> inline T GCD(T a,T b){T c;while(b!=0){c=a%b;a=b;b=c;}return a;}
template<class T> inline T LCM(T a,T b){T c=GCD(a,b);a/=c;return a*b;}
template<class T> inline T nCr(T a,T b){T i,r=1;for(i=1;i<=b;i++){r*=(a+1-i);r/=i;}return r;}
template<class T> inline T nHr(T a,T b){return nCr(a+b-1,b);}
template<class T> inline bool chmax(T& a,T b){if(a<b){a=b;return 1;}return 0;}
template<class T> inline bool chmin(T& a,T b){if(a>b){a=b;return 1;}return 0;}

using pint = pair<int, int>; using plnt = pair<lnt, lnt>;
using vint = vector<int>; using vlnt = vector<lnt>;
constexpr lnt MOD = 1e9+7;

template <typename T, typename F>
struct SegmentTree {
	int n; T *dat;
	T identity; F merge;
	SegmentTree(F f, T id) : merge(f), identity(id){}
	void init(int siz){
		n = 1; while(n < siz) n <<= 1;
		dat = new T[n*2-1];
		REP(i, n*2-1) dat[i] = identity;
	}
	void set(int i, T x){
		dat[i+n-1] = x;
	}
	void build(void){
		for(int i = n-2; i >= 0; i--){
			dat[i] = merge(dat[i*2 + 1], dat[i*2 + 2]);
		}
	}
	void update(int i, T x){
		i += n-1;
		dat[i] = x;
		while(i > 0){
			i = (i-1) / 2;
			dat[i] = merge(dat[i*2 + 1], dat[i*2 + 2]);
		}
	}
	T query(int l, int r){  // [l, r)(0-indexed), 処理だけ 1-indexed で考える
		l += n, r += n;
		T res = identity;
		while(l < r){
			if(r & 1){
				--r; res = merge(res, dat[r-1]);
			}
			if(l & 1){
				res = merge(res, dat[l-1]);
				++l;
			}
			l >>= 1; r >>= 1;
		}
		return res;
	}
};

int main(){
	summon_tourist;

	int N, Q; cin >> N >> Q;
	auto f = [](pint a, pint b){
		pint res;
		res.second = min(a.second, b.second);
		if(a.second < b.second) res.first = a.first;
		else res.first = b.first;
		return res;
	};
	SegmentTree<pint, decltype(f)> seg(f, {-1, BIG});
	seg.init(N);
	vint a(N);
	REP(i, N){
		cin >> a[i];
		seg.set(i, {i, a[i]});
	}
	seg.build();
	REP(i, Q){
		int com, l, r; cin >> com >> l >> r;
		l--; r--;
		if(com == 1){
			int p = a[l], q = a[r];
			swap(a[l], a[r]);
			seg.update(l, {l, q}); seg.update(r, {r, p});
		} else {
			pint ans = seg.query(l, r+1);
			cout << ans.first + 1 << ln;
		}
	}
}
0