結果

問題 No.878 Range High-Element Query
ユーザー CinnamorollCinnamoroll
提出日時 2019-09-06 22:39:09
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 151 ms / 2,000 ms
コード長 6,731 bytes
コンパイル時間 2,172 ms
コンパイル使用メモリ 194,384 KB
実行使用メモリ 15,804 KB
最終ジャッジ日時 2023-09-07 01:33:54
合計ジャッジ時間 4,548 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 129 ms
14,236 KB
testcase_12 AC 112 ms
13,600 KB
testcase_13 AC 102 ms
11,404 KB
testcase_14 AC 78 ms
10,180 KB
testcase_15 AC 107 ms
13,504 KB
testcase_16 AC 141 ms
15,400 KB
testcase_17 AC 149 ms
15,688 KB
testcase_18 AC 151 ms
15,804 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

// warm heart, wagging tail,and a smile just for you!
//
//                            ▒█████▒▒
//                             ██████████▒
//                             ▒████████████▒
//                            ██████████████████
//                           ████████████████████▒
//                          ▒██████████████████████▒
//                          ▒███████████████████████
//                      ▒████▒▒▒▒▒▒█████████████████▒
//                    ███▒▒▒▒▒▒██████████████████████▒▒▒
//                  ▒██▒▒███████████████████████▒▒▒▒▒██████
//                 ▒█████████████████████████▒▒▒▒▒▒█████████▒
//                 ▒█████████████████████▒▒▒▒▒▒██████████████
//                  ▒████         ████▒▒▒▒▒████         ████▒
//              ▒█████▒    ████    ▒▒▒▒███████    ████    ██████▒
//            ▒██▒▒▒▒▒    ██████    █████████    ██████    ██▒▒▒██▒
//           █████████   ████████   █████████   ████████   ▒▒▒▒█████
//          ▒█████████    ██████    ████████▒    ██████    █████████
//          ▒██████████    ████    █████▒▒▒▒▒▒    ████    ██████████
//           ████████████        ▒▒▒▒▒▒▒████████        ███████████▒
//       ▒██████████▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒███████████████████████████████████▒
//     ███▒▒▒▒▒▒▒▒▒▒▒▒█████████████████████████████████████████▒▒████████▒
//   ▒▒▒▒▒▒▒▒▒██████████████                         ███████▒▒▒▒███████████
//   █████████████████████████                     ███████▒▒▒██████████████▒
//   █████████████████████████████             ███████▒▒▒██████████████████▒
//   ██████████████████████████████████████████████████████████████████████
//    ██████████████████████████████████████████████████████████████████▒
//      ▒█████████████████▒▒▒▒▒▒▒██████████████████████████████████▒▒▒
//
#include "bits/stdc++.h"
using namespace std;
#define MOD 1000000007
//#define MOD 998244353
const double EPS = 1e-9;
#define INF (1LL<<60)
#define D double
#define fs first
#define sc second
#define int long long
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define RFOR(i,a,b) for(int i = (b-1);i>=(a);--i)
#define REP(i,n)  FOR(i,0,(n))
#define RREP(i,n) RFOR(i,0,(n))
#define ITR(itr,mp) for(auto itr = (mp).begin(); itr != (mp).end(); ++itr)
#define RITR(itr,mp) for(auto itr = (mp).rbegin(); itr != (mp).rend(); ++itr)
#define range(i,a,b) ((a)<=(i) && (i)<(b))
#define debug(x)  cout << #x << " = " << (x) << endl;
#define SP << " " << 
typedef pair<int,int> P;
typedef vector<int> vec;
typedef vector<vector<int>> mat;

template <typename T>
struct SegmentTree{
  using F = function<T(T,T)>;
  int n;
  F f;
  T ti;
  vector<T> dat;
  SegmentTree(){};
  SegmentTree(F f,T ti):f(f),ti(ti){}
  void init(int n_){
    n=1;
    while(n<n_) n<<=1;
    dat.assign(n<<1,ti);
  }
  void build(const vector<T> &v){
    int n_=v.size();
    init(n_);
    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)|0],dat[(i<<1)|1]);
  }
  void update(int k,T x){
    dat[k+=n]=x;
    while(k>>=1)
      dat[k]=f(dat[(k<<1)|0],dat[(k<<1)|1]);
  }
  void add(int k,T x){
    dat[k+=n]+=x;
    while(k>>=1)
      dat[k]=f(dat[(k<<1)|0],dat[(k<<1)|1]);
  }
  T query(int a,int b){
    T vl=ti,vr=ti;
    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);
  }
  template<typename C>
  int find(int a, int b, C &check, T x, int k=1, int l=0, int r=-1){
    if(r<0)r=n;
    if(!check(f(x,dat[k]))||r<=a||b<=l)return -1;
    if(r-l==1)return l;
    int xl = find(a,b,check,x,(k<<1),l,(l+r)/2);
    if(xl>=0)return xl;
    x = f(x,dat[(k<<1)]);
    return find(a,b,check,x,(k<<1)|1,(l+r)/2,r);
  }
  template<typename C>
  int find(int a, int b, C &check){
    T x=ti;
    return find(a,b,check,x);
  }
};

signed main(){
  ios::sync_with_stdio(false);
  cin.tie(0);

  int n,q;
  cin >> n >> q;

  vector<P> a(n),b(n);
  REP(i,n) cin >> a[i].fs, a[i].fs--, a[i].sc = i;
  
  sort(a.rbegin(),a.rend());

  set<int> st; st.insert(-1);
  REP(i,n){
    auto itr = st.upper_bound(a[i].sc); itr--;
    //debug(*itr);
    b[i] = P(*itr+1,a[i].sc);
    st.insert(a[i].sc);
  }

  sort(b.begin(),b.end());

  auto f = [](int a,int b){return a+b;};
  SegmentTree<int> seg(f,0);
  seg.build(vec(n,0));

  using T = tuple<int,int,int>;
  vector<T> c(q);
  REP(i,q){
    int p,l,r;
    cin >> p >> l >> r;
    c[i] = T(l-1,r,i);
  }

  sort(c.begin(),c.end());

  vec ans(q); int id = 0;
  REP(i,q){
    int l,r,in;
    tie(l,r,in) = c[i];
    while(id<n&&b[id].fs<=l){
      seg.update(b[id].sc,1);
      id++;
    }
    ans[in] = seg.query(l,r);
  }

  REP(i,q) cout << ans[i] << "\n";

  return 0;
}
0