結果

問題 No.878 Range High-Element Query
ユーザー betrue12betrue12
提出日時 2019-09-06 22:25:07
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 300 ms / 2,000 ms
コード長 1,809 bytes
コンパイル時間 1,903 ms
コンパイル使用メモリ 185,036 KB
実行使用メモリ 9,396 KB
最終ジャッジ日時 2023-09-07 01:02:45
合計ジャッジ時間 5,109 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
6,012 KB
testcase_01 AC 4 ms
5,840 KB
testcase_02 AC 5 ms
5,780 KB
testcase_03 AC 4 ms
5,708 KB
testcase_04 AC 5 ms
5,776 KB
testcase_05 AC 4 ms
5,704 KB
testcase_06 AC 3 ms
5,712 KB
testcase_07 AC 3 ms
5,760 KB
testcase_08 AC 6 ms
5,772 KB
testcase_09 AC 6 ms
5,928 KB
testcase_10 AC 4 ms
5,768 KB
testcase_11 AC 290 ms
8,924 KB
testcase_12 AC 189 ms
8,256 KB
testcase_13 AC 230 ms
8,100 KB
testcase_14 AC 174 ms
7,520 KB
testcase_15 AC 192 ms
8,452 KB
testcase_16 AC 277 ms
8,972 KB
testcase_17 AC 300 ms
9,396 KB
testcase_18 AC 297 ms
8,788 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

template<typename T>
struct Segtree {
    int n;
    T e;
    vector<T> dat;
    typedef function<T(T a, T b)> Func;
    Func f;

    Segtree(){}
    Segtree(int n_input, Func f_input, T e_input){
        initialize(n_input, f_input, e_input);
    }
    void initialize(int n_input, Func f_input, T e_input){
        f = f_input;
        e = e_input;
        n = 1;
        while(n < n_input) n <<= 1;
        dat.resize(2*n-1, e);
    }

    void update(int k, T a){
        k += n - 1;
        dat[k] = a;
        while(k > 0){
            k = (k - 1)/2;
            dat[k] = f(dat[2*k+1], dat[2*k+2]);
        }
    }

    T get(int k){
        return dat[k+n-1];
    }

    T between(int a, int b){
        return query(a, b+1, 0, 0, n);
    }

    T query(int a, int b, int k, int l, int r){
        if(r<=a || b<=l) return e;
        if(a<=l && r<=b) return dat[k];
        T vl = query(a, b, 2*k+1, l, (l+r)/2);
        T vr = query(a, b, 2*k+2, (l+r)/2, r);
        return f(vl, vr);
    }
};

int main(){
    int N, Q;
    cin >> N >> Q;
    vector<int> A(N);
    for(int i=0; i<N; i++) cin >> A[i];

    vector<pair<int, int>> qs[100000];
    for(int i=0; i<Q; i++){
        int t, l, r;
        cin >> t >> l >> r;
        qs[l-1].emplace_back(r-1, i);
    }
    vector<int> ans(Q);
    Segtree<int> st(N, [](int a, int b){ return a+b; }, 0);
    map<int, int> mp;
    for(int l=N-1; l>=0; l--){
        mp[A[l]] = l;
        st.update(l, 1);
        while(mp.begin()->first < A[l]){
            int i = mp.begin()->second;
            st.update(i, 0);
            mp.erase(mp.begin());
        }
        for(auto& p : qs[l]){
            ans[p.second] = st.between(l, p.first);
        }
    }
    for(int a : ans) cout << a << endl;
    return 0;
}
0