結果

問題 No.878 Range High-Element Query
ユーザー t33ft33f
提出日時 2019-09-12 20:01:05
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 250 ms / 2,000 ms
コード長 971 bytes
コンパイル時間 824 ms
コンパイル使用メモリ 81,840 KB
実行使用メモリ 8,600 KB
最終ジャッジ日時 2023-09-15 14:16:50
合計ジャッジ時間 3,974 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 3 ms
4,380 KB
testcase_02 AC 3 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 3 ms
4,380 KB
testcase_05 AC 4 ms
4,376 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 3 ms
4,380 KB
testcase_09 AC 3 ms
4,376 KB
testcase_10 AC 3 ms
4,376 KB
testcase_11 AC 244 ms
7,648 KB
testcase_12 AC 157 ms
7,280 KB
testcase_13 AC 195 ms
6,844 KB
testcase_14 AC 147 ms
6,048 KB
testcase_15 AC 160 ms
7,268 KB
testcase_16 AC 230 ms
8,080 KB
testcase_17 AC 250 ms
8,600 KB
testcase_18 AC 248 ms
8,392 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstring>
#include <stack>
#include <vector>
#include <algorithm>
#include <numeric>
#include <iostream>
using namespace std;
int main() {
    int n, q; cin >> n >> q;
    int a[n]; for (int i = 0; i < n; i++) cin >> a[i];
    vector<pair<int, int> > qs[n];
    for (int i = 0; i < q; i++) {
        int x, l, r; cin >> x >> l >> r;
        qs[l-1].push_back({r-1, i});
    }
    stack<pair<int, int> > st;
    int bit[n+1];
    memset(bit, 0, sizeof(bit));
    vector<int> ans(q, 0);
    for (int i = n-1; i >= 0; i--) {
        while (!st.empty() && st.top().first < a[i]) {
            for (int x = st.top().second+1; x <= n; x += x&-x) bit[x]--;
            st.pop();
        }
        st.push({a[i], i});
        for (int x = i+1; x <= n; x += x&-x) bit[x]++;
        for (auto query : qs[i]) {
            for (int x = query.first+1; x > 0; x -= x&-x)
                ans[query.second] += bit[x];
        }
    }
    for (int a : ans) cout << a << endl;
}
0