結果

問題 No.877 Range ReLU Query
ユーザー tjaketjake
提出日時 2019-09-07 09:56:24
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 690 ms / 2,000 ms
コード長 2,961 bytes
コンパイル時間 1,017 ms
コンパイル使用メモリ 90,124 KB
実行使用メモリ 54,336 KB
最終ジャッジ日時 2024-04-25 22:09:57
合計ジャッジ時間 8,488 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 7 ms
15,920 KB
testcase_01 AC 9 ms
16,056 KB
testcase_02 AC 9 ms
16,028 KB
testcase_03 AC 9 ms
15,912 KB
testcase_04 AC 7 ms
15,972 KB
testcase_05 AC 8 ms
15,864 KB
testcase_06 AC 8 ms
15,924 KB
testcase_07 AC 7 ms
16,020 KB
testcase_08 AC 10 ms
16,132 KB
testcase_09 AC 7 ms
15,944 KB
testcase_10 AC 8 ms
15,900 KB
testcase_11 AC 689 ms
47,348 KB
testcase_12 AC 572 ms
46,768 KB
testcase_13 AC 439 ms
37,920 KB
testcase_14 AC 454 ms
35,820 KB
testcase_15 AC 690 ms
53,496 KB
testcase_16 AC 661 ms
52,532 KB
testcase_17 AC 677 ms
53,488 KB
testcase_18 AC 671 ms
53,780 KB
testcase_19 AC 421 ms
54,284 KB
testcase_20 AC 577 ms
54,336 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<string>
#include<vector>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<algorithm>
#include<functional>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cassert>
#include<ctime>
using namespace std;

#define mind(a,b) (a>b?b:a)
#define maxd(a,b) (a>b?a:b)
#define absd(x) (x<0?-(x):x)
#define pow2(x) ((x)*(x))
#define rep(i,n) for(int i=0; i<n; ++i)
#define repr(i,n) for(int i=n-1; i>=0; --i)
#define repl(i,s,n) for(int i=s; i<=n; ++i)
#define replr(i,s,n) for(int i=n; i>=s; --i)
#define repf(i,s,n,j) for(int i=s; i<=n; i+=j)
#define repe(e,obj) for(auto e : obj)

#define SP << " " <<
#define COL << " : " <<
#define COM << ", " <<
#define ARR << " -> " <<
#define PNT(STR) cout << STR << endl
#define POS(X,Y) "(" << X << ", " << Y << ")"
#define DEB(A) " (" << #A << ") " << A
#define DEBREP(i,n,val) for(int i=0; i<n; ++i) cout << val << " "; cout << endl
#define ALL(V) (V).begin(), (V).end()
#define INF 1000000007
#define INFLL 1000000000000000007LL
#define EPS 1e-9

typedef unsigned int uint;
typedef unsigned long ulong;
typedef unsigned long long ull;
typedef long long ll;
typedef long double ld;
#define P_TYPE int
typedef pair<P_TYPE, P_TYPE> P;
typedef pair<P, P_TYPE> PI;
typedef pair<P_TYPE, P> IP;
typedef pair<P, P> PP;
typedef priority_queue<P, vector<P>, greater<P> > pvqueue;

#define N (1 << 17)

class MergeSortTree {
  vector<ll> data[2*N];
  vector<ll> sum[2*N];
  int n0, n;

  void build(int k, int l, int r, ll *a) {
    if(l+1 == r) {
      if(l < n) data[k].push_back(a[l]);
      sum[k].push_back(0);
      sum[k].push_back(a[l]);
      return;
    }

    int m = (l+r)/2;
    if(l < m) build(2*k+1, l, m, a);
    if(m < r) build(2*k+2, m, r, a);

    int sz = data[2*k+1].size() + data[2*k+2].size();
    data[k].resize(sz);
    sum[k].resize(sz+1);
    merge(data[2*k+1].begin(), data[2*k+1].end(), data[2*k+2].begin(), data[2*k+2].end(), data[k].begin());
    sum[k][0] = 0;
    for(int i=0; i<data[k].size(); ++i) {
      sum[k][i+1] = sum[k][i] + data[k][i];
    }
  }

  ll _query(ll x, int a, int b, int k, int l, int r) {
    if(b <= l || r <= a) {
      return 0;
    }
    if(a <= l && r <= b) {
      auto it = lower_bound(data[k].begin(), data[k].end(), x);
      int idx = (int)(it - data[k].begin());
      return sum[k][sum[k].size()-1] - sum[k][idx] - x * ((int) data[k].size() - idx);
    }
    int m = (l + r) / 2;
    ll lv = _query(x, a, b, 2*k+1, l, m);
    ll rv = _query(x, a, b, 2*k+2, m, r);
    return lv + rv;
  }
public:
  MergeSortTree(int n, ll *a) : n(n) {
    n0 = 1;
    while(n0 < n) n0 <<= 1;
    build(0, 0, n0, a);
  }

  ll query(int l, int r, ll x) {
    return _query(x, l, r, 0, 0, n0);
  }
};

ll a[N];

int main() {
  int n, q;
  cin >> n >> q;
  rep(i, n) cin >> a[i];
  MergeSortTree mst(n, a);
  while(q--) {
    int t, l, r, x;
    cin >> t >> l >> r >> x;
    cout << mst.query(l-1, r, x) << endl;
  }
  return 0;
}
0