結果

問題 No.877 Range ReLU Query
ユーザー umezoumezo
提出日時 2024-03-09 03:41:58
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 356 ms / 2,000 ms
コード長 1,451 bytes
コンパイル時間 3,890 ms
コンパイル使用メモリ 261,616 KB
実行使用メモリ 54,600 KB
最終ジャッジ日時 2024-03-09 03:42:08
合計ジャッジ時間 9,664 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,676 KB
testcase_01 AC 3 ms
6,676 KB
testcase_02 AC 2 ms
6,676 KB
testcase_03 AC 3 ms
6,676 KB
testcase_04 AC 2 ms
6,676 KB
testcase_05 AC 2 ms
6,676 KB
testcase_06 AC 3 ms
6,676 KB
testcase_07 AC 2 ms
6,676 KB
testcase_08 AC 3 ms
6,676 KB
testcase_09 AC 2 ms
6,676 KB
testcase_10 AC 2 ms
6,676 KB
testcase_11 AC 310 ms
48,036 KB
testcase_12 AC 301 ms
47,432 KB
testcase_13 AC 206 ms
32,052 KB
testcase_14 AC 207 ms
30,116 KB
testcase_15 AC 328 ms
54,080 KB
testcase_16 AC 356 ms
53,264 KB
testcase_17 AC 349 ms
53,952 KB
testcase_18 AC 333 ms
54,368 KB
testcase_19 AC 167 ms
54,600 KB
testcase_20 AC 287 ms
54,600 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define ALL(v) v.begin(),v.end()
typedef long long ll;
 
#include<bits/stdc++.h>
using namespace std;

struct mergesorttree{
  int n;
  vector<vector<ll>> a; //ソート列
  vector<vector<ll>> s; //累積和
  mergesorttree(int m){
    n=1;
    while(n<m) n<<=1;
    a.resize(2*n);
    s.resize(2*n,vector<ll>(1));
  }
  void set(int i,ll x){
    a[n+i].push_back(x);
    s[n+i].push_back(x);
  }
  void init(){
    for(int i=n-1;i>=1;i--){
      int l=i<<1,r=l|1;
      merge(ALL(a[l]),ALL(a[r]),back_inserter(a[i]));
      s[i]=vector<ll>(a[i].size()+1);
      rep(j,a[i].size()) s[i][j+1]=s[i][j]+a[i][j];
    }
  }
  pair<ll,ll> get(int i,ll x){
    auto l=lower_bound(ALL(a[i]),x)-a[i].begin();
    int sz=s[i].size();
    return {s[i][sz-1]-s[i][l],sz-1-l};
  }
  ll solve(int l,int r,ll x){
    l+=n,r+=n;
    ll sum=0,cnt=0;
    while(l<r){
      if(l&1){
        auto a=get(l++,x);
        sum+=a.first;
        cnt+=a.second;
      }
      if(r&1){
        auto a=get(--r,x);
        sum+=a.first;
        cnt+=a.second;
      }
      l>>=1,r>>=1;
    }
    return sum-cnt*x;
  }
};

int main(){
  ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
  
  int n,q;
  cin>>n>>q;
  vector<ll> A(n);
  rep(i,n) cin>>A[i];
  mergesorttree t(n);
  rep(i,n) t.set(i,A[i]);
  t.init();

  while(q--){
    ll c,l,r,x;
    cin>>c>>l>>r>>x;
    l--;
    cout<<t.solve(l,r,x)<<'\n';
  }
    
  return 0;
}
0