結果

問題 No.878 Range High-Element Query
ユーザー m_tsubasam_tsubasa
提出日時 2020-07-31 18:14:56
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 440 ms / 2,000 ms
コード長 2,249 bytes
コンパイル時間 2,316 ms
コンパイル使用メモリ 212,752 KB
実行使用メモリ 21,932 KB
最終ジャッジ日時 2023-09-20 18:36:11
合計ジャッジ時間 7,197 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 3 ms
4,376 KB
testcase_02 AC 4 ms
4,376 KB
testcase_03 AC 3 ms
4,376 KB
testcase_04 AC 3 ms
4,380 KB
testcase_05 AC 4 ms
4,380 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 4 ms
4,376 KB
testcase_09 AC 5 ms
4,376 KB
testcase_10 AC 3 ms
4,380 KB
testcase_11 AC 396 ms
18,196 KB
testcase_12 AC 313 ms
19,596 KB
testcase_13 AC 317 ms
14,788 KB
testcase_14 AC 241 ms
12,856 KB
testcase_15 AC 306 ms
18,808 KB
testcase_16 AC 413 ms
21,324 KB
testcase_17 AC 439 ms
21,932 KB
testcase_18 AC 440 ms
21,784 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

// 0-indexed
template <class T>
struct SegmentTree {
  // a,b,c: T, e:T(unit)
  // abc = (ab)c = a(bc)
  // ae = ea = a
  typedef function<T(T, T)> F;
  int n;
  F f;
  T unit;
  vector<T> dat;
  SegmentTree(){};
  SegmentTree(int newn, F f, T t) : f(f), unit(t) { init(newn); }
  SegmentTree(const vector<T> &v, F f, T t) : f(f), unit(t) {
    int _n = v.size();
    init(v.size());
    for (int i = 0; i < _n; ++i) dat[n + i] = v[i];
    for (int i = n - 1; i; --i) dat[i] = f(dat[i << 1], dat[(i << 1) | 1]);
  }
  void init(int newn) {
    n = 1;
    while (n < newn) n <<= 1;
    dat.assign(n << 1, unit);
  }

  // "go up" process
  void update(int k, T newdata) {
    dat[k += n] = newdata;
    while (k >>= 1) {
      dat[k] = f(dat[(k << 1) | 0], dat[(k << 1) | 1]);
    }
  }
  // [a,b)
  T query(int a, int b) {
    T vl = unit, vr = unit;
    for (int l = a + n, r = b + n; l < r; l >>= 1, r >>= 1) {
      if (l & 1) vl = f(vl, dat[l++]);
      if (r & 1) vr = f(dat[--r], vr);
    }
    return f(vl, vr);
  }
};

using P = pair<int, int>;

int n, q;
vector<P> v;
vector<vector<int>> dp;
SegmentTree<P> seg;

int main() {
  cin >> n >> q;
  v.resize(n);
  for (int i = 0; i < n; ++i) {
    cin >> v[i].first;
    v[i].second = i;
  }
  v.emplace_back(n + 1, n);
  {  // make seg
    auto f = [](P l, P r) { return max(l, r); };
    seg = SegmentTree<P>(v, f, P(-1, -1));
  }
  {  // dp
    auto binary = [](int id) {
      int l = id, r = n;
      while (r - l > 1) {
        int mid = (l + r) / 2;
        if (seg.query(id, mid + 1).second > id)
          r = mid;
        else
          l = mid;
      }
      return r;
    };
    dp.assign(n + 1, vector<int>(31, n));
    for (int i = 0; i < n; ++i) dp[i][0] = binary(i);
    for (int i = n - 1; i >= 0; --i)
      for (int j = 0; j < 30; ++j) dp[i][j + 1] = dp[dp[i][j]][j];
  }
  {  // query
    auto calc = [](int l, int r) {
      int res = 1;
      for (int i = 30; i >= 0; --i)
        if (dp[l][i] < r) {
          l = dp[l][i];
          res += 1 << i;
        }
      return res;
    };
    for (int i = 0; i < q; ++i) {
      int a, b, c;
      cin >> a >> b >> c;
      cout << calc(b - 1, c) << endl;
    }
  }
  return 0;
}
0