結果
| 問題 | No.875 Range Mindex Query |
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2020-04-08 15:03:08 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.89.0) |
| 結果 |
AC
|
| 実行時間 | 266 ms / 2,000 ms |
| コード長 | 2,320 bytes |
| 記録 | |
| コンパイル時間 | 1,537 ms |
| コンパイル使用メモリ | 176,832 KB |
| 実行使用メモリ | 6,948 KB |
| 最終ジャッジ日時 | 2024-07-18 12:46:21 |
| 合計ジャッジ時間 | 4,647 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 1 |
| other | AC * 18 |
ソースコード
#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<n;i++)
using namespace std;
using ll = long long ;
using P = pair<int,int> ;
const int INF = 1e9;
const int MOD = 1000000007;
struct SegmentTree {
private:
int n;
vector<P> node;
public:
SegmentTree(vector<int> v){
int sz = v.size();
n = 1;
while(n < sz) n *= 2;
node.resize(2*n-1,P(INF,-1));
for(int i=0;i<sz;i++) node[i+n-1] = P(v[i],i);
for(int i=n-2;i>=0;i--){
if(node[2*i+1].first < node[2*i+2].first){
node[i] = node[2*i+1];
}else{
node[i] = node[2*i+2];
}
}
}
//x番目の要素をvalに変更する。
void update(int x,int val){
x += n-1;
node[x].first = val;
while(x > 0){
x = (x-1)/2;
if(node[2*x+1].first < node[2*x+2].first){
node[x] = node[2*x+1];
}else{
node[x] = node[2*x+2];
}
}
}
//区間[a,b)の最小値を求める
P getmin(int a,int b,int k=0,int l=0,int r=-1){
if(r < 0) r = n;
if(r <= a || b <= l) return P(INF,-1);
if(a <= l && r <= b) return node[k];
P vl = getmin(a,b,2*k+1,l,(l+r)/2);
P vr = getmin(a,b,2*k+2,(l+r)/2,r);
if(vl.first < vr.first) return vl;
else return vr;
}
};
int main(){
int n,q;
cin >> n >> q;
vector<int> a(n);
rep(i,n) cin >> a[i];
SegmentTree seg(a);
rep(i,q){
int c,l,r;
cin >> c >> l >> r;
--l;--r;
if(c==1){
swap(a[l],a[r]);
seg.update(l,a[l]);
seg.update(r,a[r]);
}else{
cout << seg.getmin(l,r+1).second+1 << endl;
}
/*rep(j,n+1){
rep(k,n+1) cout << seg.getmin(j,k).second + 1 << " ";
cout << endl;
}
rep(j,n) cout << a[j] << " ";
cout << endl;*/
}
return 0;
}