結果

問題 No.878 Range High-Element Query
ユーザー finefine
提出日時 2019-09-06 22:16:24
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 276 ms / 2,000 ms
コード長 3,348 bytes
コンパイル時間 2,001 ms
コンパイル使用メモリ 178,804 KB
実行使用メモリ 24,188 KB
最終ジャッジ日時 2023-09-07 00:47:05
合計ジャッジ時間 5,030 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,384 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 3 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 3 ms
4,380 KB
testcase_05 AC 3 ms
4,380 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 3 ms
4,376 KB
testcase_09 AC 3 ms
4,380 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 255 ms
21,556 KB
testcase_12 AC 169 ms
22,604 KB
testcase_13 AC 198 ms
15,300 KB
testcase_14 AC 142 ms
13,780 KB
testcase_15 AC 172 ms
21,972 KB
testcase_16 AC 256 ms
23,752 KB
testcase_17 AC 276 ms
24,188 KB
testcase_18 AC 270 ms
23,896 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

using ll = long long;
using P = pair<vector<ll>, ll>;

template <typename T>
struct SegmentTree {
    int n;
    vector<T> data;
    T INITIAL_VALUE;

    //使うときは、この2つを適宜変更する
    static T merge(const T& x, const T& y);
    void updateNode(int k, const T& x);

    SegmentTree(int size, T initial_value) {
        n = 1;
        INITIAL_VALUE = initial_value;
        while (n < size) n *= 2;
        data.resize(2 * n - 1, INITIAL_VALUE);
    }

    SegmentTree(const vector<T>& v, const T& initial_value) {
        int size = v.size();
        n = 1;
        INITIAL_VALUE = initial_value;
        while (n < size) n *= 2;
        data.resize(2 * n - 1, INITIAL_VALUE);

        for (int i = 0; i < size; i++) data[i + n - 1] = v[i];
        for (int i = n - 2; i >= 0; i--) data[i] = merge(data[i * 2 + 1], data[i * 2 + 2]);
    }

    T getLeaf(int k) {
        return data[k + n - 1];
    }

    void update(int k, T x) {
        k += n - 1; //葉の節点
        updateNode(k, x);
        while (k > 0) {
            k = (k - 1) / 2;
            data[k] = merge(data[k * 2 + 1], data[k * 2 + 2]);
        }
    }

    //区間[a, b)に対するクエリに答える
    //k:節点番号, [l, r):節点に対応する区間
    T query(int a, int b, int k, int l, int r) {
        //[a, b)と[l, r)が交差しない場合
        if (r <= a || b <= l) return INITIAL_VALUE;
        //[a, b)が[l, r)を含む場合、節点の値
        if (a <= l && r <= b) return data[k];
        else {
            //二つの子をマージ
            T vl = query(a, b, k * 2 + 1, l, (l + r) / 2);
            T vr = query(a, b, k * 2 + 2, (l + r) / 2, r);
            return merge(vl, vr);
        }
    }

    //外から呼ぶ用
    T query(int a, int b) {
        return query(a, b, 0, 0, n);
    }

    //非再帰版: バグってるかもしれないので定数倍高速化する時以外使わないで
    //区間[a, b)に対するクエリに答える
    T query_fast(int a, int b) {
        T vl = INITIAL_VALUE, vr = INITIAL_VALUE;
        for (int l = a + n, r = b + n; l != r; l >>= 1, r >>= 1) {
            if (l & 1) vl = merge(vl, data[l++ - 1]);
            if (r & 1) vr = merge(data[--r - 1], vr);
        }
        return merge(vl, vr);
    }
};

//使うときは以下2つを変更
//非可換の場合は順序に注意!!!
template <typename T>
T SegmentTree<T>::merge(const T& x, const T& y) {
    int idx = lower_bound(y.first.begin(), y.first.end(), x.second) - y.first.begin();
    P res(x.first, 0);
    for (int i = idx; i < y.first.size(); i++) {
        res.first.push_back(y.first[i]);
    }
    res.second = max(x.second, y.second);
    return res;
}

template <typename T>
void SegmentTree<T>::updateNode(int k, const T& x) {
    data[k] = x;
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n, q;
    cin >> n >> q;
    vector<P> v;
    for (int i = 0; i < n; i++) {
        ll a;
        cin >> a;
        vector<ll> tmp = {a};
        v.emplace_back(tmp, a);
    }

    vector<ll> emp;
    SegmentTree<P> st(v, P(emp, 0));
    for (int i = 0; i < q; i++) {
        int c, l, r;
        cin >> c >> l >> r;
        l--;
        cout << st.query_fast(l, r).first.size() << "\n";
    }
    return 0;
}
0