結果

問題 No.880 Yet Another Segment Tree Problem
ユーザー KoDKoD
提出日時 2021-12-17 21:31:30
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 9,356 bytes
コンパイル時間 2,283 ms
コンパイル使用メモリ 89,072 KB
実行使用メモリ 17,328 KB
最終ジャッジ日時 2023-10-24 03:00:35
合計ジャッジ時間 21,078 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 4 ms
4,348 KB
testcase_02 AC 4 ms
4,348 KB
testcase_03 AC 4 ms
4,348 KB
testcase_04 AC 4 ms
4,348 KB
testcase_05 AC 3 ms
4,348 KB
testcase_06 AC 3 ms
4,348 KB
testcase_07 AC 4 ms
4,348 KB
testcase_08 AC 4 ms
4,348 KB
testcase_09 AC 4 ms
4,348 KB
testcase_10 AC 4 ms
4,348 KB
testcase_11 AC 625 ms
12,332 KB
testcase_12 AC 603 ms
12,332 KB
testcase_13 AC 434 ms
12,068 KB
testcase_14 AC 596 ms
12,596 KB
testcase_15 AC 627 ms
12,596 KB
testcase_16 AC 662 ms
12,596 KB
testcase_17 AC 561 ms
12,860 KB
testcase_18 AC 557 ms
12,860 KB
testcase_19 AC 419 ms
12,596 KB
testcase_20 AC 426 ms
12,860 KB
testcase_21 AC 438 ms
12,596 KB
testcase_22 AC 424 ms
12,596 KB
testcase_23 AC 443 ms
12,596 KB
testcase_24 AC 389 ms
12,596 KB
testcase_25 AC 393 ms
12,860 KB
testcase_26 AC 408 ms
12,596 KB
testcase_27 AC 387 ms
12,596 KB
testcase_28 AC 411 ms
12,596 KB
testcase_29 AC 591 ms
12,596 KB
testcase_30 AC 618 ms
12,596 KB
testcase_31 AC 656 ms
12,596 KB
testcase_32 TLE -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#line 1 "segment_tree_beats.test.cpp"
#define PROBLEM "https://yukicoder.me/problems/no/880"
#line 2 "/Users/kodamankod/Desktop/CppProcon/Library/proconlib/container/segment_tree_beats.cpp"
#include <cassert>
#include <optional>
#include <utility>
#include <vector>
#line 2 "/Users/kodamankod/Desktop/CppProcon/Library/proconlib/utility/int_alias.cpp"
#include <cstdint>

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;
#line 3 "/Users/kodamankod/Desktop/CppProcon/Library/proconlib/utility/ceil_log2.cpp"

__attribute__((target("avx2"))) constexpr int ceil_log2(const u64 x) {
    int e = 0;
    while (((u64)1 << e) < x) ++e;
    return e;
}
#line 3 "/Users/kodamankod/Desktop/CppProcon/Library/proconlib/utility/countr_zero.cpp"

__attribute__((target("avx2"))) constexpr int countr_zero(const u64 x) { return x == 0 ? 64 : __builtin_ctzll(x); }
#line 2 "/Users/kodamankod/Desktop/CppProcon/Library/proconlib/utility/rep.cpp"
#include <algorithm>

class Range {
    struct Iter {
        int itr;
        constexpr Iter(const int pos) noexcept : itr(pos) {}
        constexpr void operator++() noexcept { ++itr; }
        constexpr bool operator!=(const Iter& other) const noexcept { return itr != other.itr; }
        constexpr int operator*() const noexcept { return itr; }
    };
    const Iter first, last;

  public:
    explicit constexpr Range(const int first, const int last) noexcept : first(first), last(std::max(first, last)) {}
    constexpr Iter begin() const noexcept { return first; }
    constexpr Iter end() const noexcept { return last; }
};

constexpr Range rep(const int l, const int r) noexcept { return Range(l, r); }
constexpr Range rep(const int n) noexcept { return Range(0, n); }
#line 3 "/Users/kodamankod/Desktop/CppProcon/Library/proconlib/utility/revrep.cpp"

class ReversedRange {
    struct Iter {
        int itr;
        constexpr Iter(const int pos) noexcept : itr(pos) {}
        constexpr void operator++() noexcept { --itr; }
        constexpr bool operator!=(const Iter& other) const noexcept { return itr != other.itr; }
        constexpr int operator*() const noexcept { return itr; }
    };
    const Iter first, last;

  public:
    explicit constexpr ReversedRange(const int first, const int 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; }
};

constexpr ReversedRange revrep(const int l, const int r) noexcept { return ReversedRange(l, r); }
constexpr ReversedRange revrep(const int n) noexcept { return ReversedRange(0, n); }
#line 10 "/Users/kodamankod/Desktop/CppProcon/Library/proconlib/container/segment_tree_beats.cpp"

template <class A> class SegmentTreeBeats {
    using M = typename A::Monoid;
    using E = typename A::Effector;
    using T = typename M::Type;
    using U = typename E::Type;
    int internal_size, logn, seg_size;
    std::vector<T> data;
    std::vector<U> lazy;

    void fetch(const int k) { data[k] = M::operation(data[2 * k], data[2 * k + 1]); }
    void apply(const int k, const U& e) {
        if (k >= seg_size) {
            data[k] = A::operation(data[k], e).value();
            return;
        }
        lazy[k] = E::operation(lazy[k], e);
        std::optional<T> result = A::operation(data[k], e);
        if (result) {
            data[k] = std::move(*result);
        } else {
            flush(k);
            fetch(k);
        }
    }
    void flush(const int k) {
        apply(2 * k, lazy[k]);
        apply(2 * k + 1, lazy[k]);
        lazy[k] = E::identity();
    }

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

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

    int size() const { return internal_size; }

    void assign(int i, const T& value) {
        assert(0 <= i and i < internal_size);
        i += seg_size;
        for (const int d : revrep(1, logn + 1)) flush(i >> d);
        data[i] = value;
        for (const int d : rep(1, logn + 1)) fetch(i >> d);
    }
    void operate(int l, int r, const U& e) {
        assert(0 <= l and l <= r and r <= internal_size);
        l += seg_size;
        r += seg_size;
        push(l);
        push(r);
        for (int 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);
    }

    T fold() const { return data[1]; }
    T fold(int l, int r) {
        assert(0 <= l and l <= r and r <= internal_size);
        l += seg_size;
        r += seg_size;
        push(l);
        push(r);
        T ret_l = M::identity(), ret_r = M::identity();
        while (l < r) {
            if (l & 1) ret_l = M::operation(ret_l, data[l++]);
            if (r & 1) ret_r = M::operation(data[--r], ret_r);
            l >>= 1;
            r >>= 1;
        }
        return M::operation(ret_l, ret_r);
    }

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

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

constexpr u32 MAX = 1000000000;

struct Info {
    u32 max, lcm;
    u64 sum;
    int len;
    static Info all_same(const u32 x, const int l) { return {x, x, (u64)x * l, l}; }
};

struct Operate {
    u32 assign, gcd;
};

struct Structure {
    struct Monoid {
        using Type = Info;
        static Info identity() { return {0, 1, 0, 0}; }
        static Info operation(const Info& l, const Info& r) {
            return {
                std::max(l.max, r.max),
                (u32)std::min<u64>(std::lcm<u64>(l.lcm, r.lcm), MAX + 1),
                l.sum + r.sum,
                l.len + r.len,
            };
        }
    };
    struct Effector {
        using Type = Operate;
        static Operate identity() { return {0, 0}; }
        static Operate operation(const Operate& l, const Operate& r) {
            if (r.assign) return r;
            if (l.assign) return {std::gcd(l.assign, r.gcd), 0};
            return {0, std::gcd(l.gcd, r.gcd)};
        }
    };
    static std::optional<Info> operation(const Info& m, const Operate& e) {
        if (m.len == 0) return m;
        if (e.assign) return Info::all_same(e.assign, m.len);
        if (e.gcd % m.lcm == 0) return m;
        if (m.len == 1) return Info::all_same(std::gcd(m.max, e.gcd), 1);
        return std::nullopt;
    }
};

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