結果

問題 No.875 Range Mindex Query
ユーザー SugarDragon5SugarDragon5
提出日時 2019-09-06 22:07:17
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 253 ms / 2,000 ms
コード長 1,668 bytes
コンパイル時間 2,654 ms
コンパイル使用メモリ 170,784 KB
実行使用メモリ 5,288 KB
最終ジャッジ日時 2023-09-07 00:02:30
合計ジャッジ時間 5,177 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,384 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 3 ms
4,384 KB
testcase_03 AC 1 ms
4,384 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,384 KB
testcase_06 AC 3 ms
4,384 KB
testcase_07 AC 3 ms
4,384 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 3 ms
4,380 KB
testcase_11 AC 187 ms
5,288 KB
testcase_12 AC 152 ms
4,384 KB
testcase_13 AC 128 ms
5,100 KB
testcase_14 AC 126 ms
5,156 KB
testcase_15 AC 176 ms
5,284 KB
testcase_16 AC 231 ms
5,228 KB
testcase_17 AC 253 ms
5,208 KB
testcase_18 AC 245 ms
5,212 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
struct Tree{
    int n;
    vector<pair<int,int>> vec;
    Tree(int _n){
        n=1;
        while(n<_n){
            n*=2;
        }
        vec.assign(n*2+10,{INT_MAX,INT_MAX});
    }
    void set(int k,pair<int,int> x){
        vec[k+n]=x;
    }
    void build(){
        for(int i=n-1;i>0;i--){
            vec[i]=min(vec[i*2],vec[i*2+1]);
        }
    }
    void update(int k,pair<int,int> x){
        k+=n;
        vec[k]=x;
        k/=2;
        while(k){
            vec[k]=min(vec[k*2],vec[k*2+1]);
            k/=2;
        }
    }
    void swap(int b,int c){
        auto p=vec[b+n];
        auto q=vec[c+n];
        p.second=c+1;
        q.second=b+1;
        update(c,p);
        update(b,q);
    }
    int query(int l,int r){
        l+=n;
        r+=n;
        pair<int,int> ans={INT_MAX,INT_MAX};
        while(l<r){
            if(l%2){
                ans=min(ans,vec[l]);
                l++;
            }
            if(r%2){
                ans=min(ans,vec[r-1]);
                r--;
            }
            l/=2;
            r/=2;
        }
        return ans.second;
    }
    void debug(){
        for(int i=1;i<=n*2;i++){
            cerr<<"{"<<vec[i].first<<","<<vec[i].second<<"},";
        }cerr<<endl;
    }
};
int main(){
    int n,q;
    cin>>n>>q;
    Tree tr(n);
    for(int i=0;i<n;i++){
        int t;
        cin>>t;
        tr.set(i,{t,i+1});
    }
    tr.build();
    for(int i=0;i<q;i++){
        int a,b,c;
        cin>>a>>b>>c;
        b--;c--;
        if(a==1){
            tr.swap(b,c);
        }else{
            cout<<tr.query(b,c+1)<<endl;
        }
    }
    return 0;
}
0