結果

問題 No.1099 Range Square Sum
ユーザー risujirohrisujiroh
提出日時 2020-06-26 21:33:16
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 128 ms / 2,000 ms
コード長 2,166 bytes
コンパイル時間 2,119 ms
コンパイル使用メモリ 205,004 KB
実行使用メモリ 14,200 KB
最終ジャッジ日時 2023-09-18 03:15:21
合計ジャッジ時間 6,433 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,384 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 3 ms
4,376 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 3 ms
4,376 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 3 ms
4,376 KB
testcase_16 AC 2 ms
4,376 KB
testcase_17 AC 2 ms
4,380 KB
testcase_18 AC 2 ms
4,380 KB
testcase_19 AC 2 ms
4,376 KB
testcase_20 AC 2 ms
4,380 KB
testcase_21 AC 126 ms
14,076 KB
testcase_22 AC 127 ms
14,200 KB
testcase_23 AC 128 ms
13,844 KB
testcase_24 AC 128 ms
13,920 KB
testcase_25 AC 127 ms
13,916 KB
testcase_26 AC 95 ms
14,196 KB
testcase_27 AC 94 ms
14,080 KB
testcase_28 AC 94 ms
13,716 KB
testcase_29 AC 94 ms
13,720 KB
testcase_30 AC 95 ms
13,720 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

#ifdef LOCAL
#include "debug.h"
#else
#define DEBUG(...)
#endif

template <class T, class F> struct lazy_segtree {
  const int n;
  vector<T> t;
  vector<F> d;
  lazy_segtree(int _n = 0) : n(_n), t(2 * n), d(n) {}
  T& operator[](int i) { return t[n + i]; }
  void build() { for (int i = n; i--; ) t[i] = t[2 * i] * t[2 * i + 1]; }
  void apply(int i, F f) {
    t[i] = f(t[i]);
    if (i < n) d[i] = d[i] * f;
  }
  void push(int i) {
    apply(2 * i, d[i]), apply(2 * i + 1, d[i]);
    d[i] = F();
  }
  void push_down(int i) {
    for (int h = __lg(i), tz = __builtin_ctz(i); h > tz; --h) push(i >> h);
  }
  void pull_up(int i) {
    while (i >>= 1) t[i] = t[2 * i] * t[2 * i + 1];
  }
  T fold(int l, int r) {
    push_down(l += n), push_down(r += n);
    T a, b;
    for (; l < r; l >>= 1, r >>= 1) {
      if (l & 1) a = a * t[l++];
      if (r & 1) b = t[--r] * b;
    }
    return a * b;
  }
  T get(int i) { return fold(i, i + 1); }
  void act(int l, int r, F f) {
    push_down(l += n), push_down(r += n);
    for (int i = l, j = r; i < j; i >>= 1, j >>= 1) {
      if (i & 1) apply(i++, f);
      if (j & 1) apply(--j, f);
    }
    pull_up(l >> __builtin_ctz(l)), pull_up(r >> __builtin_ctz(r));
  }
  void set(int i, T a) {
    push_down(i += n), push_down(i + 1);
    t[i] = a;
    pull_up(i);
  }
};

using ll = long long;
struct node {
  ll s0 = 0, s1 = 0, s2 = 0;
  node operator*(node b) const {
    return {s0 + b.s0, s1 + b.s1, s2 + b.s2};
  }
};
struct add {
  ll v = 0;
  add operator*(add g) const {
    return {v + g.v};
  }
  node operator()(node x) const {
    x.s2 += 2 * v * x.s1 + x.s0 * v * v;
    x.s1 += x.s0 * v;
    return x;
  }
};

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  lazy_segtree<node, add> seg(n);
  for (int i = 0; i < n; ++i) {
    ll a;
    cin >> a;
    seg[i] = {1, a, a * a};
  }
  seg.build();
  int q;
  cin >> q;
  while (q--) {
    int tp, l, r;
    cin >> tp >> l >> r;
    --l;
    if (tp == 1) {
      ll x;
      cin >> x;
      seg.act(l, r, {x});
    } else {
      cout << seg.fold(l, r).s2 << '\n';
    }
  }
}
0