結果

問題 No.1099 Range Square Sum
ユーザー stoqstoq
提出日時 2020-12-01 22:25:43
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 297 ms / 2,000 ms
コード長 5,273 bytes
コンパイル時間 2,836 ms
コンパイル使用メモリ 224,192 KB
実行使用メモリ 29,032 KB
最終ジャッジ日時 2024-09-13 03:12:44
合計ジャッジ時間 6,932 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 2 ms
6,944 KB
testcase_03 AC 2 ms
6,944 KB
testcase_04 AC 2 ms
6,944 KB
testcase_05 AC 2 ms
6,940 KB
testcase_06 AC 2 ms
6,944 KB
testcase_07 AC 2 ms
6,944 KB
testcase_08 AC 2 ms
6,940 KB
testcase_09 AC 2 ms
6,944 KB
testcase_10 AC 2 ms
6,944 KB
testcase_11 AC 3 ms
6,944 KB
testcase_12 AC 3 ms
6,944 KB
testcase_13 AC 3 ms
6,940 KB
testcase_14 AC 3 ms
6,944 KB
testcase_15 AC 3 ms
6,940 KB
testcase_16 AC 3 ms
6,940 KB
testcase_17 AC 3 ms
6,944 KB
testcase_18 AC 3 ms
6,944 KB
testcase_19 AC 3 ms
6,944 KB
testcase_20 AC 3 ms
6,940 KB
testcase_21 AC 293 ms
28,976 KB
testcase_22 AC 294 ms
28,860 KB
testcase_23 AC 297 ms
28,972 KB
testcase_24 AC 295 ms
29,032 KB
testcase_25 AC 297 ms
28,860 KB
testcase_26 AC 207 ms
28,900 KB
testcase_27 AC 206 ms
28,972 KB
testcase_28 AC 204 ms
28,960 KB
testcase_29 AC 205 ms
29,020 KB
testcase_30 AC 204 ms
28,972 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#define MOD_TYPE 1

#pragma region Macros

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

#if 0
#include <boost/multiprecision/cpp_int.hpp>
#include <boost/multiprecision/cpp_dec_float.hpp>
using Int = boost::multiprecision::cpp_int;
using lld = boost::multiprecision::cpp_dec_float_100;
#endif
#if 1
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#endif
using ll = long long int;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using pld = pair<ld, ld>;
template <typename Q_type>
using smaller_queue = priority_queue<Q_type, vector<Q_type>, greater<Q_type>>;

constexpr ll MOD = (MOD_TYPE == 1 ? (ll)(1e9 + 7) : 998244353);
constexpr int INF = (int)1e9 + 10;
constexpr ll LINF = (ll)4e18;
constexpr double PI = acos(-1.0);
constexpr double EPS = 1e-7;
constexpr int Dx[] = {0, 0, -1, 1, -1, 1, -1, 1, 0};
constexpr int Dy[] = {1, -1, 0, 0, -1, -1, 1, 1, 0};

#define REP(i, m, n) for (ll i = m; i < (ll)(n); ++i)
#define rep(i, n) REP(i, 0, n)
#define REPI(i, m, n) for (int i = m; i < (int)(n); ++i)
#define repi(i, n) REPI(i, 0, n)
#define MP make_pair
#define MT make_tuple
#define YES(n) cout << ((n) ? "YES" : "NO") << "\n"
#define Yes(n) cout << ((n) ? "Yes" : "No") << "\n"
#define possible(n) cout << ((n) ? "possible" : "impossible") << "\n"
#define Possible(n) cout << ((n) ? "Possible" : "Impossible") << "\n"
#define all(v) v.begin(), v.end()
#define NP(v) next_permutation(all(v))
#define dbg(x) cerr << #x << ":" << x << "\n";

struct io_init
{
  io_init()
  {
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << setprecision(30) << setiosflags(ios::fixed);
  };
} io_init;
template <typename T>
inline bool chmin(T &a, T b)
{
  if (a > b)
  {
    a = b;
    return true;
  }
  return false;
}
template <typename T>
inline bool chmax(T &a, T b)
{
  if (a < b)
  {
    a = b;
    return true;
  }
  return false;
}
inline ll CEIL(ll a, ll b)
{
  return (a + b - 1) / b;
}
template <typename A, size_t N, typename T>
inline void Fill(A (&array)[N], const T &val)
{
  fill((T *)array, (T *)(array + N), val);
}
template <typename T, typename U>
constexpr istream &operator>>(istream &is, pair<T, U> &p) noexcept
{
  is >> p.first >> p.second;
  return is;
}
template <typename T, typename U>
constexpr ostream &operator<<(ostream &os, pair<T, U> &p) noexcept
{
  os << p.first << " " << p.second;
  return os;
}
#pragma endregion

template <typename T, typename E>
struct SegmentTree
{
  using F = function<T(T, T)>;
  using G = function<T(T, E)>;
  using H = function<E(E, E)>;
  using P = function<E(E, int)>;
  int n;
  F f;
  G g;
  H h;
  P p;
  T ti;
  E ei;
  vector<T> dat;
  vector<E> laz;
  SegmentTree() {}
  SegmentTree(
      int n_, F f, G g, H h, T ti, E ei, P p = [](E a, int b) { return a; })
      : f(f), g(g), h(h), ti(ti), ei(ei), p(p)
  {
    init(n_);
  }
  SegmentTree(
      vector<T> &v, F f, G g, H h, T ti, E ei, P p = [](E a, int b) { return a; })
      : f(f), g(g), h(h), ti(ti), ei(ei), p(p)
  {
    init(v.size());
    build(v);
  }
  void init(int n_)
  {
    n = 1;
    while (n < n_)
      n *= 2;
    dat.clear();
    dat.resize(2 * n - 1, ti);
    laz.clear();
    laz.resize(2 * n - 1, ei);
  }
  void build(const vector<T> v)
  {
    for (int i = 0; i < v.size(); i++)
      dat[i + n - 1] = v[i];
    for (int i = n - 2; i >= 0; i--)
      dat[i] = f(dat[i * 2 + 1], dat[i * 2 + 2]);
  }
  void eval(int len, int k)
  {
    if (laz[k] == ei)
      return;
    if (k * 2 + 1 < n * 2 - 1)
    {
      laz[k * 2 + 1] = h(laz[k * 2 + 1], laz[k]);
      laz[k * 2 + 2] = h(laz[k * 2 + 2], laz[k]);
    }
    dat[k] = g(dat[k], p(laz[k], len));
    laz[k] = ei;
  }
  T update(int a, int b, E x, int k, int l, int r)
  {
    eval(r - l, k);
    if (r <= a || b <= l)
      return dat[k];
    if (a <= l && r <= b)
    {
      laz[k] = h(laz[k], x);
      return g(dat[k], p(laz[k], r - l));
    }
    return dat[k] = f(update(a, b, x, k * 2 + 1, l, (l + r) / 2),
                      update(a, b, x, k * 2 + 2, (l + r) / 2, r));
  }
  T update(int a, int b, E x) { return update(a, b, x, 0, 0, n); }
  T query(int a, int b, int k, int l, int r)
  {
    eval(r - l, k);
    if (r <= a || b <= l)
      return ti;
    if (a <= l && r <= b)
      return dat[k];
    T vl = query(a, b, k * 2 + 1, l, (l + r) / 2);
    T vr = query(a, b, k * 2 + 2, (l + r) / 2, r);
    return f(vl, vr);
  }
  T query(int a, int b) { return query(a, b, 0, 0, n); }
};

void solve()
{
  int n;
  cin >> n;
  using arr = array<ll, 3>;
  vector<arr> v(n);
  rep(i, n)
  {
    ll a;
    cin >> a;
    v[i] = {a * a, a, 1};
  }
  auto f = [](arr a, arr b) {
    return arr{a[0] + b[0], a[1] + b[1], a[2] + b[2]};
  };
  auto g = [](arr a, ll x) {
    return arr{a[0] + 2 * a[1] * x + a[2] * x * x, a[1] + a[2] * x, a[2]};
  };
  auto h = [](ll x, ll y) {
    return x + y;
  };
  SegmentTree<arr, ll> sg(v, f, g, h, arr{0, 0, 0}, 0);
  int q;
  cin >> q;
  rep(qi, q)
  {
    int type;
    cin >> type;
    if (type == 1)
    {
      int l, r;
      ll x;
      cin >> l >> r >> x;
      l--;
      sg.update(l, r, x);
    }
    else
    {
      int l, r;
      cin >> l >> r;
      l--;
      cout << sg.query(l, r)[0] << "\n";
    }
  }
}

int main()
{
  solve();
}
0