結果

問題 No.875 Range Mindex Query
ユーザー momoharamomohara
提出日時 2019-11-04 17:31:54
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 229 ms / 2,000 ms
コード長 2,814 bytes
コンパイル時間 1,837 ms
コンパイル使用メモリ 101,664 KB
実行使用メモリ 8,060 KB
最終ジャッジ日時 2023-10-13 02:00:29
合計ジャッジ時間 4,380 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,352 KB
testcase_01 AC 2 ms
4,352 KB
testcase_02 AC 3 ms
4,348 KB
testcase_03 AC 2 ms
4,352 KB
testcase_04 AC 2 ms
4,372 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 2 ms
4,352 KB
testcase_07 AC 2 ms
4,348 KB
testcase_08 AC 2 ms
4,348 KB
testcase_09 AC 3 ms
4,352 KB
testcase_10 AC 2 ms
4,352 KB
testcase_11 AC 151 ms
7,752 KB
testcase_12 AC 125 ms
5,440 KB
testcase_13 AC 105 ms
7,924 KB
testcase_14 AC 103 ms
7,672 KB
testcase_15 AC 144 ms
8,012 KB
testcase_16 AC 215 ms
7,940 KB
testcase_17 AC 229 ms
8,060 KB
testcase_18 AC 217 ms
7,928 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include<queue>
#include<vector>
#include<set>
#include<map>
#include<algorithm>
#include<cstring>
#include<string>
#include<cassert>
#include<cmath>
#include<climits>
#include<iomanip>
#include<bitset>
#include<unordered_map>

using namespace std;

#define MOD 1000000007
#define REP(i,n) for(ll (i)=0;(i)<(n);(i)++)
#define rep(i,j,n) for(ll (i)=(j);(i)<(n);(i)++)
#define FOR(i,c) for(decltype((c).begin())i=(c).begin();i!=(c).end();++i)
#define ll long long
#define ull unsigned long long
#define all(hoge) (hoge).begin(),(hoge).end()
typedef pair<ll, ll> P;
const long long INF = 1LL << 60;
typedef vector<ll> Array;
typedef vector<Array> Matrix;


template<class T> inline bool chmin(T& a, T b) {
	if (a > b) {
		a = b;
		return true;
	}
	return false;
}
template<class T> inline bool chmax(T& a, T b) {
	if (a < b) {
		a = b;
		return true;
	}
	return false;
}

//グラフ関連
struct Edge {//グラフ
	ll to, cap, rev;
	Edge(ll _to, ll _cap, ll _rev) {
		to = _to; cap = _cap; rev = _rev;
	}
};
typedef vector<Edge> Edges;
typedef vector<Edges> Graph;

void add_edge(Graph& G, ll from, ll to, ll cap, bool revFlag, ll revCap) {
	G[from].push_back(Edge(to, cap, (ll)G[to].size()));
	if (revFlag)G[to].push_back(Edge(from, revCap, (ll)G[from].size() - 1));
}

class RmqTree {
private:
	P _find(ll a, ll b, ll k, ll l, ll r) {
		//区間[l,r)の最小値を持つk番目のノードを探索
		if (r <= a || b <= l)return make_pair(INF,-1);    // 交差しない
		if (a <= l && r <= b)return dat[k];   // 完全に含む
		else {
			P s1 = _find(a, b, 2 * k + 1, l, (l + r) / 2); // 左の子
			P s2 = _find(a, b, 2 * k + 2, (l + r) / 2, r); // 右の子
			return min(s1, s2);
		}
	}

public:
	ll n, height;
	vector<P> dat;

	// 初期化(_nは最大要素数)
	RmqTree(ll _n) {
		n = 1;
		height = 1;
		while (n < _n) {
			n *= 2;
			height++;
		}
		dat = vector<P>(2 * n - 1, make_pair(INF,-1));
	}

	// i番目の値(0-indexed)をxに変更
	void update(ll i, ll x) {
		i += n - 1; // i番目の葉ノードへ
		dat[i] = make_pair(x,i-(n-1));
		while (i > 0) { // 登りながら更新
			i = (i - 1) / 2;//親ノードのインデックス
			dat[i] = min(dat[i * 2 + 1], dat[i * 2 + 2]);
			//子ノードの小さい方の値を代入
		}
	}

	// 区間[a,b)の最小値の取得
	P find(ll a, ll b) {
		return _find(a, b, 0, 0, n);
	}
};

int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	cout.tie(nullptr);

	ll n, q;
	cin >> n >> q;
	Array a(n, 0);
	RmqTree tree(n);
	
	REP(i, n) {
		cin >> a[i];
		tree.update(i, a[i]);
	}
	
	REP(i, q) {
		ll qq,l,r;
		
		cin >> qq>>l>>r;
		l--; r--;
		if (qq == 1) {
			swap(a[l],a[r]);
			tree.update(l, a[l]);
			tree.update(r, a[r]);
		}
		else {
			cout << tree.find(l, r + 1).second+1 << endl;
		}
	}


	return 0;
}
0