結果

問題 No.875 Range Mindex Query
ユーザー kappybarkappybar
提出日時 2020-04-08 15:03:08
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 278 ms / 2,000 ms
コード長 2,320 bytes
コンパイル時間 1,711 ms
コンパイル使用メモリ 175,040 KB
実行使用メモリ 6,072 KB
最終ジャッジ日時 2023-09-25 16:11:19
合計ジャッジ時間 4,999 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 3 ms
4,380 KB
testcase_02 AC 3 ms
4,380 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 3 ms
4,380 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 3 ms
4,376 KB
testcase_11 AC 199 ms
5,976 KB
testcase_12 AC 162 ms
4,636 KB
testcase_13 AC 142 ms
5,676 KB
testcase_14 AC 137 ms
5,808 KB
testcase_15 AC 188 ms
5,656 KB
testcase_16 AC 259 ms
6,072 KB
testcase_17 AC 278 ms
6,008 KB
testcase_18 AC 269 ms
5,964 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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;
}
0