結果

問題 No.1099 Range Square Sum
ユーザー PachicobuePachicobue
提出日時 2020-06-26 22:41:31
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 120 ms / 2,000 ms
コード長 12,005 bytes
コンパイル時間 2,288 ms
コンパイル使用メモリ 215,336 KB
実行使用メモリ 20,184 KB
最終ジャッジ日時 2023-09-18 06:18:58
合計ジャッジ時間 5,227 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 1 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 3 ms
4,380 KB
testcase_15 AC 3 ms
4,380 KB
testcase_16 AC 2 ms
4,376 KB
testcase_17 AC 2 ms
4,380 KB
testcase_18 AC 3 ms
4,380 KB
testcase_19 AC 2 ms
4,376 KB
testcase_20 AC 2 ms
4,384 KB
testcase_21 AC 119 ms
19,968 KB
testcase_22 AC 118 ms
20,004 KB
testcase_23 AC 120 ms
20,088 KB
testcase_24 AC 117 ms
20,156 KB
testcase_25 AC 118 ms
19,992 KB
testcase_26 AC 98 ms
19,928 KB
testcase_27 AC 99 ms
19,936 KB
testcase_28 AC 100 ms
20,184 KB
testcase_29 AC 99 ms
19,996 KB
testcase_30 AC 99 ms
19,936 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using ll   = long long;
using uint = unsigned int;
using ull  = unsigned long long;
using ld   = long double;
template<typename T>
using max_heap = std::priority_queue<T>;
template<typename T>
using min_heap = std::priority_queue<T, std::vector<T>, std::greater<T>>;
constexpr int popcount(const ull v) { return v ? __builtin_popcountll(v) : 0; }
constexpr int log2p1(const ull v) { return v ? 64 - __builtin_clzll(v) : 0; }
constexpr int lsbp1(const ull v) { return __builtin_ffsll(v); }
constexpr int clog(const ull v) { return v ? log2p1(v - 1) : 0; }
constexpr ull ceil2(const ull v) { return 1ULL << clog(v); }
constexpr ull floor2(const ull v) { return v ? (1ULL << (log2p1(v) - 1)) : 0ULL; }
constexpr bool btest(const ull mask, const int ind) { return (mask >> ind) & 1ULL; }
template<typename T> void bset(T& mask, const int ind) { mask |= ((T)1 << ind); }
template<typename T> void breset(T& mask, const int ind) { mask &= ~((T)1 << ind); }
template<typename T> void bflip(T& mask, const int ind) { mask ^= ((T)1 << ind); }
template<typename T> void bset(T& mask, const int ind, const bool b) { (b ? bset(mask, ind) : breset(mask, ind)); }
template<typename T> bool chmin(T& a, const T& b) { return (a > b ? a = b, true : false); }
template<typename T> bool chmax(T& a, const T& b) { return (a < b ? a = b, true : false); }

template<typename T> constexpr T inf_v      = std::numeric_limits<T>::max() / 4;
template<typename Real> constexpr Real pi_v = Real{3.141592653589793238462643383279502884};
constexpr ull TEN(const int n) { return n == 0 ? 1ULL : TEN(n - 1) * 10ULL; }
template<typename F> struct fix : F
{
    fix(F&& f) : F{std::forward<F>(f)} {}
    template<typename... Args> auto operator()(Args&&... args) const { return F::operator()(*this, std::forward<Args>(args)...); }
};
class printer
{
public:
    printer(std::ostream& os_ = std::cout) : os{os_} {}
    template<typename T> int operator()(const T& v) { return os << v, 0; }
    template<typename T> int operator()(const std::vector<T>& vs)
    {
        for (int i = 0; i < (int)vs.size(); i++) { os << (i ? " " : ""), this->operator()(vs[i]); }
        return 0;
    }
    template<typename T> int operator()(const std::vector<std::vector<T>>& vss)
    {
        for (int i = 0; i < (int)vss.size(); i++) { os << (0 <= i or i + 1 < (int)vss.size() ? "\n" : ""), this->operator()(vss[i]); }
        return 0;
    }
    template<typename T, typename... Args> int operator()(const T& v, const Args&... args) { return this->operator()(v), os << ' ', this->operator()(args...), 0; }
    template<typename... Args> int ln(const Args&... args) { return this->operator()(args...), os << '\n', 0; }
    template<typename... Args> int el(const Args&... args) { return this->operator()(args...), os << std::endl, 0; }
    template<typename... Args> int fmt(const std::string& s, const Args&... args) { return rec(s, 0, args...); }

private:
    int rec(const std::string& s, int index) { return os << s.substr(index, s.size()), 0; }
    template<typename T, typename... Args> int rec(const std::string& s, int index, const T& v, const Args&... args) { return index == s.size() ? 0 : s[index] == '%' ? (this->operator()(v), rec(s, index + 1, args...)) : (os << s[index], rec(s, index + 1, v, args...)); }
    std::ostream& os;
};
printer out;

template<typename T, typename F> std::vector<T> generated(const int n, F f)
{
    std::vector<T> ans(n);
    return std::generate(ans.begin(), ans.end(), f), ans;
}
std::vector<int> ioted(const int n, const int offset = 0)
{
    std::vector<int> ans(n);
    return std::iota(ans.begin(), ans.end(), offset), ans;
}
template<typename Vs> Vs reversed(const Vs& vs)
{
    auto ans = vs;
    return std::reverse(ans.begin(), ans.end()), ans;
}
template<typename T, typename F = std::less<T>> std::vector<T> sorted(const std::vector<T>& vs, F comp = F{})
{
    auto ans = vs;
    return std::sort(ans.begin(), ans.end(), comp), ans;
}
template<typename F> std::vector<int> sorted_iota(const int n, F comp = F{}, const int offset = 0) { return sorted(ioted(n, offset), comp); }
class scanner
{
public:
    scanner(std::istream& is_ = std::cin) : is{is_} { is.tie(nullptr), std::ios::sync_with_stdio(false); }
    template<typename T> T val()
    {
        static T v;
        return is >> v, v;
    }
    template<typename T> T val(const T offset) { return val<T>() - offset; }
    template<typename T> std::vector<T> vec(const int n)
    {
        return generated<T>(n, [&] { return val<T>(); });
    }
    template<typename T> std::vector<T> vec(const int n, const T offset)
    {
        return generated<T>(n, [&] { return val<T>(offset); });
    }
    template<typename T> std::vector<std::vector<T>> vvec(const int n0, const int n1)
    {
        return generated<std::vector<T>>(n0, [&] { return vec<T>(n1); });
    }
    template<typename T> std::vector<std::vector<T>> vvec(const int n0, const int n1, const T offset)
    {
        return generated<std::vector<T>>(n0, [&] { return vec<T>(n1, offset); });
    }
    template<typename... Args> auto tup() { return std::tuple<std::decay_t<Args>...>{val<Args>()...}; }
    template<typename... Args> auto tup(const Args&... offsets) { return std::tuple<std::decay_t<Args>...>{val<Args>(offsets)...}; }

private:
    std::istream& is;
};
scanner in;
#    define SHOW(...) static_cast<void>(0)

template<typename T, int n, int i = 0>
auto make_v(int const (&szs)[n], const T x = T{})
{
    if constexpr (i == n) {
        return x;
    } else {
        return std::vector(szs[i], make_v<T, n, i + 1>(szs, x));
    }
}

const auto min_merge    = [](const auto& x1, const auto& x2) { return std::min(x1, x2); };
const auto max_merge    = [](const auto& x1, const auto& x2) { return std::max(x1, x2); };
const auto minmax_merge = [](const auto& x1, const auto& x2) { return decltype(x1){std::min(x1.first, x2.first), std::max(x1.second, x2.second)}; };
const auto sum_merge    = [](const auto& x1, const auto& x2) { return x1 + x2; };
const auto affine_merge = [](const auto& x1, const auto& x2) { return decltype(x1){x1.first * x2.first, x1.first * x2.second + x1.second}; };

const auto plus_comp   = [](const auto& f1, const auto& f2) { return f1 + f2; };
const auto mult_comp   = [](const auto& f1, const auto& f2) { return f1 * f2; };
const auto assign_comp = [](const auto& f1, const auto& f2) { return f1 == inf_v<decltype(f1)> ? f2 : f1; };
const auto affine_comp = [](const auto& f1, const auto& f2) { return decltype(f1){f1.first * f2.first, f1.first * f2.second + f1.second}; };

const auto min_plus_apply      = [](const auto& f, const auto& x, const auto&) { return x + f; };
const auto max_plus_apply      = [](const auto& f, const auto& x, const auto&) { return x + f; };
const auto minmax_plus_apply   = [](const auto& f, const auto& x, const auto&) { return decltype(x){x.first + f, x.second + f}; };
const auto minmax_mult_apply   = [](const auto& f, const auto& x, const auto&) { return f >= 0 ? decltype(x){f * x.first, f * x.second} : decltype(x){f * x.second, f * x.first}; };
const auto min_assign_apply    = [](const auto& f, const auto& x, const auto&) { return f == inf_v<decltype(f)> ? x : f; };
const auto max_assign_apply    = [](const auto& f, const auto& x, const auto&) { return f == inf_v<decltype(f)> ? x : f; };
const auto minmax_assign_apply = [](const auto& f, const auto& x, const auto&) { return f == inf_v<decltype(f)> ? x : decltype(x){f, f}; };
const auto minmax_affine_apply = [](const auto& f, const auto x, const auto&) { return f.first >= 0 ? decltype(x){x.first * f.first + f.second, x.second * f.first + f.second} : decltype(x){x.second * f.first + f.second, x.first * f.first + f.second}; };
const auto sum_plus_apply      = [](const auto& f, const auto& x, const auto& l) { return x + f * l; };
const auto sum_mult_apply      = [](const auto& f, const auto& x, const auto&) { return x * f; };
const auto sum_assign_apply    = [](const auto& f, const auto& x, const auto& l) { return f == inf_v<decltype(f)> ? x : f * l; };
const auto sum_affine_apply    = [](const auto& f, const auto x, const auto& l) { return x * f.first + f.second * l; };

const auto reverse = [](const auto f) { return [f](const auto& x1, const auto& x2) { return f(x2, x1); }; };

template<typename T, typename F, typename Merge, typename Compose, typename Apply>
class lazyseg
{
public:
    lazyseg(const std::vector<T>& vs,
            const Merge merge_,
            const T& e_,
            const Compose compose_,
            const F& id_,
            const Apply apply_) : size{(int)vs.size()}, depth{clog(size)}, half{1 << depth}, vals(half << 1, e_), ops(half << 1, id_), merge{merge_}, e{e_}, compose{compose_}, id{id_}, apply{apply_}
    {
        std::copy(vs.begin(), vs.end(), vals.begin() + half);
        for (int i = half - 1; i >= 1; i--) { up(i); }
    }
    T get(const int a) { return assert(a < size), fold(a, a + 1); }
    void set(int a, const T& v)
    {
        assert(0 <= a and a < size);
        top_down(a += half), top_down(a + 1), ops[a] = id, vals[a] = v;
        while (a >>= 1) { up(a); }
    }
    T fold(int l, int r)
    {
        assert(0 <= l and l <= r and r <= size);
        if (l >= r) { return e; }
        top_down(l += half), top_down(r += half);
        T accl = e, accr = e;
        for (; l < r; l >>= 1, r >>= 1) {
            if (l & 1) { accl = merge(accl, vals[l++]); }
            if (r & 1) { accr = merge(vals[--r], accr); }
        }
        return merge(accl, accr);
    }
    void act(int l, int r, const F& f)
    {
        assert(0 <= l and l <= r and r <= size);
        const int lin = l + half, rin = r + half;
        top_down(l += half), top_down(r += half);
        for (int ls = 1, rs = 1; l < r; l >>= 1, r >>= 1, ls <<= 1, rs <<= 1) {
            if (l & 1) { update(l++, f, ls); }
            if (r & 1) { update(--r, f, rs); }
        }
        bottom_up(lin), bottom_up(rin);
    }
    friend std::ostream& operator<<(std::ostream& os, const lazyseg& lseg)
    {
        auto lseg2 = lseg;
        os << "[";
        for (int i = 0; i < lseg.sz; i++) { os << lseg2.get(i) << (i + 1 == lseg2.sz ? "" : ","); }
        return (os << "]\n");
    }

private:
    void up(const int i) { vals[i] = merge(vals[i << 1], vals[i << 1 | 1]); }
    void update(const int a, const F& f, const int l) { ops[a] = compose(f, ops[a]), vals[a] = apply(f, vals[a], l); }
    void down(const int a, const int l) { update(a << 1, ops[a], l >> 1), update(a << 1 | 1, ops[a], l >> 1), ops[a] = id; }
    void top_down(const int a)
    {
        const int b = (a / (a & -a)) >> 1;
        for (int i = 0, l = half; i < depth; i++, l >>= 1) {
            const int v = a >> (depth - i);
            if (v > b) { break; }
            down(v, l);
        }
    }
    void bottom_up(int a)
    {
        a = (a / (a & -a)) >> 1;
        for (; a >= 1; a >>= 1) { up(a); }
    }
    const int size, depth, half;
    std::vector<T> vals;
    std::vector<F> ops;
    const Merge merge;
    const T e;
    const Compose compose;
    const F id;
    const Apply apply;
};
int main()
{
    const auto N  = in.val<int>();
    const auto as = in.vec<ll>(N);
    using pll     = std::pair<ll, ll>;
    std::vector<pll> vs(N);
    for (int i = 0; i < N; i++) { vs[i] = pll{as[i] * as[i], as[i]}; }
    const auto Q = in.val<int>();
    auto seg     = lazyseg(
        vs, [&](const pll& x1, const pll& x2) { return pll{x1.first + x2.first, x1.second + x2.second}; }, pll{0, 0}, plus_comp, 0LL, [&](const auto f, const pll& x, const auto l) { return pll{x.first + 2LL * f * x.second + f * f * l, x.second + f * l}; });
    for (int q = 0; q < Q; q++) {
        const auto type = in.val<int>();
        if (type == 1) {
            const auto [l, r, x] = in.tup<int, int, ll>(1, 0, 0);
            seg.act(l, r, x);
        } else {
            const auto [l, r] = in.tup<int, int>(1, 0);
            out.ln(seg.fold(l, r).first);
        }
    }
    return 0;
}
0