結果

問題 No.1099 Range Square Sum
ユーザー koyoprokoyopro
提出日時 2020-06-26 22:59:19
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 910 ms / 2,000 ms
コード長 3,503 bytes
コンパイル時間 1,386 ms
コンパイル使用メモリ 149,448 KB
実行使用メモリ 4,632 KB
最終ジャッジ日時 2023-09-18 07:04:26
合計ジャッジ時間 9,757 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 3 ms
4,380 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 2 ms
4,380 KB
testcase_15 AC 2 ms
4,376 KB
testcase_16 AC 3 ms
4,376 KB
testcase_17 AC 2 ms
4,376 KB
testcase_18 AC 2 ms
4,376 KB
testcase_19 AC 3 ms
4,380 KB
testcase_20 AC 2 ms
4,380 KB
testcase_21 AC 385 ms
4,528 KB
testcase_22 AC 386 ms
4,584 KB
testcase_23 AC 387 ms
4,564 KB
testcase_24 AC 385 ms
4,536 KB
testcase_25 AC 385 ms
4,536 KB
testcase_26 AC 904 ms
4,580 KB
testcase_27 AC 904 ms
4,632 KB
testcase_28 AC 910 ms
4,536 KB
testcase_29 AC 904 ms
4,488 KB
testcase_30 AC 904 ms
4,628 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include "bits/stdc++.h"
using namespace std;
#define int long long
#define FOR(i, a, b) for(int i=(a);i<(b);i++)
#define RFOR(i, a, b) for(int i=(b-1);i>=(a);i--)
#define REP(i, n) for(int i=0; i<(n); i++)
#define RREP(i, n) for(int i=(n-1); i>=0; i--)
#define REP1(i, n) for(int i=1; i<=(n); i++)
#define RREP1(i, n) for(int i=(n); i>=1; i--)
#define ALL(a) (a).begin(),(a).end()
#define UNIQUE_SORT(l) sort(ALL(l)); l.erase(unique(ALL(l)), l.end());
#define CONTAIN(a, b) find(ALL(a), (b)) != (a).end()
#define out(...) printf(__VA_ARGS__)
#if DEBUG
#define debug(...) printf(__VA_ARGS__)
#else
#define debug(...) /* ... */
#endif
template<class T>bool chmax(T &a,const T &b){if(a<b){a=b;return true;}return false;}
template<class T>bool chmin(T &a,const T &b){if(b<a){a=b;return true;}return false;}

void solve();
signed main()
{
#if DEBUG
    std::ifstream in("input.txt");
    std::cin.rdbuf(in.rdbuf());
#endif
    cin.tie(0);
    ios::sync_with_stdio(false);
    solve();
    return 0;
}

/*================================*/
#if DEBUG
#define SIZE 100
#else
#define SIZE 123450
#endif

int N,M,Q;

/********************************
 * 平方分割
 *******************************/
class SqrtDecomposition {
public:
    vector<int> A; // 全体に足される分
    vector<int> S; // Tの合計
    vector<int> S2; // T^2の合計
    vector<int> T; // 多い方
    int K;
    
    SqrtDecomposition(int size) {
        K = sqrt(size);
        T = vector<int>(size, 0);
        S = vector<int>(size/K+1, 0);
        S2 = vector<int>(size/K+1, 0);
        A = vector<int>(size/K+1, 0);
    }
    
    void udpate() {
        REP(i,T.size()) {
            int lk = i/K;
            S2[lk] += T[i]*T[i];
            S[lk] += T[i];
        }
    }
    
    void add(int l, int r, int w) {
        while(l < r) {
            int lk = l / K;
            int rk = r / K;
            if (lk == rk) {
                FOR(j, l, r) {
                    S2[lk] -= T[j]*T[j];
                    S[lk] += w;
                    T[j] += w;
                    S2[lk] += T[j]*T[j];
                }
            }
            else if (l % K == 0) {
                A[lk] += w;
            }
            else {
                FOR(j, l, (lk+1)*K) {
                    S2[lk] -= T[j]*T[j];
                    S[lk] += w;
                    T[j] += w;
                    S2[lk] += T[j]*T[j];
                }
            }
            l = (lk+1)*K;
        }
    }
    
    int query(int l, int r) {
        int ret = 0;
        while(l < r) {
            int lk = l / K;
            int rk = r / K;
            if (lk == rk) {
                FOR(j, l, r) {
                    int c = A[lk] + T[j];
                    ret += c*c;
                }
            }
            else if (l % K == 0) {
                int add = K*A[lk]*A[lk] + 2*A[lk]*S[lk]+S2[lk];
                ret += add;
            }
            else {
                FOR(j, l, (lk+1)*K) {
                    int c = A[lk] + T[j];
                    ret += c*c;
                }
            }
            l = (lk+1)*K;
        }
        return ret;
    }
};


void solve() {
    cin>>N;
    int a;
    auto sd = SqrtDecomposition(N);
    REP(i,N) {
        cin>>a;
        sd.T[i]=a;
    }
    sd.udpate();
    cin>>Q;
    int c,l,r,x;
    while(Q--) {
        cin>>c>>l>>r;
        if (c==1) {
            cin>>x;
            sd.add(--l, r, x);
        } else {
            out("%lld\n", sd.query(--l, r));
        }
    }
}

0