proconlib

This documentation is automatically generated by competitive-verifier/competitive-verifier

View the Project on GitHub anqooqie/proconlib

:heavy_check_mark: tests/avl_tree/set.test.cpp

Depends on

Code

// competitive-verifier: PROBLEM https://judge.yosupo.jp/problem/point_set_range_composite

#include <utility>
#include <variant>
#include <iostream>
#include <vector>
#include "atcoder/modint.hpp"
#include "tools/avl_tree.hpp"

using mint = atcoder::modint998244353;

using S = std::pair<mint, mint>;
struct SM {
  using T = S;
  static T op(const T& x, const T& y) {
    return T(y.first * x.first, y.first * x.second + y.second);
  }
  static T e() {
    return T(mint::raw(1), mint::raw(0));
  }
};

int main() {
  std::cin.tie(nullptr);
  std::ios_base::sync_with_stdio(false);

  int N, Q;
  std::cin >> N >> Q;
  std::vector<S> init;
  init.reserve(N);
  for (int i = 0; i < N; ++i) {
    int a, b;
    std::cin >> a >> b;
    init.emplace_back(mint::raw(a), mint::raw(b));
  }

  tools::avl_tree<SM>::buffer buffer;
  tools::avl_tree<SM> avl_tree(buffer, init);
  for (int q = 0; q < Q; ++q) {
    int t;
    std::cin >> t;
    if (t == 0) {
      int p, c, d;
      std::cin >> p >> c >> d;
      avl_tree.set(p, S(mint::raw(c), mint::raw(d)));
    } else {
      int l, r, x;
      std::cin >> l >> r >> x;
      const auto [a, b] = avl_tree.prod(l, r);
      std::cout << (a * mint::raw(x) + b).val() << '\n';
    }
  }

  return 0;
}
#line 1 "tests/avl_tree/set.test.cpp"
// competitive-verifier: PROBLEM https://judge.yosupo.jp/problem/point_set_range_composite

#include <utility>
#include <variant>
#include <iostream>
#include <vector>
#line 1 "lib/ac-library/atcoder/modint.hpp"



#include <cassert>
#include <numeric>
#include <type_traits>

#ifdef _MSC_VER
#include <intrin.h>
#endif

#line 1 "lib/ac-library/atcoder/internal_math.hpp"



#line 5 "lib/ac-library/atcoder/internal_math.hpp"

#ifdef _MSC_VER
#include <intrin.h>
#endif

namespace atcoder {

namespace internal {

// @param m `1 <= m`
// @return x mod m
constexpr long long safe_mod(long long x, long long m) {
    x %= m;
    if (x < 0) x += m;
    return x;
}

// Fast modular multiplication by barrett reduction
// Reference: https://en.wikipedia.org/wiki/Barrett_reduction
// NOTE: reconsider after Ice Lake
struct barrett {
    unsigned int _m;
    unsigned long long im;

    // @param m `1 <= m`
    explicit barrett(unsigned int m) : _m(m), im((unsigned long long)(-1) / m + 1) {}

    // @return m
    unsigned int umod() const { return _m; }

    // @param a `0 <= a < m`
    // @param b `0 <= b < m`
    // @return `a * b % m`
    unsigned int mul(unsigned int a, unsigned int b) const {
        // [1] m = 1
        // a = b = im = 0, so okay

        // [2] m >= 2
        // im = ceil(2^64 / m)
        // -> im * m = 2^64 + r (0 <= r < m)
        // let z = a*b = c*m + d (0 <= c, d < m)
        // a*b * im = (c*m + d) * im = c*(im*m) + d*im = c*2^64 + c*r + d*im
        // c*r + d*im < m * m + m * im < m * m + 2^64 + m <= 2^64 + m * (m + 1) < 2^64 * 2
        // ((ab * im) >> 64) == c or c + 1
        unsigned long long z = a;
        z *= b;
#ifdef _MSC_VER
        unsigned long long x;
        _umul128(z, im, &x);
#else
        unsigned long long x =
            (unsigned long long)(((unsigned __int128)(z)*im) >> 64);
#endif
        unsigned long long y = x * _m;
        return (unsigned int)(z - y + (z < y ? _m : 0));
    }
};

// @param n `0 <= n`
// @param m `1 <= m`
// @return `(x ** n) % m`
constexpr long long pow_mod_constexpr(long long x, long long n, int m) {
    if (m == 1) return 0;
    unsigned int _m = (unsigned int)(m);
    unsigned long long r = 1;
    unsigned long long y = safe_mod(x, m);
    while (n) {
        if (n & 1) r = (r * y) % _m;
        y = (y * y) % _m;
        n >>= 1;
    }
    return r;
}

// Reference:
// M. Forisek and J. Jancina,
// Fast Primality Testing for Integers That Fit into a Machine Word
// @param n `0 <= n`
constexpr bool is_prime_constexpr(int n) {
    if (n <= 1) return false;
    if (n == 2 || n == 7 || n == 61) return true;
    if (n % 2 == 0) return false;
    long long d = n - 1;
    while (d % 2 == 0) d /= 2;
    constexpr long long bases[3] = {2, 7, 61};
    for (long long a : bases) {
        long long t = d;
        long long y = pow_mod_constexpr(a, t, n);
        while (t != n - 1 && y != 1 && y != n - 1) {
            y = y * y % n;
            t <<= 1;
        }
        if (y != n - 1 && t % 2 == 0) {
            return false;
        }
    }
    return true;
}
template <int n> constexpr bool is_prime = is_prime_constexpr(n);

// @param b `1 <= b`
// @return pair(g, x) s.t. g = gcd(a, b), xa = g (mod b), 0 <= x < b/g
constexpr std::pair<long long, long long> inv_gcd(long long a, long long b) {
    a = safe_mod(a, b);
    if (a == 0) return {b, 0};

    // Contracts:
    // [1] s - m0 * a = 0 (mod b)
    // [2] t - m1 * a = 0 (mod b)
    // [3] s * |m1| + t * |m0| <= b
    long long s = b, t = a;
    long long m0 = 0, m1 = 1;

    while (t) {
        long long u = s / t;
        s -= t * u;
        m0 -= m1 * u;  // |m1 * u| <= |m1| * s <= b

        // [3]:
        // (s - t * u) * |m1| + t * |m0 - m1 * u|
        // <= s * |m1| - t * u * |m1| + t * (|m0| + |m1| * u)
        // = s * |m1| + t * |m0| <= b

        auto tmp = s;
        s = t;
        t = tmp;
        tmp = m0;
        m0 = m1;
        m1 = tmp;
    }
    // by [3]: |m0| <= b/g
    // by g != b: |m0| < b/g
    if (m0 < 0) m0 += b / s;
    return {s, m0};
}

// Compile time primitive root
// @param m must be prime
// @return primitive root (and minimum in now)
constexpr int primitive_root_constexpr(int m) {
    if (m == 2) return 1;
    if (m == 167772161) return 3;
    if (m == 469762049) return 3;
    if (m == 754974721) return 11;
    if (m == 998244353) return 3;
    int divs[20] = {};
    divs[0] = 2;
    int cnt = 1;
    int x = (m - 1) / 2;
    while (x % 2 == 0) x /= 2;
    for (int i = 3; (long long)(i)*i <= x; i += 2) {
        if (x % i == 0) {
            divs[cnt++] = i;
            while (x % i == 0) {
                x /= i;
            }
        }
    }
    if (x > 1) {
        divs[cnt++] = x;
    }
    for (int g = 2;; g++) {
        bool ok = true;
        for (int i = 0; i < cnt; i++) {
            if (pow_mod_constexpr(g, (m - 1) / divs[i], m) == 1) {
                ok = false;
                break;
            }
        }
        if (ok) return g;
    }
}
template <int m> constexpr int primitive_root = primitive_root_constexpr(m);

// @param n `n < 2^32`
// @param m `1 <= m < 2^32`
// @return sum_{i=0}^{n-1} floor((ai + b) / m) (mod 2^64)
unsigned long long floor_sum_unsigned(unsigned long long n,
                                      unsigned long long m,
                                      unsigned long long a,
                                      unsigned long long b) {
    unsigned long long ans = 0;
    while (true) {
        if (a >= m) {
            ans += n * (n - 1) / 2 * (a / m);
            a %= m;
        }
        if (b >= m) {
            ans += n * (b / m);
            b %= m;
        }

        unsigned long long y_max = a * n + b;
        if (y_max < m) break;
        // y_max < m * (n + 1)
        // floor(y_max / m) <= n
        n = (unsigned long long)(y_max / m);
        b = (unsigned long long)(y_max % m);
        std::swap(m, a);
    }
    return ans;
}

}  // namespace internal

}  // namespace atcoder


#line 1 "lib/ac-library/atcoder/internal_type_traits.hpp"



#line 7 "lib/ac-library/atcoder/internal_type_traits.hpp"

namespace atcoder {

namespace internal {

#ifndef _MSC_VER
template <class T>
using is_signed_int128 =
    typename std::conditional<std::is_same<T, __int128_t>::value ||
                                  std::is_same<T, __int128>::value,
                              std::true_type,
                              std::false_type>::type;

template <class T>
using is_unsigned_int128 =
    typename std::conditional<std::is_same<T, __uint128_t>::value ||
                                  std::is_same<T, unsigned __int128>::value,
                              std::true_type,
                              std::false_type>::type;

template <class T>
using make_unsigned_int128 =
    typename std::conditional<std::is_same<T, __int128_t>::value,
                              __uint128_t,
                              unsigned __int128>;

template <class T>
using is_integral = typename std::conditional<std::is_integral<T>::value ||
                                                  is_signed_int128<T>::value ||
                                                  is_unsigned_int128<T>::value,
                                              std::true_type,
                                              std::false_type>::type;

template <class T>
using is_signed_int = typename std::conditional<(is_integral<T>::value &&
                                                 std::is_signed<T>::value) ||
                                                    is_signed_int128<T>::value,
                                                std::true_type,
                                                std::false_type>::type;

template <class T>
using is_unsigned_int =
    typename std::conditional<(is_integral<T>::value &&
                               std::is_unsigned<T>::value) ||
                                  is_unsigned_int128<T>::value,
                              std::true_type,
                              std::false_type>::type;

template <class T>
using to_unsigned = typename std::conditional<
    is_signed_int128<T>::value,
    make_unsigned_int128<T>,
    typename std::conditional<std::is_signed<T>::value,
                              std::make_unsigned<T>,
                              std::common_type<T>>::type>::type;

#else

template <class T> using is_integral = typename std::is_integral<T>;

template <class T>
using is_signed_int =
    typename std::conditional<is_integral<T>::value && std::is_signed<T>::value,
                              std::true_type,
                              std::false_type>::type;

template <class T>
using is_unsigned_int =
    typename std::conditional<is_integral<T>::value &&
                                  std::is_unsigned<T>::value,
                              std::true_type,
                              std::false_type>::type;

template <class T>
using to_unsigned = typename std::conditional<is_signed_int<T>::value,
                                              std::make_unsigned<T>,
                                              std::common_type<T>>::type;

#endif

template <class T>
using is_signed_int_t = std::enable_if_t<is_signed_int<T>::value>;

template <class T>
using is_unsigned_int_t = std::enable_if_t<is_unsigned_int<T>::value>;

template <class T> using to_unsigned_t = typename to_unsigned<T>::type;

}  // namespace internal

}  // namespace atcoder


#line 14 "lib/ac-library/atcoder/modint.hpp"

namespace atcoder {

namespace internal {

struct modint_base {};
struct static_modint_base : modint_base {};

template <class T> using is_modint = std::is_base_of<modint_base, T>;
template <class T> using is_modint_t = std::enable_if_t<is_modint<T>::value>;

}  // namespace internal

template <int m, std::enable_if_t<(1 <= m)>* = nullptr>
struct static_modint : internal::static_modint_base {
    using mint = static_modint;

  public:
    static constexpr int mod() { return m; }
    static mint raw(int v) {
        mint x;
        x._v = v;
        return x;
    }

    static_modint() : _v(0) {}
    template <class T, internal::is_signed_int_t<T>* = nullptr>
    static_modint(T v) {
        long long x = (long long)(v % (long long)(umod()));
        if (x < 0) x += umod();
        _v = (unsigned int)(x);
    }
    template <class T, internal::is_unsigned_int_t<T>* = nullptr>
    static_modint(T v) {
        _v = (unsigned int)(v % umod());
    }

    unsigned int val() const { return _v; }

    mint& operator++() {
        _v++;
        if (_v == umod()) _v = 0;
        return *this;
    }
    mint& operator--() {
        if (_v == 0) _v = umod();
        _v--;
        return *this;
    }
    mint operator++(int) {
        mint result = *this;
        ++*this;
        return result;
    }
    mint operator--(int) {
        mint result = *this;
        --*this;
        return result;
    }

    mint& operator+=(const mint& rhs) {
        _v += rhs._v;
        if (_v >= umod()) _v -= umod();
        return *this;
    }
    mint& operator-=(const mint& rhs) {
        _v -= rhs._v;
        if (_v >= umod()) _v += umod();
        return *this;
    }
    mint& operator*=(const mint& rhs) {
        unsigned long long z = _v;
        z *= rhs._v;
        _v = (unsigned int)(z % umod());
        return *this;
    }
    mint& operator/=(const mint& rhs) { return *this = *this * rhs.inv(); }

    mint operator+() const { return *this; }
    mint operator-() const { return mint() - *this; }

    mint pow(long long n) const {
        assert(0 <= n);
        mint x = *this, r = 1;
        while (n) {
            if (n & 1) r *= x;
            x *= x;
            n >>= 1;
        }
        return r;
    }
    mint inv() const {
        if (prime) {
            assert(_v);
            return pow(umod() - 2);
        } else {
            auto eg = internal::inv_gcd(_v, m);
            assert(eg.first == 1);
            return eg.second;
        }
    }

    friend mint operator+(const mint& lhs, const mint& rhs) {
        return mint(lhs) += rhs;
    }
    friend mint operator-(const mint& lhs, const mint& rhs) {
        return mint(lhs) -= rhs;
    }
    friend mint operator*(const mint& lhs, const mint& rhs) {
        return mint(lhs) *= rhs;
    }
    friend mint operator/(const mint& lhs, const mint& rhs) {
        return mint(lhs) /= rhs;
    }
    friend bool operator==(const mint& lhs, const mint& rhs) {
        return lhs._v == rhs._v;
    }
    friend bool operator!=(const mint& lhs, const mint& rhs) {
        return lhs._v != rhs._v;
    }

  private:
    unsigned int _v;
    static constexpr unsigned int umod() { return m; }
    static constexpr bool prime = internal::is_prime<m>;
};

template <int id> struct dynamic_modint : internal::modint_base {
    using mint = dynamic_modint;

  public:
    static int mod() { return (int)(bt.umod()); }
    static void set_mod(int m) {
        assert(1 <= m);
        bt = internal::barrett(m);
    }
    static mint raw(int v) {
        mint x;
        x._v = v;
        return x;
    }

    dynamic_modint() : _v(0) {}
    template <class T, internal::is_signed_int_t<T>* = nullptr>
    dynamic_modint(T v) {
        long long x = (long long)(v % (long long)(mod()));
        if (x < 0) x += mod();
        _v = (unsigned int)(x);
    }
    template <class T, internal::is_unsigned_int_t<T>* = nullptr>
    dynamic_modint(T v) {
        _v = (unsigned int)(v % mod());
    }

    unsigned int val() const { return _v; }

    mint& operator++() {
        _v++;
        if (_v == umod()) _v = 0;
        return *this;
    }
    mint& operator--() {
        if (_v == 0) _v = umod();
        _v--;
        return *this;
    }
    mint operator++(int) {
        mint result = *this;
        ++*this;
        return result;
    }
    mint operator--(int) {
        mint result = *this;
        --*this;
        return result;
    }

    mint& operator+=(const mint& rhs) {
        _v += rhs._v;
        if (_v >= umod()) _v -= umod();
        return *this;
    }
    mint& operator-=(const mint& rhs) {
        _v += mod() - rhs._v;
        if (_v >= umod()) _v -= umod();
        return *this;
    }
    mint& operator*=(const mint& rhs) {
        _v = bt.mul(_v, rhs._v);
        return *this;
    }
    mint& operator/=(const mint& rhs) { return *this = *this * rhs.inv(); }

    mint operator+() const { return *this; }
    mint operator-() const { return mint() - *this; }

    mint pow(long long n) const {
        assert(0 <= n);
        mint x = *this, r = 1;
        while (n) {
            if (n & 1) r *= x;
            x *= x;
            n >>= 1;
        }
        return r;
    }
    mint inv() const {
        auto eg = internal::inv_gcd(_v, mod());
        assert(eg.first == 1);
        return eg.second;
    }

    friend mint operator+(const mint& lhs, const mint& rhs) {
        return mint(lhs) += rhs;
    }
    friend mint operator-(const mint& lhs, const mint& rhs) {
        return mint(lhs) -= rhs;
    }
    friend mint operator*(const mint& lhs, const mint& rhs) {
        return mint(lhs) *= rhs;
    }
    friend mint operator/(const mint& lhs, const mint& rhs) {
        return mint(lhs) /= rhs;
    }
    friend bool operator==(const mint& lhs, const mint& rhs) {
        return lhs._v == rhs._v;
    }
    friend bool operator!=(const mint& lhs, const mint& rhs) {
        return lhs._v != rhs._v;
    }

  private:
    unsigned int _v;
    static internal::barrett bt;
    static unsigned int umod() { return bt.umod(); }
};
template <int id> internal::barrett dynamic_modint<id>::bt(998244353);

using modint998244353 = static_modint<998244353>;
using modint1000000007 = static_modint<1000000007>;
using modint = dynamic_modint<-1>;

namespace internal {

template <class T>
using is_static_modint = std::is_base_of<internal::static_modint_base, T>;

template <class T>
using is_static_modint_t = std::enable_if_t<is_static_modint<T>::value>;

template <class> struct is_dynamic_modint : public std::false_type {};
template <int id>
struct is_dynamic_modint<dynamic_modint<id>> : public std::true_type {};

template <class T>
using is_dynamic_modint_t = std::enable_if_t<is_dynamic_modint<T>::value>;

}  // namespace internal

}  // namespace atcoder


#line 1 "tools/avl_tree.hpp"



#line 1 "tools/detail/avl_tree_impl.hpp"



#line 6 "tools/detail/avl_tree_impl.hpp"
#include <functional>
#line 8 "tools/detail/avl_tree_impl.hpp"
#include <algorithm>
#line 11 "tools/detail/avl_tree_impl.hpp"
#include <cmath>
#line 1 "tools/fix.hpp"



#line 6 "tools/fix.hpp"

namespace tools {
  template <typename F>
  struct fix : F {
    template <typename G>
    fix(G&& g) : F({::std::forward<G>(g)}) {
    }

    template <typename... Args>
    decltype(auto) operator()(Args&&... args) const {
      return F::operator()(*this, ::std::forward<Args>(args)...);
    }
  };

  template <typename F>
  fix(F&&) -> fix<::std::decay_t<F>>;
}


#line 13 "tools/detail/avl_tree_impl.hpp"

namespace tools {
  namespace detail {
    namespace avl_tree {
      struct nop_monoid {
        using T = ::std::monostate;
        static constexpr T op(T, T) {
          return T{};
        }
        static constexpr T e() {
          return T{};
        }
      };
      template <typename SM>
      typename SM::T nop(typename nop_monoid::T, const typename SM::T& x) {
        return x;
      }

      template <bool Reversible, typename SM, typename FM = nop_monoid, auto mapping = nop<SM>>
      class avl_tree_impl {
      private:
        using S = typename SM::T;
        using F = typename FM::T;
        static_assert(
          ::std::is_convertible_v<decltype(mapping), ::std::function<S(F, S)>>,
          "mapping must work as S(F, S)");
        constexpr static bool is_lazy = !::std::is_same_v<FM, nop_monoid>;

        struct node {
          int id;
          int l_id;
          int r_id;
          int height;
          int size;
          S prod;
          ::std::conditional_t<Reversible, S, ::std::monostate> rprod;
          bool rev;
          F lazy;
        };

      public:
        class buffer {
        private:
          ::std::vector<node> m_nodes;

        public:
          buffer() {
            if constexpr (Reversible) {
              this->m_nodes.push_back(node{0, 0, 0, 0, 0, SM::e(), SM::e(), false, FM::e()});
            } else {
              this->m_nodes.push_back(node{0, 0, 0, 0, 0, SM::e(), ::std::monostate{}, false, FM::e()});
            }
          }
          buffer(const buffer&) = default;
          buffer(buffer&&) = default;
          ~buffer() = default;
          buffer& operator=(const buffer&) = default;
          buffer& operator=(buffer&&) = default;

          friend ::tools::detail::avl_tree::avl_tree_impl<Reversible, SM, FM, mapping>;
        };

      private:
        buffer *m_buffer;
        int m_root_id;

        void fetch(const int id) {
          auto& node = this->m_buffer->m_nodes[id];
          const auto& l_node = this->m_buffer->m_nodes[node.l_id];
          const auto& r_node = this->m_buffer->m_nodes[node.r_id];

          node.height = 1 + ::std::max(l_node.height, r_node.height);
          node.size = l_node.size + r_node.size;
          node.prod = SM::op(l_node.prod, r_node.prod);
          if constexpr (Reversible) {
            node.rprod = SM::op(r_node.rprod, l_node.rprod);
          }
        }
        void propagate(const int id) {
          auto& node = this->m_buffer->m_nodes[id];
          auto& l_node = this->m_buffer->m_nodes[node.l_id];
          auto& r_node = this->m_buffer->m_nodes[node.r_id];

          assert(!(node.size == 0) || (node.id == 0 && node.l_id == 0 && node.r_id == 0));
          assert(!(node.size == 1) || (node.id > 0 && node.l_id == 0 && node.r_id == 0));
          assert(!(node.size > 1) || (node.id > 0 && node.l_id > 0 && node.r_id > 0));

          if constexpr (Reversible) {
            if (node.rev) {
              if (node.size > 1) {
                l_node.rev = !l_node.rev;
                r_node.rev = !r_node.rev;
                ::std::swap(node.l_id, node.r_id);
                ::std::swap(node.prod, node.rprod);
              }
              node.rev = false;
            }
          }
          if constexpr (is_lazy) {
            if (node.lazy != FM::e()) {
              if (node.size > 1) {
                l_node.lazy = FM::op(node.lazy, l_node.lazy);
                r_node.lazy = FM::op(node.lazy, r_node.lazy);
              }
              node.prod = mapping(node.lazy, node.prod);
              if constexpr (Reversible) {
                 node.rprod = mapping(node.lazy, node.rprod);
              }
              node.lazy = FM::e();
            }
          }
        }

        int add_node(const S& x) {
          const int id = this->m_buffer->m_nodes.size();
          if constexpr (Reversible) {
            this->m_buffer->m_nodes.push_back(node{id, 0, 0, 1, 1, x, x, false, FM::e()});
          } else {
            this->m_buffer->m_nodes.push_back(node{id, 0, 0, 1, 1, x, ::std::monostate{}, false, FM::e()});
          }
          return id;
        }
        int add_node(const int l_id, const int r_id) {
          const int id = this->m_buffer->m_nodes.size();
          if constexpr (Reversible) {
            this->m_buffer->m_nodes.push_back(node{id, l_id, r_id, 0, 0, SM::e(), SM::e(), false, FM::e()});
          } else {
            this->m_buffer->m_nodes.push_back(node{id, l_id, r_id, 0, 0, SM::e(), ::std::monostate{}, false, FM::e()});
          }
          this->fetch(id);
          return id;
        }

        int rotate_l(const int id) {
          auto& node = this->m_buffer->m_nodes[id];
          auto& r_node = this->m_buffer->m_nodes[node.r_id];

          assert(node.size > 1);
          assert(node.id > 0);
          assert(node.l_id > 0);
          assert(node.r_id > 0);
          assert(r_node.size > 1);
          assert(r_node.id > 0);
          assert(r_node.l_id > 0);
          assert(r_node.r_id > 0);

          if constexpr (Reversible || is_lazy) {
            this->propagate(id);
            this->propagate(node.l_id);
            this->propagate(node.r_id);
            this->propagate(r_node.l_id);
            this->propagate(r_node.r_id);
          }

          node.r_id = r_node.l_id;
          r_node.l_id = node.id;

          this->fetch(id);
          this->fetch(r_node.id);

          return r_node.id;
        }
        int rotate_r(const int id) {
          auto& node = this->m_buffer->m_nodes[id];
          auto& l_node = this->m_buffer->m_nodes[node.l_id];

          assert(node.size > 1);
          assert(node.id > 0);
          assert(node.l_id > 0);
          assert(node.r_id > 0);
          assert(l_node.size > 1);
          assert(l_node.id > 0);
          assert(l_node.l_id > 0);
          assert(l_node.r_id > 0);

          if constexpr (Reversible || is_lazy) {
            this->propagate(id);
            this->propagate(node.l_id);
            this->propagate(node.r_id);
            this->propagate(l_node.l_id);
            this->propagate(l_node.r_id);
          }

          node.l_id = l_node.r_id;
          l_node.r_id = node.id;

          this->fetch(id);
          this->fetch(l_node.id);

          return l_node.id;
        }
        int height_diff(const int id) {
          const auto& node = this->m_buffer->m_nodes[id];
          const auto& l_node = this->m_buffer->m_nodes[node.l_id];
          const auto& r_node = this->m_buffer->m_nodes[node.r_id];

          return l_node.height - r_node.height;
        }
        int balance(const int id) {
          auto& node = this->m_buffer->m_nodes[id];

          const auto diff = this->height_diff(id);
          assert(::std::abs(diff) <= 2);

          if (diff == 2) {
            if (this->height_diff(node.l_id) < 0) node.l_id = this->rotate_l(node.l_id);
            return this->rotate_r(id);
          } else if (diff == -2) {
            if (this->height_diff(node.r_id) > 0) node.r_id = this->rotate_r(node.r_id);
            return this->rotate_l(id);
          } else {
            return id;
          }
        }

        void set(const int id, const int p, const S& x) {
          auto& node = this->m_buffer->m_nodes[id];

          assert(0 <= p && p < node.size);

          if constexpr (Reversible || is_lazy) {
            this->propagate(id);
          }
          if (node.size == 1) {
            node.prod = x;
          } else {
            const auto half = this->m_buffer->m_nodes[node.l_id].size;

            if (p < half) {
              this->set(node.l_id, p, x);
              if constexpr (Reversible || is_lazy) {
                this->propagate(node.r_id);
              }
            } else {
              if constexpr (Reversible || is_lazy) {
                this->propagate(node.l_id);
              }
              this->set(node.r_id, p - half, x);
            }
            this->fetch(id);
          }
        }
        S prod(const int id, const int l, const int r) {
          auto& node = this->m_buffer->m_nodes[id];

          assert(0 <= l && l <= r && r <= node.size);

          if (l == r) return SM::e();

          if constexpr (Reversible || is_lazy) {
            this->propagate(id);
          }
          if (l == 0 && r == node.size) {
            return node.prod;
          } else {
            const auto half = this->m_buffer->m_nodes[node.l_id].size;

            auto res = SM::e();
            if (l < half) res = SM::op(res, this->prod(node.l_id, l, ::std::min(r, half)));
            if (half < r) res = SM::op(res, this->prod(node.r_id, ::std::max(0, l - half), r - half));
            return res;
          }
        }
        template <bool SFINAE = is_lazy>
        ::std::enable_if_t<SFINAE, void> apply(const int id, const int l, const int r, const F& f) {
          auto& node = this->m_buffer->m_nodes[id];

          assert(0 <= l && l <= r && r <= node.size);

          if (l == r) return;

          if (l == 0 && r == node.size) {
            node.lazy = FM::op(f, node.lazy);
            this->propagate(id);
          } else {
            this->propagate(id);

            const auto half = this->m_buffer->m_nodes[node.l_id].size;
            if (l < half) {
              this->apply(node.l_id, l, ::std::min(r, half), f);
            } else {
              this->propagate(node.l_id);
            }
            if (half < r) {
              this->apply(node.r_id, ::std::max(0, l - half), r - half, f);
            } else {
              this->propagate(node.r_id);
            }
            this->fetch(id);
          }
        }
        int insert(const int id, const int p, const S& x) {
          auto& node = this->m_buffer->m_nodes[id];

          assert(0 <= p && p <= node.size);

          if constexpr (Reversible || is_lazy) {
            this->propagate(id);
          }
          if (node.size == 0) {
            return this->add_node(x);
          } else if (node.size == 1) {
            if (p == 0) {
              return this->add_node(this->add_node(x), id);
            } else {
              return this->add_node(id, this->add_node(x));
            }
          } else {
            const auto half = this->m_buffer->m_nodes[node.l_id].size;

            if (p < half) {
              if constexpr (Reversible || is_lazy) {
                this->propagate(node.r_id);
              }
              const auto l_id = this->insert(node.l_id, p, x);
              this->m_buffer->m_nodes[id].l_id = l_id;
            } else {
              if constexpr (Reversible || is_lazy) {
                this->propagate(node.l_id);
              }
              const auto r_id = this->insert(node.r_id, p - half, x);
              this->m_buffer->m_nodes[id].r_id = r_id;
            }
            this->fetch(id);
            return this->balance(id);
          }
        }
        int erase(const int id, const int p) {
          auto& node = this->m_buffer->m_nodes[id];

          assert(0 <= p && p < node.size);

          if constexpr (Reversible || is_lazy) {
            this->propagate(id);
          }
          if (node.size == 1) {
            return 0;
          } else {
            const auto half = this->m_buffer->m_nodes[node.l_id].size;

            if (p < half) {
              if constexpr (Reversible || is_lazy) {
                this->propagate(node.r_id);
              }
              node.l_id = this->erase(node.l_id, p);
              if (node.l_id == 0) return node.r_id;
            } else {
              if constexpr (Reversible || is_lazy) {
                this->propagate(node.l_id);
              }
              node.r_id = this->erase(node.r_id, p - half);
              if (node.r_id == 0) return node.l_id;
            }
            this->fetch(id);
            return this->balance(id);
          }
        }

        int merge(const int l_id, const int r_id, const int free_id) {
          if (l_id == 0) {
            if constexpr (Reversible || is_lazy) {
              this->propagate(r_id);
            }
            return r_id;
          }
          if (r_id == 0) {
            if constexpr (Reversible || is_lazy) {
              this->propagate(l_id);
            }
            return l_id;
          }

          auto& l_node = this->m_buffer->m_nodes[l_id];
          auto& r_node = this->m_buffer->m_nodes[r_id];
          const auto diff = l_node.height - r_node.height;
          if (diff >= 2) {
            if constexpr (Reversible || is_lazy) {
              this->propagate(l_id);
              this->propagate(l_node.l_id);
            }
            const auto merged_id = this->merge(l_node.r_id, r_id, free_id);
            this->m_buffer->m_nodes[l_id].r_id = merged_id;
            this->fetch(l_id);
            return this->balance(l_id);
          } else if (diff <= -2) {
            if constexpr (Reversible || is_lazy) {
              this->propagate(r_id);
              this->propagate(r_node.r_id);
            }
            const auto merged_id = this->merge(l_id, r_node.l_id, free_id);
            this->m_buffer->m_nodes[r_id].l_id = merged_id;
            this->fetch(r_id);
            return this->balance(r_id);
          } else {
            if constexpr (Reversible || is_lazy) {
              this->propagate(l_id);
              this->propagate(r_id);
            }
            if (free_id == 0) {
              return this->add_node(l_id, r_id);
            } else {
              auto& node = this->m_buffer->m_nodes[free_id];
              node.l_id = l_id;
              node.r_id = r_id;
              if constexpr (Reversible) {
                node.rev = false;
              }
              if constexpr (is_lazy) {
                node.lazy = FM::e();
              }
              this->fetch(free_id);
              return free_id;
            }
          }
        }
        ::std::pair<int, int> split(const int id, const int i) {
          auto& node = this->m_buffer->m_nodes[id];

          assert(0 <= i && i <= node.size);

          if (i == 0) return ::std::make_pair(0, id);
          if (i == node.size) return ::std::make_pair(id, 0);

          if constexpr (Reversible || is_lazy) {
            this->propagate(id);
          }
          const auto half = this->m_buffer->m_nodes[node.l_id].size;
          if (i < half) {
            const auto [l_id, r_id] = this->split(node.l_id, i);
            return ::std::make_pair(l_id, this->merge(r_id, this->m_buffer->m_nodes[id].r_id, this->m_buffer->m_nodes[id].l_id));
          } else if (i > half) {
            const auto [l_id, r_id] = this->split(node.r_id, i - half);
            return ::std::make_pair(this->merge(this->m_buffer->m_nodes[id].l_id, l_id, this->m_buffer->m_nodes[id].r_id), r_id);
          } else {
            return ::std::make_pair(node.l_id, node.r_id);
          }
        }

        template <typename G>
        ::std::pair<int, S> max_right(const int id, const int l, const G& g, S carry) {
          const auto& node = this->m_buffer->m_nodes[id];

          assert(0 <= l && l <= node.size);

          if constexpr (Reversible || is_lazy) {
            this->propagate(id);
          }
          if (node.size == 0) {
            return ::std::make_pair(0, carry);
          } else if (node.size == 1) {
            if (l == 0) {
              const auto whole = SM::op(carry, node.prod);
              if (g(whole)) return ::std::make_pair(1, whole);
              return ::std::make_pair(0, carry);
            } else {
              assert(carry == SM::e());
              return ::std::make_pair(1, carry);
            }
          } else {
            const auto half = this->m_buffer->m_nodes[node.l_id].size;
            int r;
            if (l == 0) {
              const auto whole = SM::op(carry, node.prod);
              if (g(whole)) return ::std::make_pair(node.size, whole);

              ::std::tie(r, carry) = this->max_right(node.l_id, 0, g, carry);
              if (r < half) return ::std::make_pair(r, carry);

              ::std::tie(r, carry) = this->max_right(node.r_id, 0, g, carry);
              r += half;
              return ::std::make_pair(r, carry);
            } else {
              assert(carry == SM::e());
              if (l < half) {
                ::std::tie(r, carry) = this->max_right(node.l_id, l, g, carry);
                if (r < half) return ::std::make_pair(r, carry);
              }
              ::std::tie(r, carry) = this->max_right(node.r_id, ::std::max(0, l - half), g, carry);
              r += half;
              return ::std::make_pair(r, carry);
            }
          }
        }
        template <typename G>
        ::std::pair<int, S> min_left(const int id, const int r, const G& g, S carry) {
          const auto& node = this->m_buffer->m_nodes[id];

          assert(0 <= r && r <= node.size);

          if constexpr (Reversible || is_lazy) {
            this->propagate(id);
          }
          if (node.size == 0) {
            return ::std::make_pair(0, carry);
          } else if (node.size == 1) {
            if (r == node.size) {
              const auto whole = SM::op(node.prod, carry);
              if (g(whole)) return ::std::make_pair(0, whole);
              return ::std::make_pair(1, carry);
            } else {
              assert(carry == SM::e());
              return ::std::make_pair(0, carry);
            }
          } else {
            const auto half = this->m_buffer->m_nodes[node.l_id].size;
            int l;
            if (r == node.size) {
              const auto whole = SM::op(node.prod, carry);
              if (g(whole)) return ::std::make_pair(0, whole);

              ::std::tie(l, carry) = this->min_left(node.r_id, node.size - half, g, carry);
              l += half;
              if (half < l) return ::std::make_pair(l, carry);

              ::std::tie(l, carry) = this->min_left(node.l_id, half, g, carry);
              return ::std::make_pair(l, carry);
            } else {
              assert(carry == SM::e());
              if (half < r) {
                ::std::tie(l, carry) = this->min_left(node.r_id, r - half, g, carry);
                l += half;
                if (half < l) return ::std::make_pair(l, carry);
              }
              ::std::tie(l, carry) = this->min_left(node.l_id, ::std::min(half, r), g, carry);
              return ::std::make_pair(l, carry);
            }
          }
        }

      public:
        explicit operator ::std::vector<S>() const {
          ::std::vector<S> v;
          if (!this->empty()) {
            ::tools::fix([&](auto&& dfs, const int id) -> void {
              auto& node = this->m_buffer->m_nodes[id];
              if constexpr (Reversible || is_lazy) {
                this->propagate(id);
              }
              if (node.size == 1) {
                v.push_back(node.prod);
              } else {
                dfs(node.l_id);
                dfs(node.r_id);
              }
            })(this->m_root_id);
          }
          return v;
        }

        avl_tree_impl() = default;
        explicit avl_tree_impl(buffer& buffer) : m_buffer(&buffer), m_root_id(0) {
        }
        avl_tree_impl(buffer& buffer, const ::std::vector<S>& v) : m_buffer(&buffer) {
          this->m_root_id = v.empty() ? 0 : ::tools::fix([&](auto&& dfs, const int l, const int r) -> int {
            if (r - l == 1) {
              return this->add_node(v[l]);
            } else {
              return this->add_node(dfs(l, (l + r) / 2), dfs((l + r) / 2, r));
            }
          })(0, v.size());
        }
        avl_tree_impl(buffer& buffer, const int n) : avl_tree_impl(buffer, ::std::vector<S>(n, SM::e())) {
        }
        avl_tree_impl(const avl_tree_impl<Reversible, SM, FM, mapping>& other) : avl_tree_impl(*other.m_buffer, static_cast<::std::vector<S>>(other)) {
        }
        avl_tree_impl(avl_tree_impl<Reversible, SM, FM, mapping>&& other) : m_buffer(other.m_buffer), m_root_id(other.m_root_id) {
        }
        ~avl_tree_impl() = default;
        avl_tree_impl<Reversible, SM, FM, mapping>& operator=(const avl_tree_impl<Reversible, SM, FM, mapping>& other) {
          this->m_buffer = other.m_buffer;
          this->m_root_id = avl_tree_impl<Reversible, SM, FM, mapping>(other).m_root_id;
        }
        avl_tree_impl<Reversible, SM, FM, mapping>& operator=(avl_tree_impl<Reversible, SM, FM, mapping>&& other) {
          this->m_buffer = other.m_buffer;
          this->m_root_id = other.m_root_id;
        }

        int size() const {
          return this->m_buffer->m_nodes[this->m_root_id].size;
        }
        bool empty() const {
          return this->m_root_id == 0;
        }

        void set(const int p, const S& x) {
          this->set(this->m_root_id, p, x);
        }
        S get(const int p) {
          return this->prod(this->m_root_id, p, p + 1);
        }
        S prod(const int l, const int r) {
          return this->prod(this->m_root_id, l, r);
        }
        S all_prod() {
          return this->prod(this->m_root_id, 0, this->size());
        }
        template <bool SFINAE = is_lazy>
        ::std::enable_if_t<SFINAE, void> apply(const int p, const F& f) {
          this->apply(this->m_root_id, p, p + 1, f);
        }
        template <bool SFINAE = is_lazy>
        ::std::enable_if_t<SFINAE, void> apply(const int l, const int r, const F& f) {
          this->apply(this->m_root_id, l, r, f);
        }
        void insert(const int p, const S& x) {
          this->m_root_id = this->insert(this->m_root_id, p, x);
        }
        void erase(const int p) {
          this->m_root_id = this->erase(this->m_root_id, p);
        }
        void merge(avl_tree_impl<Reversible, SM, FM, mapping>& other) {
          assert(this->m_buffer == other.m_buffer);
          this->m_root_id = this->merge(this->m_root_id, other.m_root_id, 0);
          other.m_root_id = 0;
        }
        ::std::pair<avl_tree_impl<Reversible, SM, FM, mapping>, avl_tree_impl<Reversible, SM, FM, mapping>> split(const int i) {
          avl_tree_impl<Reversible, SM, FM, mapping> l(*this->m_buffer), r(*this->m_buffer);
          ::std::tie(l.m_root_id, r.m_root_id) = this->split(this->m_root_id, i);
          return ::std::make_pair(l, r);
        }
        template <bool SFINAE = Reversible>
        ::std::enable_if_t<SFINAE, void> reverse(const int l, const int r) {
          assert(0 <= l && l <= r && r <= this->size());

          if (l == r) return;

          if (l == 0) {
            if (r == this->size()) {
              this->m_buffer->m_nodes[this->m_root_id].rev = !this->m_buffer->m_nodes[this->m_root_id].rev;
            } else {
              const auto [l_id, r_id] = this->split(this->m_root_id, r);
              this->m_buffer->m_nodes[l_id].rev = !this->m_buffer->m_nodes[l_id].rev;
              this->m_root_id = this->merge(l_id, r_id, this->m_root_id);
            }
          } else {
            if (r == this->size()) {
              const auto [l_id, r_id] = this->split(this->m_root_id, l);
              this->m_buffer->m_nodes[r_id].rev = !this->m_buffer->m_nodes[r_id].rev;
              this->m_root_id = this->merge(l_id, r_id, this->m_root_id);
            } else {
              const auto [lm_id, r_id] = this->split(this->m_root_id, r);
              const auto [l_id, m_id] = this->split(lm_id, l);
              this->m_buffer->m_nodes[m_id].rev = !this->m_buffer->m_nodes[m_id].rev;
              this->m_root_id = this->merge(this->merge(l_id, m_id, lm_id), r_id, this->m_root_id);
            }
          }
        }
        template <typename G>
        int max_right(const int l, const G& g) {
          return this->max_right(this->m_root_id, l, g, SM::e()).first;
        }
        template <typename G>
        int min_left(const int r, const G& g) {
          return this->min_left(this->m_root_id, r, g, SM::e()).first;
        }
      };
    }
  }
}


#line 5 "tools/avl_tree.hpp"

namespace tools {
  template <typename SM, bool Reversible = false>
  using avl_tree = ::tools::detail::avl_tree::avl_tree_impl<Reversible, SM>;
}


#line 9 "tests/avl_tree/set.test.cpp"

using mint = atcoder::modint998244353;

using S = std::pair<mint, mint>;
struct SM {
  using T = S;
  static T op(const T& x, const T& y) {
    return T(y.first * x.first, y.first * x.second + y.second);
  }
  static T e() {
    return T(mint::raw(1), mint::raw(0));
  }
};

int main() {
  std::cin.tie(nullptr);
  std::ios_base::sync_with_stdio(false);

  int N, Q;
  std::cin >> N >> Q;
  std::vector<S> init;
  init.reserve(N);
  for (int i = 0; i < N; ++i) {
    int a, b;
    std::cin >> a >> b;
    init.emplace_back(mint::raw(a), mint::raw(b));
  }

  tools::avl_tree<SM>::buffer buffer;
  tools::avl_tree<SM> avl_tree(buffer, init);
  for (int q = 0; q < Q; ++q) {
    int t;
    std::cin >> t;
    if (t == 0) {
      int p, c, d;
      std::cin >> p >> c >> d;
      avl_tree.set(p, S(mint::raw(c), mint::raw(d)));
    } else {
      int l, r, x;
      std::cin >> l >> r >> x;
      const auto [a, b] = avl_tree.prod(l, r);
      std::cout << (a * mint::raw(x) + b).val() << '\n';
    }
  }

  return 0;
}

Test cases

Env Name Status Elapsed Memory
g++ example_00 :heavy_check_mark: AC 5 ms 3 MB
g++ max_random_00 :heavy_check_mark: AC 487 ms 41 MB
g++ max_random_01 :heavy_check_mark: AC 487 ms 41 MB
g++ max_random_02 :heavy_check_mark: AC 489 ms 42 MB
g++ max_random_03 :heavy_check_mark: AC 483 ms 41 MB
g++ max_random_04 :heavy_check_mark: AC 495 ms 42 MB
g++ random_00 :heavy_check_mark: AC 376 ms 41 MB
g++ random_01 :heavy_check_mark: AC 412 ms 40 MB
g++ random_02 :heavy_check_mark: AC 244 ms 9 MB
g++ random_03 :heavy_check_mark: AC 96 ms 40 MB
g++ random_04 :heavy_check_mark: AC 124 ms 39 MB
g++ small_00 :heavy_check_mark: AC 5 ms 4 MB
g++ small_01 :heavy_check_mark: AC 4 ms 4 MB
g++ small_02 :heavy_check_mark: AC 4 ms 4 MB
g++ small_03 :heavy_check_mark: AC 4 ms 3 MB
g++ small_04 :heavy_check_mark: AC 4 ms 4 MB
Back to top page