結果

問題 No.878 Range High-Element Query
ユーザー snow39snow39
提出日時 2019-09-06 23:12:23
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 349 ms / 2,000 ms
コード長 2,563 bytes
コンパイル時間 1,249 ms
コンパイル使用メモリ 111,984 KB
実行使用メモリ 11,088 KB
最終ジャッジ日時 2023-09-07 02:26:29
合計ジャッジ時間 4,823 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,384 KB
testcase_01 AC 3 ms
4,376 KB
testcase_02 AC 3 ms
4,380 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 3 ms
4,380 KB
testcase_05 AC 4 ms
4,376 KB
testcase_06 AC 3 ms
4,376 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 4 ms
4,376 KB
testcase_09 AC 4 ms
4,376 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 334 ms
10,340 KB
testcase_12 AC 220 ms
10,712 KB
testcase_13 AC 264 ms
7,740 KB
testcase_14 AC 196 ms
7,164 KB
testcase_15 AC 227 ms
10,432 KB
testcase_16 AC 327 ms
10,980 KB
testcase_17 AC 346 ms
11,036 KB
testcase_18 AC 349 ms
11,088 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <cmath>
#include <map>
#include <queue>
#include <iomanip>
#include <set>
#include <tuple>
#define mkp make_pair
#define mkt make_tuple
#define rep(i,n) for(int i = 0; i < (n); ++i)
using namespace std;
typedef long long ll;
const ll MOD=1e9+7;

#include <functional>
#include <climits>

//SegmentTree<int> seg(N,[](int a,int b){return min(a,b);},INT_MAX);
template< typename T>
class SegmentTree{
public:

  using F = function<T(T,T)>;

  int n;
  vector<T> tree;
  const F operation;
  const T def;

  SegmentTree(int size,F _operation,T _def):operation(_operation),def(_def){
    n=1;
    while(n<size) n*=2;
    tree.resize(2*n-1,def);
  }

  void initialize(vector<T> v){
    int vSize=v.size();
    n=1;
    while(n<vSize) n*=2;
    tree.resize(2*n-1,def);

    for(int i=0;i<vSize;i++) tree[i+n-1]=v[i];
    for(int i=n-2;i>=0;i--) tree[i]=operation(tree[2*i+1],tree[2*i+2]);
  }

  void update(int index,T value){
    index+=n-1;

    tree[index]=value;
    while(index>0){
      index=(index-1)/2;
      tree[index]=operation(tree[2*index+1],tree[2*index+2]);
    }
  }

  T query(int a,int b,int k=0,int l=0,int r=-1){//[a,b),getMin(a,b,0,0,-1)
    if(r<0) r=n;

    if(r<=a||b<=l) return def;
    else if(a<=l&&r<=b) return tree[k];
    else{
      T lval=query(a,b,2*k+1,l,(l+r)/2);
      T rval=query(a,b,2*k+2,(l+r)/2,r);
      return operation(lval,rval);
    }
  }

  T getVal(int index){
    return tree[index+n-1];
  }
};

class BIT{//1-indexed
public:

  vector<ll> bit;
  BIT(){}
  BIT(int size){
    bit.resize(size,0);
  }

  ll sum(int i){
    ll s=0;
    while(i>0){
      s+=bit[i];
      i-=i&(-i);
    }
    return s;
  }

  void add(int i,ll x){//i!=0
    while(i<bit.size()){
      bit[i]+=x;
      i+=i&(-i);
    }
  }
};


int N,Q;
vector<ll> A;

int dp[100010];

int main(){
  cin>>N>>Q;
  A.resize(N);
  for(int i=0;i<N;i++){
    cin>>A[i];
    A[i]--;
  }
  vector<pair<ll,int>> v;
  for(int i=0;i<N;i++) v.push_back(mkp(A[i],i));

  SegmentTree<pair<ll,int>> seg(N,[](pair<ll,int> a,pair<ll,int> b){return max(a,b);},mkp(0,0));
  seg.initialize(v);

  SegmentTree<int> ind(N+1,[](int a,int b){return min(a,b);},N+1);
  for(int i=N-1;i>=0;i--){
    dp[i]=1;
    auto a=ind.query(A[i],N+1);
    if(a!=N+1) dp[i]+=dp[a];
    ind.update(A[i],i);
  }

  for(int i=0;i<Q;i++){
    int t,l,r;
    cin>>t>>l>>r;
    l--;
    r--;

    auto f=seg.query(l,r+1);
    int tar=f.second;
    ll res=dp[l]-dp[tar]+1;
    cout<<res<<endl;
  }

  return 0;
}
0