結果

問題 No.3078 Difference Sum Query
ユーザー otoshigo
提出日時 2025-03-28 22:16:09
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 95 ms / 2,000 ms
コード長 1,234 bytes
コンパイル時間 2,425 ms
コンパイル使用メモリ 209,336 KB
実行使用メモリ 12,848 KB
最終ジャッジ日時 2025-03-28 22:16:16
合計ジャッジ時間 6,653 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 26
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
using ll=long long;
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
template<class T> bool chmax(T &a, T b){if (a < b){a = b;return true;} else return false;}
template<class T> bool chmin(T &a, T b){if (a > b){a = b;return true;} else return false;}

#include<atcoder/fenwicktree>

void Solve(){
    int N,Q;
    cin>>N>>Q;
    vector<ll>A(N);
    for(int i=0;i<N;i++)cin>>A[i];
    vector<tuple<ll,int,int,int>>V;
    for(int i=0;i<Q;i++){
        int l,r;
        ll x;
        cin>>l>>r>>x;
        l--;
        r--;
        V.push_back(make_tuple(x,l,r,i));
    }
    for(int i=0;i<N;i++)V.push_back(make_tuple(A[i],-1,-1,i));
    sort(all(V));
    vector<ll>S(N+1);
    for(int i=0;i<N;i++)S[i+1]=S[i]+A[i];
    atcoder::fenwick_tree<ll>fw1(N),fw2(N);
    vector<ll>ans(Q);
    for(auto[x,l,r,q]:V){
        if(l==-1){
            fw1.add(q,x);
            fw2.add(q,1);
        }else{
            ll sum=S[r+1]-S[l];
            ll a=fw1.sum(l,r+1),b=sum-a,c=fw2.sum(l,r+1),d=r+1-l-c;
            ans[q]=b-d*x+c*x-a;
        }
    }
    for(int i=0;i<Q;i++)cout<<ans[i]<<"\n";
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    Solve();
}
0