結果

問題 No.875 Range Mindex Query
ユーザー rniyarniya
提出日時 2020-03-03 10:21:23
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 78 ms / 2,000 ms
コード長 1,578 bytes
コンパイル時間 2,106 ms
コンパイル使用メモリ 169,376 KB
実行使用メモリ 6,400 KB
最終ジャッジ日時 2024-04-21 23:25:14
合計ジャッジ時間 3,700 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 3 ms
5,376 KB
testcase_02 AC 3 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 3 ms
5,376 KB
testcase_10 AC 3 ms
5,376 KB
testcase_11 AC 72 ms
6,144 KB
testcase_12 AC 62 ms
5,376 KB
testcase_13 AC 55 ms
6,272 KB
testcase_14 AC 53 ms
6,272 KB
testcase_15 AC 71 ms
6,400 KB
testcase_16 AC 68 ms
6,400 KB
testcase_17 AC 77 ms
6,400 KB
testcase_18 AC 78 ms
6,400 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

template<typename Monoid>
struct SegmentTree{
    typedef function<Monoid(Monoid,Monoid)> F;
    int n;
    F f;
    Monoid id;
    vector<Monoid> dat;
    SegmentTree(int n_,F f,Monoid id):f(f),id(id){init(n_);}
    void init(int n_){
        n=1;
        while(n<n_) n<<=1;
        dat.assign(n<<1,id);
    }
    void build(const vector<Monoid> &v){
        for (int i=0;i<v.size();++i) dat[i+n]=v[i];
        for (int i=n-1;i;--i) dat[i]=f(dat[i<<1|0],dat[i<<1|1]);
    }
    void update(int k,Monoid x){
        dat[k+=n]=x;
        while(k>>=1) dat[k]=f(dat[k<<1|0],dat[k<<1|1]);
    }
    Monoid query(int a,int b){
        if (a>=b) return id;
        Monoid vl=id,vr=id;
        for (int l=a+n,r=b+n;l<r;l>>=1,r>>=1){
            if (l&1) vl=f(vl,dat[l++]);
            if (r&1) vr=f(dat[--r],vr);
        }
        return f(vl,vr);
    }
    Monoid operator[](int i){
        return dat[i+n];
    }
};

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    int N,Q; cin >> N >> Q;
    vector<int> a(N);
    for (int i=0;i<N;++i) cin >> a[i];
    struct node{int n,id;};
    auto f=[](node a,node b){return (a.n<b.n?a:b);};
    SegmentTree<node> seg(N,f,node{N,0});
    vector<node> v(N);
    for (int i=0;i<N;++i) v[i]={a[i]-1,i+1};
    seg.build(v);
    for (;Q--;){
        int c,l,r; cin >> c >> l >> r; --l,--r;
        if (c==1){
            int L=seg[l].n,R=seg[r].n;
            seg.update(l,node{R,l+1});
            seg.update(r,node{L,r+1});
        } else cout << seg.query(l,r+1).id << '\n';
    }
}
0