結果

問題 No.878 Range High-Element Query
ユーザー TAISA_TAISA_
提出日時 2019-09-06 22:59:12
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 412 ms / 2,000 ms
コード長 3,333 bytes
コンパイル時間 2,579 ms
コンパイル使用メモリ 188,020 KB
実行使用メモリ 28,172 KB
最終ジャッジ日時 2023-09-07 02:04:16
合計ジャッジ時間 6,621 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 4 ms
4,384 KB
testcase_02 AC 3 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 3 ms
4,380 KB
testcase_05 AC 3 ms
4,380 KB
testcase_06 AC 2 ms
4,384 KB
testcase_07 AC 2 ms
4,384 KB
testcase_08 AC 4 ms
4,380 KB
testcase_09 AC 4 ms
4,384 KB
testcase_10 AC 2 ms
4,384 KB
testcase_11 AC 374 ms
24,412 KB
testcase_12 AC 266 ms
25,792 KB
testcase_13 AC 297 ms
18,044 KB
testcase_14 AC 220 ms
15,900 KB
testcase_15 AC 271 ms
24,644 KB
testcase_16 AC 383 ms
27,464 KB
testcase_17 AC 412 ms
28,172 KB
testcase_18 AC 404 ms
27,928 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define all(vec) vec.begin(), vec.end()
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
constexpr ll INF = (1LL << 30) - 1LL;
constexpr ll LINF = (1LL << 60) - 1LL;
constexpr double eps = 1e-9;
constexpr ll MOD = 1000000007LL;
template <typename T> bool chmin(T &a, T b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
};
template <typename T> bool chmax(T &a, T b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
};
template <typename T> ostream &operator<<(ostream &os, vector<T> v) {
    for (int i = 0; i < v.size(); i++) {
        os << v[i] << (i + 1 == v.size() ? "\n" : " ");
    }
    return os;
}
template <typename T> vector<T> make_v(size_t a) { return vector<T>(a); }
template <typename T, typename... Ts> auto make_v(size_t a, Ts... ts) {
    return vector<decltype(make_v<T>(ts...))>(a, make_v<T>(ts...));
}
template <typename T, typename V>
typename enable_if<is_class<T>::value == 0>::type fill_v(T &t, const V &v) {
    t = v;
}
template <typename T, typename V>
typename enable_if<is_class<T>::value != 0>::type fill_v(T &t, const V &v) {
    for (auto &e : t) {
        fill_v(e, v);
    }
};

struct Segtree {
    int n;
    vector<vector<int>> dat;
    Segtree(int n_) {
        n = 1;
        while (n < n_) {
            n <<= 1;
        }
        dat.resize(2 * n);
    }
    /* void upd(int k, int x) {
         k += n;
         dat[k] = x;
         k >>= 1;
         while (k > 0) {
             dat[k] = dat[k << 1] + dat[k << 1 | 1];
             k >>= 1;
         }
     }*/
    void build(vector<int> &a) {
        for (int i = n; i < 2 * n; i++) {
            if (i - n >= a.size()) {
                break;
            }
            dat[i].emplace_back(a[i - n]);
        }
        for (int i = n - 1; i >= 1; i--) {
            for (auto &e : dat[2 * i]) {
                dat[i].emplace_back(e);
            }
            for (auto &e : dat[2 * i + 1]) {
                dat[i].emplace_back(e);
            }
            sort(all(dat[i]));
        }
    }
    int get(int a, int b, int x, int k, int l, int r) {
        if (b <= l || r <= a) {
            return 0;
        }
        if (a <= l && r <= b) {
            int s = lower_bound(all(dat[k]), x) - dat[k].begin();
            return s;
        }
        return get(a, b, x, 2 * k, l, (l + r) / 2) +
               get(a, b, x, 2 * k + 1, (l + r) / 2, r);
    }
    int get(int a, int b, int x) {
        if (a >= b) {
            return 0;
        }
        return get(a, b, x, 1, 0, n);
    }
};
int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n, q;
    cin >> n >> q;
    vector<int> a(n), b(n, -1);
    vector<P> v;
    for (int i = 0; i < n; i++) {
        cin >> a[i];
        v.push_back(P(a[i], i));
    }
    sort(all(v));
    reverse(all(v));
    set<int> st;
    for (int i = 0; i < n; i++) {
        int id = v[i].second;
        if (st.empty() || (*st.begin()) > id) {
            st.insert(id);
            continue;
        }
        auto it = st.lower_bound(id);
        --it;
        b[id] = *it;
        st.insert(id);
    }
    Segtree seg(n);
    seg.build(b);
    while (q--) {
        int t, l, r;
        cin >> t >> l >> r;
        --l;
        cout << seg.get(l, r, l) << endl;
    }
}
0