結果

問題 No.878 Range High-Element Query
ユーザー tjaketjake
提出日時 2019-09-07 10:18:06
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 361 ms / 2,000 ms
コード長 2,853 bytes
コンパイル時間 853 ms
コンパイル使用メモリ 89,172 KB
実行使用メモリ 17,056 KB
最終ジャッジ日時 2023-09-08 12:19:13
合計ジャッジ時間 5,142 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
9,696 KB
testcase_01 AC 6 ms
9,736 KB
testcase_02 AC 6 ms
9,520 KB
testcase_03 AC 5 ms
9,520 KB
testcase_04 AC 6 ms
9,508 KB
testcase_05 AC 7 ms
9,476 KB
testcase_06 AC 6 ms
9,508 KB
testcase_07 AC 5 ms
9,476 KB
testcase_08 AC 7 ms
9,496 KB
testcase_09 AC 7 ms
9,572 KB
testcase_10 AC 6 ms
9,488 KB
testcase_11 AC 349 ms
15,640 KB
testcase_12 AC 224 ms
16,092 KB
testcase_13 AC 277 ms
14,236 KB
testcase_14 AC 203 ms
13,548 KB
testcase_15 AC 228 ms
15,712 KB
testcase_16 AC 340 ms
16,812 KB
testcase_17 AC 359 ms
16,984 KB
testcase_18 AC 361 ms
17,056 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<string>
#include<vector>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<algorithm>
#include<functional>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cassert>
#include<ctime>
using namespace std;

#define mind(a,b) (a>b?b:a)
#define maxd(a,b) (a>b?a:b)
#define absd(x) (x<0?-(x):x)
#define pow2(x) ((x)*(x))
#define rep(i,n) for(int i=0; i<n; ++i)
#define repr(i,n) for(int i=n-1; i>=0; --i)
#define repl(i,s,n) for(int i=s; i<=n; ++i)
#define replr(i,s,n) for(int i=n; i>=s; --i)
#define repf(i,s,n,j) for(int i=s; i<=n; i+=j)
#define repe(e,obj) for(auto e : obj)

#define SP << " " <<
#define COL << " : " <<
#define COM << ", " <<
#define ARR << " -> " <<
#define PNT(STR) cout << STR << endl
#define POS(X,Y) "(" << X << ", " << Y << ")"
#define DEB(A) " (" << #A << ") " << A
#define DEBREP(i,n,val) for(int i=0; i<n; ++i) cout << val << " "; cout << endl
#define ALL(V) (V).begin(), (V).end()
#define INF 1000000007
#define INFLL 1000000000000000007LL
#define EPS 1e-9

typedef unsigned int uint;
typedef unsigned long ulong;
typedef unsigned long long ull;
typedef long long ll;
typedef long double ld;
#define P_TYPE ll
typedef pair<P_TYPE, P_TYPE> P;
typedef pair<P, P_TYPE> PI;
typedef pair<P_TYPE, P> IP;
typedef pair<P, P> PP;
typedef priority_queue<P, vector<P>, greater<P> > pvqueue;

#define N (1 << 17)

class SegmentTree {
  vector<ll> data[2*N];
  int n0, n;

  void build(int k, int l, int r, ll *a) {
    if(l+1 == r) {
      if(l < n) data[k].push_back(a[l]);
      return;
    }

    int m = (l+r)/2;
    if(l < m) build(2*k+1, l, m, a);
    if(m < r) build(2*k+2, m, r, a);

    int sz = data[2*k+1].size() + data[2*k+2].size();
    data[k].reserve(sz);
    data[k].resize(data[2*k+1].size());
    copy(data[2*k+1].begin(), data[2*k+1].end(), data[k].begin());

    for(ll x : data[2*k+2]) {
      if(data[k].size() == 0 || data[k].back() <= x) {
        data[k].push_back(x);
      }
    }
    for(ll x : data[k]) {
    }
  }

  P _query(ll x, int a, int b, int k, int l, int r) {
    if(b <= l || r <= a) {
      return P{x, 0};
    }
    if(a <= l && r <= b) {
      auto it = lower_bound(data[k].begin(), data[k].end(), x);
      return P{max(x, data[k].back()), data[k].end() - it};
    }
    int m = (l + r) / 2;
    P lv = _query(x, a, b, 2*k+1, l, m);
    P rv = _query(lv.first, a, b, 2*k+2, m, r);
    return P{rv.first, lv.second + rv.second};
  }
public:
  SegmentTree(int n, ll *a) : n(n) {
    n0 = 1;
    while(n0 < n) n0 <<= 1;
    build(0, 0, n0, a);
  }

  ll query(int l, int r) {
    return _query(0, l, r, 0, 0, n0).second;
  }
};

ll a[N];

int main() {
  int n, q;
  cin >> n >> q;
  rep(i, n) cin >> a[i];
  SegmentTree st(n, a);
  while(q--) {
    int t, l, r;
    cin >> t >> l >> r;
    cout << st.query(l-1, r) << endl;
  }
  return 0;
}
0