結果

問題 No.877 Range ReLU Query
ユーザー goodbatongoodbaton
提出日時 2019-09-06 21:59:55
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 102 ms / 2,000 ms
コード長 2,475 bytes
コンパイル時間 1,165 ms
コンパイル使用メモリ 107,768 KB
実行使用メモリ 9,900 KB
最終ジャッジ日時 2023-08-08 04:16:46
合計ジャッジ時間 3,940 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,620 KB
testcase_01 AC 2 ms
5,628 KB
testcase_02 AC 2 ms
5,496 KB
testcase_03 AC 2 ms
5,492 KB
testcase_04 AC 2 ms
5,828 KB
testcase_05 AC 2 ms
5,828 KB
testcase_06 AC 2 ms
5,504 KB
testcase_07 AC 2 ms
5,620 KB
testcase_08 AC 3 ms
5,636 KB
testcase_09 AC 2 ms
5,604 KB
testcase_10 AC 2 ms
5,628 KB
testcase_11 AC 95 ms
9,032 KB
testcase_12 AC 85 ms
8,724 KB
testcase_13 AC 68 ms
8,516 KB
testcase_14 AC 71 ms
8,136 KB
testcase_15 AC 102 ms
9,412 KB
testcase_16 AC 98 ms
9,404 KB
testcase_17 AC 100 ms
9,880 KB
testcase_18 AC 99 ms
9,692 KB
testcase_19 AC 86 ms
9,900 KB
testcase_20 AC 99 ms
9,664 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);
  }
};



int main(){
  int N, Q, a[SIZE];
  int l[SIZE], r[SIZE], x[SIZE];
  vector<pair<int,int> > query;
  priority_queue<pair<int,int> > pq;

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

  BIT<ll> bit1(N), bit2(N);

  for(int i=0; i<N; i++) {
    scanf("%d", a+i);
    pq.push({-a[i], i});
    bit1.add(i, 1);
    bit2.add(i, a[i]);
  }

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

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

  ll ans[SIZE];

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

    while(pq.size() && -pq.top().first <= query[i].first) {
      //debug(pq.top().first);
      bit1.add(pq.top().second, -1);
      bit2.add(pq.top().second, pq.top().first);
      pq.pop();
    }

    ll s = bit1.query(l[idx], r[idx]);
    ll t = bit2.query(l[idx], r[idx]);

    ans[idx] = t - s * query[i].first;
  }

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

  return 0;
}
0