結果

問題 No.1099 Range Square Sum
ユーザー chineristACchineristAC
提出日時 2020-06-27 01:04:30
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,170 ms / 2,000 ms
コード長 2,368 bytes
コンパイル時間 815 ms
コンパイル使用メモリ 102,384 KB
実行使用メモリ 6,556 KB
最終ジャッジ日時 2023-09-18 12:44:06
合計ジャッジ時間 11,352 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
4,412 KB
testcase_01 AC 3 ms
4,616 KB
testcase_02 AC 3 ms
6,512 KB
testcase_03 AC 3 ms
4,544 KB
testcase_04 AC 3 ms
4,460 KB
testcase_05 AC 3 ms
4,408 KB
testcase_06 AC 3 ms
6,520 KB
testcase_07 AC 3 ms
4,420 KB
testcase_08 AC 3 ms
6,468 KB
testcase_09 AC 3 ms
4,532 KB
testcase_10 AC 3 ms
4,620 KB
testcase_11 AC 4 ms
4,460 KB
testcase_12 AC 5 ms
4,508 KB
testcase_13 AC 4 ms
4,488 KB
testcase_14 AC 4 ms
4,408 KB
testcase_15 AC 4 ms
4,420 KB
testcase_16 AC 5 ms
4,468 KB
testcase_17 AC 5 ms
6,556 KB
testcase_18 AC 4 ms
4,420 KB
testcase_19 AC 4 ms
4,468 KB
testcase_20 AC 4 ms
4,616 KB
testcase_21 AC 525 ms
4,416 KB
testcase_22 AC 525 ms
4,508 KB
testcase_23 AC 523 ms
4,520 KB
testcase_24 AC 524 ms
4,404 KB
testcase_25 AC 529 ms
4,516 KB
testcase_26 AC 1,165 ms
4,408 KB
testcase_27 AC 1,169 ms
4,488 KB
testcase_28 AC 1,164 ms
4,620 KB
testcase_29 AC 1,163 ms
4,512 KB
testcase_30 AC 1,170 ms
4,628 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<vector>
#include<string>
#include<map>
#include<set>
#include<queue>
#include<algorithm>
#include<cmath>
#include<iomanip>
#include<random>
#include<stdio.h>
using namespace std;

typedef long long ll;

long long Mod(long long val, long long m) {
  long long res = val % m;
  if (res < 0) res += m;
  return res;
}



vector<ll> A(200000);
ll Adata[200000];
ll RSQdata[200000];
ll RSSQdata[200000];
ll n;

ll RSQ(ll l,ll r){
    ll id=l;
    ll L,q,R;
    ll res=0;
    while (r>=id){
        L=id;
        q=id/n;
        R=min((q+1)*n-1,r);
        if (R-L+1==n){
            res+=RSQdata[q];
        }
        else{
            for (ll i=L;i<R+1;i++){
                res+=A[i]+Adata[q];
            }
        }
        id=R+1;
    }
    return res;
}

void update(ll l,ll r,ll x){
    ll id=l;
    ll L,q,R;
    while (r>=id){
        L=id;
        q=id/n;
        R=min((q+1)*n-1,r);
        if (R-L+1==n){
            Adata[q]+=x;
            RSQdata[q]+=x*n;
            RSSQdata[q]+=2*x*RSQ(L,R)-n*x*x;
        }
        else{
            for (ll i=L;i<R+1;i++){
                A[i]+=x;
            }
            RSQdata[q]+=(R-L+1)*x;
            RSSQdata[q]+=2*x*RSQ(L,R)-(R-L+1)*x*x;
        }
        id=R+1;
    }
}

ll RSSQ(ll l,ll r){
    ll id=l;
    ll L,q,R;
    ll res=0;
    while (r>=id){
        L=id;
        q=id/n;
        R=min((q+1)*n-1,r);
        if (R-L+1==n){
            res+=RSSQdata[q];
        }
        else{
            for (ll i=L;i<R+1;i++){
                res+=(A[i]+Adata[q])*(A[i]+Adata[q]);
            }
        }
        id=R+1;
    }
    return res;
}

int main(){
    ll N;
    scanf("%lld",&N);
    for (ll i=0;i<N;i++){
        scanf("%lld",&A[i]);
    }
    while (N>=n*n){
        n+=1;
    }
    n-=1;

    for (ll i=0;i<N;i++){
        ll q=i/n;
        RSQdata[q]+=A[i];
        RSSQdata[q]+=A[i]*A[i];
    }

    ll Q;
    scanf("%lld",&Q);
    while (Q--){
        int q;
        scanf("%lld",&q);
        if (q==1){
            ll l,r,x;
            scanf("%lld",&l);
            scanf("%lld",&r);
            scanf("%lld",&x);
          	l-=1;r-=1;
            update(l,r,x);
        }
        else{
            ll l,r;
            scanf("%lld",&l);
            scanf("%lld",&r);
          	l-=1;r-=1;
            ll ans=RSSQ(l,r);
            cout<<ans<<endl;
        }
    }


}
0