結果
| 問題 |
No.875 Range Mindex Query
|
| コンテスト | |
| ユーザー |
merom686
|
| 提出日時 | 2019-09-06 21:38:55 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 68 ms / 2,000 ms |
| コード長 | 2,096 bytes |
| コンパイル時間 | 713 ms |
| コンパイル使用メモリ | 78,936 KB |
| 実行使用メモリ | 5,376 KB |
| 最終ジャッジ日時 | 2024-06-24 17:10:05 |
| 合計ジャッジ時間 | 2,252 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 1 |
| other | AC * 18 |
ソースコード
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;
using ll = long long;
struct SegmentTree {
struct Node {
Node(pair<int, int> p) : v(p) {}
Node operator*(const Node &o) const {
return { min(v, o.v) };
}
pair<int, int> v;
};
SegmentTree(int n) : e({ 1 << 30, -1 }), n(n) {
for (m = 2; m < n; m *= 2);
t = vector<Node>(m + n + n % 2, e);
}
Node &operator[](int i) {
return t[m + i];
}
const Node &root() const {
return t[1];
}
void build() {
for (int i = (n - 1 + m) / 2; i > 0; i--) t[i] = t[i * 2] * t[i * 2 + 1];
}
void update(int i, const Node &o) {
for (t[i += m] = o; i /= 2, i > 0;) t[i] = t[i * 2] * t[i * 2 + 1];
}
Node query(int l, int r) {
Node o[2] = { e, e };
for (l += m, r += m; l < r; l /= 2, r /= 2) {
if (r & 1) o[1] = t[--r] * o[1];
if (l & 1) o[0] = o[0] * t[l++];
}
return o[0] * o[1];
}
void print() {
for (int j = 1; j <= m; j *= 2) {
int k = min(j * 2, m + n);
for (int i = j; i < k; i++) {
cout << t[i].v.first << " \n"[i == k - 1];
}
}
}
vector<Node> t;
const Node e;
int n, m;
};
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, q;
cin >> n >> q;
SegmentTree st(n);
for (int i = 0; i < n; i++) {
int a;
cin >> a;
st[i] = SegmentTree::Node({ a, i });
}
st.build();
for (int h = 0; h < q; h++) {
int t, l, r;
cin >> t >> l >> r;
if (t == 1) {
l--; r--;
auto ln = st[l];
auto rn = st[r];
swap(ln.v.first, rn.v.first);
st.update(l, ln);
st.update(r, rn);
} else {
l--;
auto qn = st.query(l, r);
cout << qn.v.second + 1 << '\n';
}
}
return 0;
}
merom686