結果
問題 |
No.878 Range High-Element Query
|
ユーザー |
![]() |
提出日時 | 2019-09-12 20:01:05 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 238 ms / 2,000 ms |
コード長 | 971 bytes |
コンパイル時間 | 783 ms |
コンパイル使用メモリ | 82,456 KB |
実行使用メモリ | 8,448 KB |
最終ジャッジ日時 | 2024-07-02 17:09:42 |
合計ジャッジ時間 | 3,254 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 1 |
other | AC * 18 |
ソースコード
#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; }