結果

問題 No.2065 Sum of Min
ユーザー Nzt3Nzt3
提出日時 2022-09-02 21:50:32
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 86 ms / 2,000 ms
コード長 1,074 bytes
コンパイル時間 2,448 ms
コンパイル使用メモリ 214,392 KB
実行使用メモリ 7,896 KB
最終ジャッジ日時 2023-08-10 03:48:59
合計ジャッジ時間 7,007 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 63 ms
7,608 KB
testcase_05 AC 64 ms
7,628 KB
testcase_06 AC 74 ms
7,588 KB
testcase_07 AC 69 ms
7,620 KB
testcase_08 AC 86 ms
7,840 KB
testcase_09 AC 82 ms
7,552 KB
testcase_10 AC 79 ms
7,628 KB
testcase_11 AC 77 ms
7,636 KB
testcase_12 AC 84 ms
7,624 KB
testcase_13 AC 84 ms
7,620 KB
testcase_14 AC 83 ms
7,632 KB
testcase_15 AC 83 ms
7,620 KB
testcase_16 AC 86 ms
7,720 KB
testcase_17 AC 84 ms
7,896 KB
testcase_18 AC 86 ms
7,680 KB
testcase_19 AC 85 ms
7,544 KB
testcase_20 AC 86 ms
7,536 KB
testcase_21 AC 85 ms
7,632 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;

void add(int n,int v,vector<long long>& BIT){
  for(;n<BIT.size();n+=n&-n)BIT[n]+=v;
}
long long getsum(int l,int r,const vector<long long>& BIT){
  long long ret=0;
  for(--l;l>0;l-=l&-l)ret-=BIT[l];
  for(;r>0;r-=r&-r)ret+=BIT[r];
  return ret;
}

int main(){
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int N,Q;
  cin>>N>>Q;
  vector<array<int,2>>A(N);
  for(int i=0;i<N;i++){
    cin>>A[i][0];
    A[i][1]=i+1;
  }
  sort(A.begin(),A.end());
  A.push_back({(int)1e9+7,-1});
  vector<long long>cnt(N+2),sum(N+2);
  vector<array<int,4>>task(Q);
  for(int i=0;i<Q;i++){
    cin>>task[i][1]>>task[i][2]>>task[i][0];
    task[i][3]=i;
  }
  sort(task.begin(),task.end());
  vector<long long>ans(Q);
  for(int i=0;i<N;i++){
    add(i+1,1,cnt);
  }
  for(int i=0,t=0;i<Q;i++){
    while(task[i][0]>=A[t][0]){
      add(A[t][1],-1,cnt);
      add(A[t][1],A[t][0],sum);
      ++t;
    }
    ans[task[i][3]]=getsum(task[i][1],task[i][2],sum)+task[i][0]*getsum(task[i][1],task[i][2],cnt);
  }
  for(auto i:ans)cout<<i<<'\n';
}
0