結果

問題 No.2065 Sum of Min
ユーザー umezoumezo
提出日時 2022-09-04 02:55:27
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 220 ms / 2,000 ms
コード長 1,357 bytes
コンパイル時間 2,301 ms
コンパイル使用メモリ 211,592 KB
実行使用メモリ 12,032 KB
最終ジャッジ日時 2024-04-28 22:35:45
合計ジャッジ時間 8,915 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 3 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 191 ms
11,904 KB
testcase_05 AC 193 ms
11,904 KB
testcase_06 AC 199 ms
11,904 KB
testcase_07 AC 188 ms
11,776 KB
testcase_08 AC 213 ms
11,904 KB
testcase_09 AC 213 ms
11,904 KB
testcase_10 AC 207 ms
11,904 KB
testcase_11 AC 205 ms
11,904 KB
testcase_12 AC 215 ms
11,904 KB
testcase_13 AC 216 ms
12,032 KB
testcase_14 AC 219 ms
11,904 KB
testcase_15 AC 219 ms
11,904 KB
testcase_16 AC 219 ms
11,776 KB
testcase_17 AC 220 ms
11,904 KB
testcase_18 AC 219 ms
11,904 KB
testcase_19 AC 218 ms
11,776 KB
testcase_20 AC 217 ms
11,904 KB
testcase_21 AC 215 ms
11,776 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;

template<typename T>
struct BIT{
private:
  vector<T> A;
  const int n;
  
public:
  BIT(int _n) : A(_n+1,0), n(_n){}
  
  T sum(int i){
    T s=0;
    while(i>0){
      s+=A[i];
      i-=i&-i;
    }
    return s;
  }
  
  T sum(int i,int j){
    return sum(j)-sum(i-1);
  }
  
  void add(int i,T x){
    while(i<=n){
      A[i]+=x;
      i+=i&-i;
    }
  }

  void update(int i,T x){
    T tmp=sum(i,i);
    if(tmp!=x) add(i,x-tmp);
  }
};

int main(){
  ios::sync_with_stdio(false);
  std::cin.tie(nullptr);

  int n,q;
  cin>>n>>q;
  vector<ll> A(n+1);
  vector<pair<ll,ll>> C(n);
  rep(i,n){
    cin>>A[i+1];
    C[i]={A[i+1],i+1};
  }
  sort(ALL(C));
  vector<ll> L(q),R(q),X(q);
  rep(i,q) cin>>L[i]>>R[i]>>X[i];
  vector<pair<ll,ll>> B(q);
  rep(i,q) B[i]={X[i],i};
  sort(ALL(B));
  reverse(ALL(B));
  
  BIT<ll> bit(n),bit1(n);
  for(int i=1;i<=n;i++) bit.add(i,A[i]);
  
  vector<ll> ANS(q);
  int now=n-1;
  rep(i,q){
    ll nx=B[i].first,ni=B[i].second;
    while(now>=0 && C[now].first>=nx){
      bit.update(C[now].second,0);
      bit1.update(C[now].second,1);
      now--; 
    }
    ANS[ni]=bit.sum(L[ni],R[ni])+X[ni]*bit1.sum(L[ni],R[ni]);
  }
  rep(i,q) cout<<ANS[i]<<endl;
  
  return 0;
}
0