結果

問題 No.878 Range High-Element Query
ユーザー risujirohrisujiroh
提出日時 2019-09-06 21:50:30
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 75 ms / 2,000 ms
コード長 1,687 bytes
コンパイル時間 1,681 ms
コンパイル使用メモリ 171,220 KB
実行使用メモリ 18,016 KB
最終ジャッジ日時 2023-09-06 23:38:07
合計ジャッジ時間 3,787 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,384 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 1 ms
4,384 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,384 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 70 ms
15,000 KB
testcase_12 AC 59 ms
16,164 KB
testcase_13 AC 54 ms
12,300 KB
testcase_14 AC 42 ms
10,820 KB
testcase_15 AC 55 ms
15,256 KB
testcase_16 AC 70 ms
17,536 KB
testcase_17 AC 73 ms
17,876 KB
testcase_18 AC 75 ms
18,016 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using lint = long long;
template<class T = int> using V = vector<T>;
template<class T = int> using VV = V< V<T> >;

struct DisjointSparseTable {
  using T = int;
  static T op(const T& x, const T& y) { return max(x, y); }

  const int n;
  VV<T> t;
  template<class Itr> DisjointSparseTable(Itr first, Itr last) :
    n(distance(first, last)), t(__lg(n) + 1, V<T>(first, last)) {
    for (int k = 1; k < (int) t.size(); ++k) {
      for (int i0 = 1 << k; i0 < n; i0 += 1 << k + 1) {
        for (int i = i0 + 1; i < min(i0 + (1 << k), n); ++i) {
          t[k][i] = op(t[k][i - 1], t[k][i]);
        }
        for (int i = i0 - 2; i >= i0 - (1 << k); --i) {
          t[k][i] = op(t[k][i], t[k][i + 1]);
        }
      }
    }
  }
  T acc(int l, int r) const {
    assert(l < r);
    if (l == --r) return t[0][l];
    int k = __lg(l ^ r);
    return op(t[k][l], t[k][r]);
  }
};

int main() {
  cin.tie(nullptr); ios::sync_with_stdio(false);
  int n, q; cin >> n >> q;
  V<> a(n); for (auto&& e : a) cin >> e;
  DisjointSparseTable dst(begin(a), end(a));
  VV<> dp(20, V<>(n));
  for (int i = 0; i < n; ++i) {
    int ok = i + 1, ng = n + 1;
    while (ng - ok > 1) {
      int mid = ok + ng >> 1;
      (dst.acc(i, mid) == a[i] ? ok : ng) = mid;
    }
    dp[0][i] = ok;
  }
  for (int k = 0; k < 19; ++k) for (int i = 0; i < n; ++i) {
    dp[k + 1][i] = dp[k][i] == n ? n : dp[k][dp[k][i]];
  }

  while (q--) {
    int tp; cin >> tp;
    int l, r; cin >> l >> r, --l;
    int res = 0;
    for (int k = 19; k >= 0; --k) {
      if (dp[k][l] >= r) continue;
      l = dp[k][l];
      res |= 1 << k;
    }
    cout << res + 1 << '\n';
  }
}
0