結果

問題 No.878 Range High-Element Query
ユーザー face4face4
提出日時 2019-10-13 11:17:44
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 338 ms / 2,000 ms
コード長 2,167 bytes
コンパイル時間 1,103 ms
コンパイル使用メモリ 95,692 KB
実行使用メモリ 12,244 KB
最終ジャッジ日時 2023-08-21 05:11:56
合計ジャッジ時間 5,650 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 3 ms
4,376 KB
testcase_02 AC 3 ms
4,376 KB
testcase_03 AC 3 ms
4,376 KB
testcase_04 AC 3 ms
4,376 KB
testcase_05 AC 4 ms
4,376 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 4 ms
4,376 KB
testcase_09 AC 4 ms
4,384 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 312 ms
10,764 KB
testcase_12 AC 219 ms
10,688 KB
testcase_13 AC 251 ms
9,172 KB
testcase_14 AC 190 ms
7,948 KB
testcase_15 AC 223 ms
10,284 KB
testcase_16 AC 316 ms
11,772 KB
testcase_17 AC 336 ms
12,244 KB
testcase_18 AC 338 ms
12,136 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<vector>
#include<algorithm>
#include<functional>
#include<climits>
#include<set>
using namespace std;

template<typename T>
struct Seg{
private:
    vector<T> node;
    int n;
    function<T(T,T)> f;
    T def;

public:
    Seg(int siz, T d, function<T(T,T)> f) : def(d), f(f) {
        n = 1;
        while(n < siz)  n *= 2;
        node.resize(2*n-1, def);
    }
    Seg(vector<T> v, T d, function<T(T,T)> f) : def(d), f(f){
        n = 1;
        while(n < v.size())  n *= 2;
        node.resize(2*n-1);
        for(int i = 0; i < v.size(); i++)   node[n-1+i] = v[i];
        for(int i = n-2; i >= 0; i--)   node[i] = f(node[2*i+1], node[2*i+2]);
    }

    void update(int x, T val){
        x += n-1;
        node[x] = val; /* ! */
        while(x){
            x = (x-1)/2;
            node[x] = f(node[2*x+1], node[2*x+2]);
        }
    }

    T query(int a, int b, int k=0, int l=0, int r=-1){
        if(r < 0)   r = n;
        if(b <= l || r <= a)    return def;
        if(a <= l && r <= b)    return node[k];
        T lx = query(a, b, 2*k+1, l, (l+r)/2);
        T rx = query(a, b, 2*k+2, (l+r)/2, r);
        return f(lx, rx);
    }
};

int main(){
    int n, q;
    cin >> n >> q;
    int a[n], rev[n];
    for(int i = 0; i < n; i++){
        cin >> a[i];
        rev[--a[i]] = i;
    }
    set<int> s; s.insert(-1); s.insert(n);
    vector<pair<int,int>> vp;
    for(int i = n-1; i >= 0; i--){
        auto it = --s.lower_bound(rev[i]);
        vp.push_back({*it+1, rev[i]});
        s.insert(rev[i]);
    }
    vector<pair<int,int>> query(q);
    for(int i = 0; i < q; i++){
        int x, y, z;
        cin >> x >> y >> z;
        vp.push_back({y-1, i+n});
        query[i] = {y-1, z-1};
    }
    sort(vp.begin(), vp.end());
    Seg<int> stsum(n, 0, [](int x, int y){return x+y;});
    vector<int> ans(q);
    for(pair<int,int> p : vp){
        int i = p.first, j = p.second;
        if(j >= n){
            j -= n;
            ans[j] = stsum.query(query[j].first, query[j].second+1);
        }else{
            stsum.update(j, 1);
        }
    }
    for(int i = 0; i < q; i++)  cout << ans[i] << endl;
    return 0;
}
0