結果

問題 No.878 Range High-Element Query
ユーザー lumc_lumc_
提出日時 2019-09-07 00:44:27
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 62 ms / 2,000 ms
コード長 2,614 bytes
コンパイル時間 5,774 ms
コンパイル使用メモリ 120,832 KB
実行使用メモリ 6,260 KB
最終ジャッジ日時 2023-09-07 04:48:17
合計ジャッジ時間 3,604 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,384 KB
testcase_02 AC 2 ms
4,384 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,388 KB
testcase_05 AC 2 ms
4,384 KB
testcase_06 AC 2 ms
4,384 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 2 ms
4,384 KB
testcase_09 AC 2 ms
4,384 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 59 ms
6,260 KB
testcase_12 AC 41 ms
5,260 KB
testcase_13 AC 49 ms
5,652 KB
testcase_14 AC 36 ms
4,892 KB
testcase_15 AC 41 ms
5,384 KB
testcase_16 AC 58 ms
5,908 KB
testcase_17 AC 62 ms
6,200 KB
testcase_18 AC 61 ms
6,204 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

// includes {{{
#include<iostream>
#include<iomanip>
#include<algorithm>
#include<vector>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<tuple>
#include<cmath>
#include<random>
#include<cassert>
#include<bitset>
#include<cstdlib>
// #include<deque>
// #include<multiset>
// #include<cstring>
// #include<bits/stdc++.h>
// }}}
using namespace std;
using ll = long long;

// .add(i, v)   : bit[i] += v
// .get(i)      : bit[i]
// .sum(i)      : bit[0] + ... + bit[i]
// .range(l, r) : bit[l] + ... + bit[r]
// .lower_bound(T v) : min i that satisfies .sum(i) >= v
//    - use only when bit[i] >= 0 for all i > 0
/// --- Binary Indexed Tree {{{ ///
#include <cassert>
#include <vector>
template < class T = long long >
struct BinaryIndexedTree {
  using size_type = std::size_t;
  size_type n, m;
  T identity;
  std::vector< T > data;
  BinaryIndexedTree() : n(0) {}
  BinaryIndexedTree(int n, T identity = T())
    : n(n), identity(identity), data(n, identity) {
      m = 1;
      while(m < n) m <<= 1;
    }
  void add(size_type i, T x) {
    assert(i < n);
    i++;
    while(i <= n) {
      data[i - 1] = data[i - 1] + x;
      i += i & -i;
    }
  }
  T sum(int i) {
    if(i < 0) return identity;
    if(i >= n) i = n - 1;
    i++;
    T s = identity;
    while(i > 0) {
      s = s + data[i - 1];
      i -= i & -i;
    }
    return s;
  }
  T get(int i) { return sum(i) - sum(i - 1); }
  T range(int a, int b) { return sum(b) - sum(a - 1); }
  size_type lower_bound(T w) {
    size_type i = 0;
    for(size_type k = m; k > 0; k >>= 1) {
      if(i + k <= n && data[i + k - 1] < w) w -= data[(i += k) - 1];
    }
    return i;
  }
};
/// }}}--- ///

template < class T = long long >
using BIT = BinaryIndexedTree< T >;


int n, q;
int a[112345];
ll ans[112345];
int main() {
  std::ios::sync_with_stdio(false), std::cin.tie(0);
  cin >> n >> q;
  using P = pair<int, int>;
  priority_queue<P, vector<P>, greater<P>> pq;
  BIT<> bit(n);
  for(int i = 0; i < n; i++) {
    cin >> a[i];
  }
  vector<tuple<int, int, int>> v;
  for(int i = 0; i < q; i++) {
    int t, l, r;
    cin >> t >> l >> r;
    l--;
    v.emplace_back(l, r, i);
  }
  sort(begin(v), end(v));
  
  for(int i = n - 1; i >= 0; i--) {
    while(pq.size() and pq.top().first < a[i]) {
      bit.add(pq.top().second, -1);
      pq.pop();
    }
    bit.add(i, 1);
    pq.emplace(a[i], i);
    while(v.size() and get<0>(v.back()) == i) {
      int l, r, id;
      tie(l, r, id) = v.back();
      v.pop_back();
      ans[id] = bit.range(l, r - 1);
    }
  }
  for(int i = 0; i < q; i++) cout << ans[i] << "\n";
  return 0;
}
0