結果

問題 No.875 Range Mindex Query
ユーザー llllltwwwwwllllltwwwww
提出日時 2019-09-19 22:48:53
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 279 ms / 2,000 ms
コード長 2,013 bytes
コンパイル時間 535 ms
コンパイル使用メモリ 73,596 KB
実行使用メモリ 5,976 KB
最終ジャッジ日時 2023-09-28 12:36:06
合計ジャッジ時間 3,452 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 3 ms
4,380 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 3 ms
4,376 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 3 ms
4,376 KB
testcase_11 AC 191 ms
5,696 KB
testcase_12 AC 153 ms
4,600 KB
testcase_13 AC 135 ms
5,976 KB
testcase_14 AC 134 ms
5,744 KB
testcase_15 AC 180 ms
5,780 KB
testcase_16 AC 252 ms
5,768 KB
testcase_17 AC 279 ms
5,772 KB
testcase_18 AC 273 ms
5,820 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<iostream>
#include<queue>
#include<vector>
#include <bitset>
#include<math.h>
#include<map>
#include<set>
#include <climits>
using namespace std;
#define INF 11000000000
#define MOD 1000000007
typedef long long ll;
typedef pair<int,int> P;

const int MAX_N=1<<17;

//セグメント木を持つグローバル配列
int n,C[110000];
ll dat[2*MAX_N-1];

//初期化
void init(int n_){
    //簡単のために要素数を2のべき乗に
    n=1;
    while(n<n_) n*=2;

    //全ての値をINT_MAXに
    for(int i=0;i<2*n-1;i++) dat[i]=2147483647;
}

//k番目の値(0-indexed)をaに変更
void update(int k,ll a){
    //葉の節点
    k+=n-1;
    dat[k]=a;
    //登りながら更新
    while(k>0){
        k=(k-1)/2;
        dat[k]=min(dat[k*2+1],dat[k*2+2]);
    }
}

//[a,b)の最小値を求める
//後ろの方の引数は、計算の簡単のための引数
//kは接点の番号、l,rはその接点が[l,r)に対応づいていることを表す
//したがって、外からはquery(a,b,0,0,n)として呼ぶ
ll query(int a,int b,int k,int l,int r){
    //cout<<l<<r<<endl;
    //[a,b)と[l,r)が交差しなければ,INT_MAX
    if(r<=a || b<=l) return 2147483647;

    //[a,b)が[l,r)を完全に含んでいれば、この節点の値
    if(a<=l && r<=b) return dat[k];
    else{
        //そうでなければ、2つの子の最小値
        ll vl=query(a,b,k*2+1,l,(l+r)/2);
        ll vr=query(a,b,k*2+2,(l+r)/2,r);
        return min(vl,vr);
    }
}

int main(){
    int N,Q,com,l,r;
    ll a;
    cin>>N>>Q;
    init(N);

    for(int i=0;i<N;i++){
        cin>>a;
        C[a]=i;
        update(i,a);
    }

    for(int i=0;i<Q;i++){
        cin>>com>>l>>r;
        l--;r--;
        if(com==1){
            a=dat[l+n-1];
            swap(C[a],C[dat[r+n-1]]);
            update(l,dat[r+n-1]);
            update(r,a);
        }else{
            a=query(l,r+1,0,0,n);
            cout<<C[a]+1<<endl;
        }
    }
}
0