結果
| 問題 |
No.875 Range Mindex Query
|
| コンテスト | |
| ユーザー |
tsutaj
|
| 提出日時 | 2019-09-06 22:35:52 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 278 ms / 2,000 ms |
| コード長 | 3,449 bytes |
| コンパイル時間 | 1,008 ms |
| コンパイル使用メモリ | 111,268 KB |
| 実行使用メモリ | 5,760 KB |
| 最終ジャッジ日時 | 2024-06-24 20:03:08 |
| 合計ジャッジ時間 | 3,751 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 1 |
| other | AC * 18 |
ソースコード
// #define _GLIBCXX_DEBUG // for STL debug (optional)
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <string>
#include <cstring>
#include <deque>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <utility>
#include <algorithm>
#include <map>
#include <set>
#include <complex>
#include <cmath>
#include <limits>
#include <cfloat>
#include <climits>
#include <ctime>
#include <cassert>
#include <numeric>
#include <fstream>
#include <functional>
#include <bitset>
using namespace std;
using ll = long long int;
using int64 = long long int;
template<typename T> void chmax(T &a, T b) {a = max(a, b);}
template<typename T> void chmin(T &a, T b) {a = min(a, b);}
template<typename T> void chadd(T &a, T b) {a = a + b;}
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
const int INF = 1 << 28;
const ll MOD = 1000000007LL;
// 抽象 SegmentTree (0-indexed・一点更新・区間取得)
template <typename MonoidType>
struct SegmentTree {
using Function = function< MonoidType(MonoidType, MonoidType) >;
// node, identity element
int n;
vector<MonoidType> node;
MonoidType E0;
// update / combine function
Function upd_f, cmb_f;
void build(int m, vector<MonoidType> v = vector<MonoidType>()) {
if(v != vector<MonoidType>()) m = v.size();
n = 1; while(n < m) n *= 2;
node = vector<MonoidType>(2*n-1, E0);
if(v != vector<MonoidType>()) {
for(int i=0; i<m; i++) {
node[n-1+i] = v[i];
}
for(int i=n-2; i>=0; i--) {
node[i] = cmb_f(node[2*i+1], node[2*i+2]);
}
}
}
// initialize
SegmentTree() {}
SegmentTree(int n_, MonoidType E0_,
Function upd_f_, Function cmb_f_,
vector<MonoidType> v = vector<MonoidType>()) :
E0(E0_), upd_f(upd_f_), cmb_f(cmb_f_) {
build(n_, v);
}
// update k-th element (applied value: x)
void update(int k, MonoidType x) {
k += n - 1;
node[k] = upd_f(node[k], x);
while(k > 0) {
k = (k - 1) / 2;
node[k] = cmb_f(node[2*k+1], node[2*k+2]);
}
}
// range query for [a, b)
// 非再帰のアイデア: http://d.hatena.ne.jp/komiyam/20131202/1385992406
MonoidType query(int a, int b) {
MonoidType vl = E0, vr = E0;
for(int l=a+n, r=b+n; l<r; l>>=1, r>>=1) {
if(l & 1) vl = cmb_f(vl, node[(l++)-1]);
if(r & 1) vr = cmb_f(node[(--r)-1], vr);
}
return cmb_f(vl, vr);
}
};
int main() {
int N, Q; cin >> N >> Q;
using Pair = pair<int, int>;
Pair I = make_pair(INF, INF);
SegmentTree<Pair> seg(N, I,
[](Pair a, Pair b) { return b; },
[](Pair a, Pair b) { return min(a, b); });
vector<int> A(N);
for(int i=0; i<N; i++) {
cin >> A[i];
seg.update(i, make_pair(A[i], i));
}
while(Q--) {
int q, l, r; cin >> q >> l >> r;
if(q == 1) {
l--; r--;
auto x = seg.node[l + seg.n - 1];
auto y = seg.node[r + seg.n - 1];
seg.update(l, make_pair(y.first, l));
seg.update(r, make_pair(x.first, r));
}
if(q == 2) {
l--;
auto res = seg.query(l, r);
cout << res.second + 1 << endl;
}
}
return 0;
}
tsutaj