結果

問題 No.878 Range High-Element Query
ユーザー tonegawatonegawa
提出日時 2020-08-12 00:47:37
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 140 ms / 2,000 ms
コード長 1,913 bytes
コンパイル時間 1,389 ms
コンパイル使用メモリ 109,068 KB
実行使用メモリ 13,184 KB
最終ジャッジ日時 2024-04-17 17:59:58
合計ジャッジ時間 3,438 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 1 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 3 ms
5,376 KB
testcase_10 AC 1 ms
5,376 KB
testcase_11 AC 137 ms
12,800 KB
testcase_12 AC 83 ms
10,240 KB
testcase_13 AC 109 ms
10,368 KB
testcase_14 AC 82 ms
9,216 KB
testcase_15 AC 87 ms
10,496 KB
testcase_16 AC 131 ms
12,544 KB
testcase_17 AC 140 ms
13,184 KB
testcase_18 AC 140 ms
13,056 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <deque>
#include <algorithm>
#include <set>
#include <map>
#include <bitset>
#include <cmath>
#include <functional>
#include <iomanip>
#define vll vector<ll>
#define vvvl vector<vvl>
#define vvl vector<vector<ll>>
#define VV(a, b, c, d) vector<vector<d>>(a, vector<d>(b, c))
#define VVV(a, b, c, d) vector<vvl>(a, vvl(b, vll (c, d)));
#define re(c, b) for(ll c=0;c<b;c++)
#define all(obj) (obj).begin(), (obj).end()
typedef long long int ll;
typedef long double ld;
using namespace std;

struct BIT{
  ll M=1;
  vector<ll> dat;
  BIT(ll N){
    while(N>M) M*=2;
    dat.resize(M*2-1, 0);
  }
  void update(ll x, ll k){
    for(int i=k+1;i<=M;i+=(i&(-i))){
      dat[i] += x;
    }
  }
  ll sum(ll r){
    ll ret = 0;
    for(int k=r;k>0;k-=(k&(-k))) ret += dat[k];
    return ret;
  }

  ll query(ll l, ll r){
    return sum(r) - sum(l);
  }
};

int main(int argc, char const *argv[]) {
  ll n, q;scanf("%lld %lld", &n,&q);
  vll a(n);re(i, n) scanf("%lld", &a[i]);
  vll pre(n, 0);
  BIT bt(n+1);

  for(int i=1;i<n;i++){
    if(a[i]<a[i-1]) pre[i] = i;
    else{
      ll p = pre[i-1];
      while(p!=0&&a[p-1]<a[i]) p--;
      pre[i] = p;
    }
  }
  //for(int i=0;i<n;i++) std::cout << pre[i] << (i==n-1?"\n":" ");

  vvl d = VV(q, 3, 0, ll);
  for(int i=0;i<q;i++){
    ll x, y, z;scanf("%lld %lld %lld",&x,&y,&z);
    d[i] = {y, z, i};
  }
  sort(all(d), [](vll x, vll y){return x[1]<y[1];});//r
  vll ans(q, 0);

  ll idx = 0;

  for(int r=1;r<=n;r++){
    //更新
    bt.update(-1, pre[r-1]);
    bt.update(1, r);
    while(idx!=q&&d[idx][1]==r){
      ll num = bt.query(d[idx][0], r+1);
      //std::cout << num << " " << d[idx][2] << '\n';
      ans[d[idx][2]] = num;
      idx++;
    }
    //for(int j=0;j<=n;j++) std::cout << bt.query(j, j+1) << (j==n?"\n":" ");
  }
  re(i, q) printf("%lld\n", ans[i]);
  return 0;
}
0