結果

問題 No.1099 Range Square Sum
ユーザー carrot46carrot46
提出日時 2020-06-26 22:45:17
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 439 ms / 2,000 ms
コード長 3,112 bytes
コンパイル時間 1,577 ms
コンパイル使用メモリ 169,132 KB
実行使用メモリ 4,912 KB
最終ジャッジ日時 2023-09-18 06:28:59
合計ジャッジ時間 7,663 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
4,612 KB
testcase_01 AC 3 ms
4,724 KB
testcase_02 AC 3 ms
4,624 KB
testcase_03 AC 3 ms
4,604 KB
testcase_04 AC 3 ms
4,632 KB
testcase_05 AC 3 ms
4,872 KB
testcase_06 AC 2 ms
4,580 KB
testcase_07 AC 3 ms
4,912 KB
testcase_08 AC 2 ms
4,616 KB
testcase_09 AC 3 ms
4,628 KB
testcase_10 AC 2 ms
4,688 KB
testcase_11 AC 6 ms
4,628 KB
testcase_12 AC 6 ms
4,868 KB
testcase_13 AC 6 ms
4,684 KB
testcase_14 AC 6 ms
4,664 KB
testcase_15 AC 6 ms
4,552 KB
testcase_16 AC 6 ms
4,716 KB
testcase_17 AC 6 ms
4,608 KB
testcase_18 AC 6 ms
4,752 KB
testcase_19 AC 6 ms
4,752 KB
testcase_20 AC 6 ms
4,624 KB
testcase_21 AC 426 ms
4,620 KB
testcase_22 AC 422 ms
4,560 KB
testcase_23 AC 425 ms
4,872 KB
testcase_24 AC 421 ms
4,624 KB
testcase_25 AC 422 ms
4,608 KB
testcase_26 AC 435 ms
4,744 KB
testcase_27 AC 434 ms
4,628 KB
testcase_28 AC 438 ms
4,604 KB
testcase_29 AC 434 ms
4,564 KB
testcase_30 AC 439 ms
4,560 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define reps(i,s,n) for(int i = s; i < n; i++)
#define rep(i,n) reps(i,0,n)
#define Rreps(i,n,e) for(int i = n - 1; i >= e; --i)
#define Rrep(i,n) Rreps(i,n,0)
#define ALL(a) a.begin(), a.end()
#define fi first
#define se second
typedef long long ll;
typedef vector<ll> vec;
typedef vector<vec> mat;

ll N,M,H,W,Q,K,A,B;
string S;
const ll MOD = 998244353;
//const ll MOD = (1e+9) + 7;
typedef pair<ll, ll> P;
const ll INF = (1LL<<58);

int main() {
    const int bnum = 450, bsize = 450, max_n = bnum * bsize;
    cin >> N;
    vec a(max_n, 0);
    rep(i,N) cin>>a[i];
    vec as(bnum, 0), as2(bnum, 0), tx(bnum, 0);
    rep(i,N){
        as[i / bsize] += a[i];
        as2[i / bsize] += a[i] * a[i];
    }
    cin>>Q;
    rep(_,Q){
        int q, l, r, x;
        cin>>q>>l>>r;
        --l; --r;
        int lb = l / bsize, rb = r / bsize;
        if(q == 1){
            cin>>x;
            if(lb < rb) {
                reps(i, lb * bsize, (lb + 1) * bsize) {
                    int temp = tx[lb] + (i >= l ? x : 0);
                    as2[lb] += (a[i] + temp) * (a[i] + temp) - a[i] * a[i];
                    as[lb] += temp;
                    a[i] += temp;
                }
                reps(i, rb * bsize, (rb + 1) * bsize) {
                    int temp = tx[rb] + (i <= r ? x : 0);
                    as2[rb] += (a[i] + temp) * (a[i] + temp) - a[i] * a[i];
                    as[rb] += temp;
                    a[i] += temp;
                }
                tx[lb] = tx[rb] = 0;
                reps(i, lb + 1, rb) tx[i] += x;
            }else{
                reps(i, lb * bsize, (lb + 1) * bsize) {
                    int temp = tx[lb] + (i >= l && i <= r ? x : 0);
                    as2[lb] += (a[i] + temp) * (a[i] + temp) - a[i] * a[i];
                    as[lb] += temp;
                    a[i] += temp;
                }
                tx[lb] = 0;
            }
        }else{
            ll res = 0;
            if(lb < rb) {
                as[lb] += tx[lb] * bsize;
                as[rb] += tx[rb] * bsize;
                reps(i, lb * bsize, (lb + 1) * bsize) {
                    as2[lb] += (a[i] + tx[lb]) * (a[i] + tx[lb]) - a[i] * a[i];
                    a[i] += tx[lb];
                    if (i >= l) res += a[i] * a[i];
                }
                reps(i, rb * bsize, (rb + 1) * bsize) {
                    as2[rb] += (a[i] + tx[rb]) * (a[i] + tx[rb]) - a[i] * a[i];
                    a[i] += tx[rb];
                    if (i <= r) res += a[i] * a[i];
                }
                tx[lb] = tx[rb] = 0;
                reps(i, lb + 1, rb) res += as2[i] + 2LL * as[i] * tx[i] + tx[i] * tx[i] * bsize;
            }else{
                as[lb] += tx[lb] * bsize;
                reps(i, lb * bsize, (lb + 1) * bsize) {
                    as2[lb] += (a[i] + tx[lb]) * (a[i] + tx[lb]) - a[i] * a[i];
                    a[i] += tx[lb];
                    if(i >= l && i <= r) res += a[i] * a[i];
                }
                tx[lb] = 0;
            }
            cout<<res<<endl;
        }
    }
}
0