結果

問題 No.878 Range High-Element Query
ユーザー goodbatongoodbaton
提出日時 2019-09-06 22:23:56
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 77 ms / 2,000 ms
コード長 2,558 bytes
コンパイル時間 2,382 ms
コンパイル使用メモリ 108,624 KB
実行使用メモリ 13,068 KB
最終ジャッジ日時 2023-09-07 01:01:39
合計ジャッジ時間 3,298 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
8,512 KB
testcase_01 AC 5 ms
10,532 KB
testcase_02 AC 5 ms
10,288 KB
testcase_03 AC 5 ms
10,320 KB
testcase_04 AC 4 ms
8,232 KB
testcase_05 AC 5 ms
8,296 KB
testcase_06 AC 4 ms
8,368 KB
testcase_07 AC 4 ms
8,192 KB
testcase_08 AC 5 ms
8,388 KB
testcase_09 AC 5 ms
8,240 KB
testcase_10 AC 5 ms
10,212 KB
testcase_11 AC 72 ms
11,504 KB
testcase_12 AC 52 ms
11,244 KB
testcase_13 AC 59 ms
12,164 KB
testcase_14 AC 45 ms
10,368 KB
testcase_15 AC 52 ms
12,400 KB
testcase_16 AC 71 ms
11,832 KB
testcase_17 AC 77 ms
13,068 KB
testcase_18 AC 76 ms
12,148 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>

#include <iostream>
#include <complex>
#include <string>
#include <algorithm>
#include <numeric>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>

#include <functional>
#include <cassert>

typedef long long ll;
using namespace std;

#ifndef LOCAL
#define debug(x) ;
#else
#define debug(x) cerr << __LINE__ << " : " << #x << " = " << (x) << endl;

template <typename T1, typename T2>
ostream &operator<<(ostream &out, const pair<T1, T2> &p) {
  out << "{" << p.first << ", " << p.second << "}";
  return out;
}

template <typename T>
ostream &operator<<(ostream &out, const vector<T> &v) {
  out << '{';
  for (const T &item : v) out << item << ", ";
  out << "\b\b}";
  return out;
}
#endif

#define mod 1000000007 //1e9+7(prime number)
#define INF 1000000000 //1e9
#define LLINF 2000000000000000000LL //2e18
#define SIZE 200010

/* Binary Indexed Tree(0-index) */

template <typename Type = int>
struct BIT{
  int bit_size;
  vector<Type> data;

  BIT(int n){
    bit_size = n;
    data.assign(n+2, 0);
  }

  //add x to kth value
  void add(int k, Type x){
    k++;
    while(k <= bit_size){
      data[k] += x;
      k += k & (-k);
    }
  }

  //sum of [0,k)
  Type query(int k){
    Type rec = 0;
    while(k > 0){
      rec += data[k];
      k -= k & (-k);
    }
    return rec;
  }

  //sum of [a, b)
  Type query(int a, int b){
    return query(b) - query(a);
  }
};



vector<int> vec[SIZE];

int main(){
  int N, Q;
  int a[SIZE];
  int l[SIZE], r[SIZE];

  scanf("%d%d", &N, &Q);

  for(int i=0; i<N; i++) {
    scanf("%d", a+i);
  }

  vector<pair<int,int> > query;

  for(int i=0; i<Q; i++) {
    scanf("%*d%d%d", l+i, r+i);
    l[i]--;
    query.push_back({l[i], i});
  }

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

  priority_queue<pair<int,int> > pq;
  for(int i=N-1; i>=0; i--) {
    pq.push({-a[i], i});

    while(pq.size() && a[i] > -pq.top().first) {
      vec[i].push_back(pq.top().second);
      debug(i);
      debug(pq.top().second);
      pq.pop();
    }
  }

  BIT<int> bit(N);
  int ans[SIZE];

  while(pq.size()) {
    int p = pq.top().second;
    bit.add(p, 1);
    pq.pop();
  }

  int t = 0;

  for(int i=0; i<Q; i++) {
    int idx = query[i].second;

    while(t < l[idx]) {
      for (int p : vec[t]) {
        bit.add(p, 1);
      }
      t++;
    }

    ans[idx] = bit.query(l[idx], r[idx]);
  }

  for(int i=0; i<Q; i++) {
    printf("%d\n", ans[i]);
  }

  return 0;
}
0