結果

問題 No.1099 Range Square Sum
ユーザー fastmathfastmath
提出日時 2020-06-26 21:55:39
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 216 ms / 2,000 ms
コード長 2,382 bytes
コンパイル時間 1,448 ms
コンパイル使用メモリ 168,184 KB
実行使用メモリ 21,120 KB
最終ジャッジ日時 2023-09-18 04:21:46
合計ジャッジ時間 5,266 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,412 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,540 KB
testcase_03 AC 2 ms
5,428 KB
testcase_04 AC 2 ms
5,372 KB
testcase_05 AC 2 ms
5,488 KB
testcase_06 AC 2 ms
5,380 KB
testcase_07 AC 2 ms
5,700 KB
testcase_08 AC 2 ms
5,644 KB
testcase_09 AC 2 ms
5,412 KB
testcase_10 AC 2 ms
5,492 KB
testcase_11 AC 3 ms
5,520 KB
testcase_12 AC 3 ms
5,612 KB
testcase_13 AC 3 ms
5,460 KB
testcase_14 AC 3 ms
5,436 KB
testcase_15 AC 3 ms
5,612 KB
testcase_16 AC 3 ms
5,428 KB
testcase_17 AC 3 ms
5,436 KB
testcase_18 AC 3 ms
5,612 KB
testcase_19 AC 3 ms
5,616 KB
testcase_20 AC 3 ms
5,508 KB
testcase_21 AC 212 ms
20,864 KB
testcase_22 AC 216 ms
21,072 KB
testcase_23 AC 214 ms
21,120 KB
testcase_24 AC 215 ms
20,912 KB
testcase_25 AC 216 ms
20,912 KB
testcase_26 AC 177 ms
21,064 KB
testcase_27 AC 178 ms
20,820 KB
testcase_28 AC 178 ms
20,868 KB
testcase_29 AC 177 ms
20,912 KB
testcase_30 AC 180 ms
20,868 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
#define int long long
#define ii pair <int, int>
#define app push_back
#define all(a) a.begin(), a.end()
#define bp __builtin_popcountll
#define ll long long
#define mp make_pair
#define f first
#define s second
#define Time (double)clock()/CLOCKS_PER_SEC

const int N = 2e5+7;
int sum[N << 2], sum2[N << 2], prop[N << 2];

void gap(int v, int x, int tl, int tr) {

    if (tl == 4 && tr == 4) {
        //cout << sum[v] << ' ' << sum2[v] << ' ' << x << endl;
    }   

    int len = tr - tl + 1;
    sum2[v] += x * x * len;
    sum2[v] += 2 * x * sum[v];

    sum[v] += x * len;

    prop[v] += x;

    if (tl == 4 && tr == 4) {
        //cout << "res " << sum[v] << ' ' << sum2[v] << endl;
    }   
}

void push(int v, int tl, int tr) {
    int tm = (tl + tr)>>1;
    gap(v * 2 + 1, prop[v], tl, tm);
    gap(v * 2 + 2, prop[v], tm+1, tr);
    prop[v] = 0;
}   

void relax(int v) {
    sum[v] = sum[v * 2 + 1] + sum[v * 2 + 2];
    sum2[v] = sum2[v * 2 + 1] + sum2[v * 2 + 2];
}   

void upd(int v, int tl, int tr, int l, int r, int x) {
    if (tr < l || r < tl)
        return;
    if (l <= tl && tr <= r) {
        gap(v, x, tl, tr);

        //cout << x << " : " << tl << ' ' << tr << ' ' << sum[v] << ' ' << sum2[v] << endl;

        return;
    }   
    int tm = (tl + tr) >> 1;
    push(v, tl, tr);
    upd(v * 2 + 1, tl, tm, l, r, x);
    upd(v * 2 + 2, tm + 1, tr, l, r, x);
    relax(v);
}   
int get(int v, int tl, int tr, int l, int r) {
    if (tr < l || r < tl)
        return 0;
    if (l <= tl && tr <= r) {
        //cout << tl << ' ' << tr << " : " << sum2[v] << endl;
        return sum2[v];
    }
    int tm = (tl + tr) >> 1;
    push(v, tl, tr);
    return get(v * 2 + 1, tl, tm, l, r) + get(v * 2 + 2, tm + 1, tr, l, r);    
}   

signed main() {
    #ifdef HOME
    freopen("input.txt", "r", stdin);
    #else
    #define endl '\n'
    ios_base::sync_with_stdio(0); cin.tie(0);
    #endif

    int n;
    cin >> n;
    vector <int> a(n+1);
    for (int i = 1; i <= n; ++i) {
        cin >> a[i];
        upd(0, 1, n, i, i, a[i]);
    }

    int q;
    cin >> q;
    while (q--) {
        int t, l, r;
        cin >> t >> l >> r;
        if (t == 2) {
            cout << get(0, 1,n, l, r) << endl;
        }   
        else {
            int x; cin >> x;
            upd(0, 1, n, l, r, x);
        }   
    }

}
0