結果

問題 No.880 Yet Another Segment Tree Problem
ユーザー KoDKoD
提出日時 2021-04-18 13:57:44
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 8,677 bytes
コンパイル時間 1,077 ms
コンパイル使用メモリ 88,696 KB
実行使用メモリ 12,916 KB
最終ジャッジ日時 2023-10-24 02:58:52
合計ジャッジ時間 19,515 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,372 KB
testcase_01 AC 4 ms
4,372 KB
testcase_02 AC 4 ms
4,372 KB
testcase_03 AC 4 ms
4,372 KB
testcase_04 AC 4 ms
4,372 KB
testcase_05 AC 4 ms
4,372 KB
testcase_06 AC 3 ms
4,372 KB
testcase_07 AC 4 ms
4,372 KB
testcase_08 AC 4 ms
4,372 KB
testcase_09 AC 5 ms
4,372 KB
testcase_10 AC 4 ms
4,372 KB
testcase_11 AC 604 ms
12,388 KB
testcase_12 AC 588 ms
12,388 KB
testcase_13 AC 421 ms
12,124 KB
testcase_14 AC 580 ms
12,652 KB
testcase_15 AC 605 ms
12,652 KB
testcase_16 AC 638 ms
12,652 KB
testcase_17 AC 538 ms
12,916 KB
testcase_18 AC 537 ms
12,916 KB
testcase_19 AC 415 ms
12,652 KB
testcase_20 AC 427 ms
12,916 KB
testcase_21 AC 442 ms
12,652 KB
testcase_22 AC 422 ms
12,652 KB
testcase_23 AC 444 ms
12,652 KB
testcase_24 AC 382 ms
12,652 KB
testcase_25 AC 392 ms
12,916 KB
testcase_26 AC 401 ms
12,652 KB
testcase_27 AC 383 ms
12,652 KB
testcase_28 AC 407 ms
12,652 KB
testcase_29 AC 575 ms
12,652 KB
testcase_30 AC 604 ms
12,652 KB
testcase_31 AC 638 ms
12,652 KB
testcase_32 TLE -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
権限があれば一括ダウンロードができます
コンパイルメッセージ
segment_tree_beats.test.cpp: In member function 'constexpr Monoid Monoid::operator+(const Monoid&) const':
segment_tree_beats.test.cpp:25:26: warning: narrowing conversion of '(long unsigned int)std::min<long unsigned int>(std::lcm<long unsigned int, unsigned int>(((long unsigned int)((u32)((const Monoid*)this)->Monoid::lcm)), ((u32)other.Monoid::lcm)), ((long unsigned int)(((unsigned int)MAX) + 1)))' from 'long unsigned int' to 'u32' {aka 'unsigned int'} [-Wnarrowing]

ソースコード

diff #

#line 1 "segment_tree_beats.test.cpp"
#define PROBLEM "https://judge.yosupo.jp/problem/range_chmin_chmax_add_range_sum"
#line 2 "/Users/kodamankod/Desktop/CppProcon/Library/proconlib/utility/int_alias.cpp"
#include <cstdint>
#include <cstddef>

using i32 = std::int32_t;
using u32 = std::uint32_t;
using i64 = std::int64_t;
using u64 = std::uint64_t;
using i128 = __int128_t;
using u128 = __uint128_t;
using isize = std::ptrdiff_t;
using usize = std::size_t;
#line 3 "/Users/kodamankod/Desktop/CppProcon/Library/proconlib/utility/rep.cpp"
#include <algorithm>

class rep {
    struct Iter {
        usize itr;
        constexpr Iter(const usize pos) noexcept: itr(pos) { }
        constexpr void operator ++ () noexcept { ++itr; }
        constexpr bool operator != (const Iter& other) const noexcept { return itr != other.itr; }
        constexpr usize operator * () const noexcept { return itr; }
    };
    const Iter first, last;
public:
    explicit constexpr rep(const usize first, const usize last) noexcept: first(first), last(std::max(first, last)) { }
    constexpr Iter begin() const noexcept { return first; }
    constexpr Iter end() const noexcept { return last; }
};
#line 4 "/Users/kodamankod/Desktop/CppProcon/Library/proconlib/utility/revrep.cpp"

class revrep {
    struct Iter {
        usize itr;
        constexpr Iter(const usize pos) noexcept: itr(pos) { }
        constexpr void operator ++ () noexcept { --itr; }
        constexpr bool operator != (const Iter& other) const noexcept { return itr != other.itr; }
        constexpr usize operator * () const noexcept { return itr; }
    };
    const Iter first, last;
public:
    explicit constexpr revrep(const usize first, const usize last) noexcept: first(last - 1), last(std::min(first, last) - 1) { }
    constexpr Iter begin() const noexcept { return first; }
    constexpr Iter end() const noexcept { return last; }
};
#line 3 "/Users/kodamankod/Desktop/CppProcon/Library/proconlib/bit/ceil_log2.cpp"

constexpr u64 ceil_log2(const u64 x) {
    u64 e = 0;
    while (((u64) 1 << e) < x) ++e;
    return e;
}
#line 3 "/Users/kodamankod/Desktop/CppProcon/Library/proconlib/bit/bit_rzeros.cpp"

constexpr u64 bit_rzeros(const u64 x) {
    return x == 0 ? 64 : __builtin_ctzll(x);
}
#line 7 "/Users/kodamankod/Desktop/CppProcon/Library/proconlib/container/segment_tree_beats.cpp"
#include <vector>
#include <cassert>
#include <optional>

template <class Monoid, class Effector, std::optional<Monoid> (*mapping) (Monoid, Effector)>
class SegmentTreeBeats {
    using M = Monoid;
    using E = Effector;
    usize internal_size, logn, seg_size;
    std::vector<M> data;
    std::vector<E> lazy;

    void fetch(const usize k) { data[k] = data[2 * k] + data[2 * k + 1]; }
    void apply(const usize k, const E& e) {
        if (k >= seg_size) {
            data[k] = mapping(data[k], e).value();
            return;
        }
        lazy[k] = lazy[k] * e;
        const auto result = mapping(data[k], e);
        if (result.has_value()) {
            data[k] = result.value();
        }
        else { 
            flush(k); 
            fetch(k); 
        }
    }
    void flush(const usize k) {
        apply(2 * k, lazy[k]);
        apply(2 * k + 1, lazy[k]);
        lazy[k] = E::one();
    }

    void push(const usize k) { for (const usize d: revrep(bit_rzeros(k) + 1, logn + 1)) flush(k >> d); }
    void pull(usize k) { for (k >>= bit_rzeros(k); k > 1;) fetch(k >>= 1); }

public:
    explicit SegmentTreeBeats(const usize size = 0, const M& value = M::zero()):
        SegmentTreeBeats(std::vector<M>(size, value)) { }
    explicit SegmentTreeBeats(const std::vector<M>& vec): internal_size(vec.size()) {
        logn = ceil_log2(internal_size);
        seg_size = 1 << logn;
        data = std::vector<M>(2 * seg_size, M::zero());
        lazy = std::vector<E>(seg_size, E::one());
        for (const usize i: rep(0, internal_size)) data[seg_size + i] = vec[i];
        for (const usize i: revrep(1, seg_size)) fetch(i);
    }

    usize size() const { return internal_size; }

    void assign(usize i, const M& value) {
        assert(i < internal_size);
        i += seg_size;
        for (const usize d: revrep(1, logn + 1)) flush(i >> d);
        data[i] = value;
        for (const usize d: rep(1, logn + 1)) fetch(i >> d);
    }
    void operate(usize l, usize r, const E& e) {
        assert(l <= r and r <= internal_size);
        l += seg_size; r += seg_size;
        push(l); push(r);
        for (usize l0 = l, r0 = r; l0 < r0; l0 >>= 1, r0 >>= 1) {
            if (l0 & 1) apply(l0++, e);
            if (r0 & 1) apply(--r0, e);
        }
        pull(l); pull(r);
    }
    
    M fold() const { return data[1]; }
    M fold(usize l, usize r) {
        assert(l <= r and r <= internal_size);
        l += seg_size; r += seg_size;
        push(l); push(r);
        M ret_l = M::zero(), ret_r = M::zero();
        while (l < r) {
            if (l & 1) ret_l = ret_l + data[l++];
            if (r & 1) ret_r = data[--r] + ret_r;
            l >>= 1;
            r >>= 1;
        }
        return ret_l + ret_r;
    }

    template <class F>
    usize max_right(usize l, const F& f) {
        assert(l <= internal_size);
        assert(f(M::zero()));
        if (l == internal_size) return internal_size;
        l += seg_size;
        for (const usize d: revrep(1, logn + 1)) flush(l >> d);
        M sum = M::zero();
        do {
            while (!(l & 1)) l >>= 1;
            if (!f(sum + data[l])) {
                while (l < seg_size) {
                    flush(l);
                    l = 2 * l;
                    if (f(sum + data[l])) sum = sum + data[l++];
                }
                return l - seg_size;
            }
            sum = sum + data[l++];
        } while ((l & -l) != l);
        return internal_size;
    }

    template <class F>
    usize min_left(usize r, const F& f) {
        assert(r <= internal_size);
        assert(f(M::zero()));
        if (r == 0) return 0;
        r += seg_size;
        for (const usize d: revrep(1, logn + 1)) flush((r - 1) >> d);
        M sum = M::zero();
        do {
            r -= 1;
            while (r > 1 and (r & 1)) r >>= 1;
            if (!f(data[r] + sum)) {
                while (r < seg_size) {
                    flush(r);
                    r = 2 * r + 1;
                    if (f(data[r] + sum)) sum = data[r--] + sum;
                }
                return r + 1 - seg_size;
            }
            sum = data[r] + sum;
        } while ((r & -r) != r);
        return 0;
    }
};
#line 5 "segment_tree_beats.test.cpp"
#include <iostream>
#include <numeric>
#line 9 "segment_tree_beats.test.cpp"

constexpr u32 MAX = 1000000000;

struct Monoid {
    static constexpr Monoid zero() {
        return Monoid { 0, 1, 0, 0 };
    }
    static constexpr Monoid all_same(const u32 x, const usize l) {
        return Monoid { x, x, (u64) x * l, l };
    };
    u32 max, lcm;
    u64 sum;
    usize len;
    constexpr Monoid operator + (const Monoid& other) const {
        return Monoid {
            std::max(max, other.max),
            std::min<u64>(std::lcm<u64>(lcm, other.lcm), MAX + 1),
            sum + other.sum,
            len + other.len,
        };
    }
};

struct Effector {
    static constexpr Effector one() {
        return Effector { 0, 0 };
    }
    u32 assign, gcd;
    constexpr Effector operator * (const Effector& other) const {
        if (other.assign) {
            return other;
        }
        if (assign) {
            return Effector { std::gcd(assign, other.gcd), 0 };
        }
        else {
            return Effector { 0, std::gcd(gcd, other.gcd) };
        }
    }    
};

std::optional<Monoid> mapping(Monoid m, Effector e) {
    if (m.len == 0) return m;
    if (e.assign) return Monoid::all_same(e.assign, m.len);
    if (e.gcd % m.lcm == 0) return m;
    if (m.len == 1) return Monoid::all_same(std::gcd(m.max, e.gcd), 1);
    return std::nullopt;
}

int main() {
    usize N, Q;
    std::cin >> N >> Q;
    std::vector<Monoid> build;
    build.reserve(N);
    for (const auto i: rep(0, N)) {
        u32 x;
        std::cin >> x;
        build.push_back(Monoid::all_same(x, 1));
    }
    SegmentTreeBeats<Monoid, Effector, mapping> seg(build);
    while (Q--) {
        usize t, l, r;
        std::cin >> t >> l >> r;
        l -= 1;
        if (t <= 2) {
            u32 x;
            std::cin >> x;
            seg.operate(l, r, (t == 1 ? Effector { x, 0 } : Effector { 0, x }));
        }
        else {
            const auto m = seg.fold(l, r);
            std::cout << (t == 3 ? m.max : m.sum) << '\n';
        }
    }
    return 0;
}
0