This documentation is automatically generated by online-judge-tools/verification-helper
#define PROBLEM "https://atcoder.jp/contests/abc179/tasks/abc179_d"
#include <iostream>
#include <vector>
#include "../../../include/algebra/static_modint.hpp"
#include "../../../include/data_structure/binary_indexed_tree.hpp"
int main() {
int N, K;
std::cin >> N >> K;
std::vector<int> L(K), R(K);
for (int i = 0; i < K; ++i)
std::cin >> L[i] >> R[i];
lib::binary_indexed_tree<lib::static_modint<998244353>> tree(N);
tree.add(0, 1);
for (int i = 0; i < N; ++i) {
const auto current = tree.get(i);
for (int j = 0; j < K; ++j) {
const int l = i + L[j];
const int r = std::min(N, i + 1 + R[j]);
if (l < N)
tree.uniform_add(l, r, current);
}
}
std::cout << tree.get(N - 1) << '\n';
}
#line 1 "test/data_structure/binary_indexed_tree/2.test.cpp"
#define PROBLEM "https://atcoder.jp/contests/abc179/tasks/abc179_d"
#include <iostream>
#include <vector>
#line 1 "include/algebra/static_modint.hpp"
//! @file static_modint.hpp
#ifndef CP_LIBRARY_STATIC_MODINT_HPP
#define CP_LIBRARY_STATIC_MODINT_HPP
#include <cassert>
#include <cstdint>
#line 10 "include/algebra/static_modint.hpp"
#include <limits>
#include <type_traits>
#define CP_LIBRARY_USE_CONSTEXPR
#ifndef CP_LIBRARY_WARN
# if (CP_LIBRARY_DEBUG_LEVEL >= 1)
//! @brief Print warning message
//! @note You can suppress the warning by uncommenting the following line
# define CP_LIBRARY_WARN(msg) (std::cerr << (msg) << '\n')
// # define CP_LIBRARY_WARN(msg) (static_cast<void>(0))
# else
# define CP_LIBRARY_WARN(msg) (static_cast<void>(0))
# undef CP_LIBRARY_USE_CONSTEXPR
# define CP_LIBRARY_USE_CONSTEXPR constexpr
# endif
# define CP_LIBRARY_WARN_NOT_DEFINED
#endif
#ifndef CP_LIBRARY_ASSERT
//! @brief Assert macro
# define CP_LIBRARY_ASSERT(...) assert(__VA_ARGS__)
# define CP_LIBRARY_ASSERT_NOT_DEFINED
#endif
namespace lib {
//! @brief modint (for compile-time constant modulo)
//! @tparam modulo modulo (e.g. 1000000007).
template <std::int_least32_t modulo,
std::enable_if_t<(1 < modulo) && (modulo < std::numeric_limits<std::int_least32_t>::max() / 2),
std::nullptr_t> = nullptr>
struct static_modint {
private:
std::int_least32_t value;
//! @param n non-zero integer
//! @return multiplicative inverse of n
template <typename Tp>
[[nodiscard]] static std::int_least32_t calc_inverse(Tp n) {
CP_LIBRARY_ASSERT(n != 0);
Tp b = modulo, u = 1, v = 0, t;
while (b > 0) {
t = n / b;
// std::swap is not necessarily constexpr in C++17
// std::swap(n -= t * b, b);
Tp tmp = std::move(n -= t * b);
n = std::move(b);
b = std::move(tmp);
// std::swap(u -= t * v, v);
tmp = std::move(u -= t * v);
u = std::move(v);
v = std::move(tmp);
}
if (u < 0)
u += modulo;
return static_cast<std::int_least32_t>(u);
}
//! @brief Calculate modulo and keep the value within [0, modulo)
//! @param v integer
//! @return integer within [0, modulo)
//! @note Time complexity: O(1)
template <typename Tp>
[[nodiscard]] static constexpr std::int_least32_t clamp_ll(Tp v) noexcept {
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wsign-compare"
if (modulo <= v || v < -modulo)
v %= modulo;
#pragma GCC diagnostic pop
if (v < 0)
v += modulo;
return static_cast<std::int_least32_t>(v);
}
//! @brief Calculate modulo and keep the value within [0, modulo)
//! @note Time complexity: O(1)
constexpr void clamp_self() noexcept {
if (0 <= value) {
if (value < modulo)
return;
if (value < modulo * 2)
value -= modulo;
else
value -= modulo * 2;
} else {
if (-modulo < value)
value += modulo;
else if (-modulo * 2 < value)
value += modulo * 2;
else {
value += modulo;
value += modulo * 2;
}
}
}
public:
//! @brief underlying integer type
using type = std::int_least32_t;
//! @return modulo (e.g. 1000000007)
[[nodiscard]] static constexpr type mod() noexcept {
return modulo;
}
//! @brief Create a modint of value 0
constexpr static_modint() noexcept : value(0) {}
//! @brief Create a modint without taking modulo
constexpr static_modint(const type v, bool) noexcept : value(v) {}
//! @brief Create a modint
template <typename ValueType>
constexpr static_modint(const ValueType v) noexcept : value() {
if constexpr (std::is_integral_v<ValueType> && (std::numeric_limits<ValueType>::digits <= 32)) {
value = v;
clamp_self();
} else {
value = clamp_ll(v);
}
}
[[nodiscard]] constexpr static_modint operator+(const static_modint rhs) const noexcept {
return static_modint(value + rhs.value);
}
[[nodiscard]] constexpr static_modint operator-(const static_modint rhs) const noexcept {
return static_modint(value - rhs.value);
}
[[nodiscard]] constexpr static_modint operator*(const static_modint rhs) const noexcept {
return static_modint(static_cast<std::int_least64_t>(value) * rhs.value);
}
[[nodiscard]] static_modint operator/(const static_modint rhs) const {
return static_modint(static_cast<std::int_least64_t>(value) * calc_inverse(rhs.value));
}
[[nodiscard]] CP_LIBRARY_USE_CONSTEXPR static_modint operator%(const static_modint rhs) const {
CP_LIBRARY_WARN("static_modint::operator% : Are you sure you want to do this?");
return static_modint(value % rhs.value);
}
[[nodiscard]] CP_LIBRARY_USE_CONSTEXPR static_modint operator&(const static_modint rhs) const {
CP_LIBRARY_WARN("static_modint::operator& : Are you sure you want to do this?");
return static_modint(value & rhs.value, true);
}
[[nodiscard]] CP_LIBRARY_USE_CONSTEXPR static_modint operator|(const static_modint rhs) const {
CP_LIBRARY_WARN("static_modint::operator| : Are you sure you want to do this?");
return static_modint(value | rhs.value);
}
[[nodiscard]] CP_LIBRARY_USE_CONSTEXPR static_modint operator^(const static_modint rhs) const {
CP_LIBRARY_WARN("static_modint::operator^ : Are you sure you want to do this?");
return static_modint(value ^ rhs.value);
}
[[nodiscard]] CP_LIBRARY_USE_CONSTEXPR static_modint operator<<(const static_modint rhs) const {
CP_LIBRARY_WARN("static_modint::operator<< : Are you sure you want to do this?");
return static_modint(static_cast<std::int_least64_t>(value) << rhs.value);
}
[[nodiscard]] CP_LIBRARY_USE_CONSTEXPR static_modint operator>>(const static_modint rhs) const {
CP_LIBRARY_WARN("static_modint::operator>> : Are you sure you want to do this?");
return static_modint(value >> rhs.value, true);
}
constexpr static_modint& operator+=(const static_modint rhs) noexcept {
value += rhs.value;
if (value >= modulo)
value -= modulo;
return *this;
}
constexpr static_modint& operator-=(const static_modint rhs) noexcept {
value -= rhs.value;
if (value < 0)
value += modulo;
return *this;
}
constexpr static_modint& operator*=(const static_modint rhs) noexcept {
value = clamp_ll(static_cast<std::int_least64_t>(value) * rhs.value);
return *this;
}
static_modint& operator/=(const static_modint rhs) {
value = clamp_ll(static_cast<std::int_least64_t>(value) * calc_inverse(rhs.value));
return *this;
}
CP_LIBRARY_USE_CONSTEXPR static_modint& operator%=(const static_modint rhs) {
CP_LIBRARY_WARN("static_modint::operator%= : Are you sure you want to do this?");
value %= rhs.value;
if (value < 0)
value += modulo;
return *this;
}
CP_LIBRARY_USE_CONSTEXPR static_modint& operator&=(const static_modint rhs) {
CP_LIBRARY_WARN("static_modint::operator&= : Are you sure you want to do this?");
value &= rhs.value;
return *this;
}
CP_LIBRARY_USE_CONSTEXPR static_modint& operator|=(const static_modint rhs) {
CP_LIBRARY_WARN("static_modint::operator|= : Are you sure you want to do this?");
value |= rhs.value;
clamp_self();
return *this;
}
CP_LIBRARY_USE_CONSTEXPR static_modint& operator^=(const static_modint rhs) {
CP_LIBRARY_WARN("static_modint::operator^= : Are you sure you want to do this?");
value ^= rhs.value;
clamp_self();
return *this;
}
CP_LIBRARY_USE_CONSTEXPR static_modint& operator<<=(const static_modint rhs) {
CP_LIBRARY_WARN("operator<<= : Are you sure you want to do this?");
value = clamp_ll(static_cast<std::int_least64_t>(value) << rhs.value);
return *this;
}
CP_LIBRARY_USE_CONSTEXPR static_modint& operator>>=(const static_modint rhs) {
CP_LIBRARY_WARN("operator>>= : Are you sure you want to do this?");
value >>= rhs.value;
return *this;
}
template <typename RhsType>
[[nodiscard]] constexpr static_modint operator+(const RhsType rhs) const noexcept {
return static_modint(value + clamp_ll(rhs));
}
template <typename RhsType>
[[nodiscard]] constexpr static_modint operator-(const RhsType rhs) const noexcept {
return static_modint(value - clamp_ll(rhs));
}
template <typename RhsType>
[[nodiscard]] constexpr static_modint operator*(const RhsType rhs) const noexcept {
return static_modint(static_cast<std::int_least64_t>(value) * clamp_ll(rhs));
}
template <typename RhsType>
[[nodiscard]] static_modint operator/(const RhsType rhs) const {
std::int_least64_t mul = (rhs > 0) ? calc_inverse(rhs) : -calc_inverse(-rhs);
return static_modint(value * mul);
}
template <typename RhsType>
[[nodiscard]] CP_LIBRARY_USE_CONSTEXPR static_modint operator%(const RhsType rhs) const {
CP_LIBRARY_WARN("static_modint::operator% : Are you sure you want to do this?");
return static_modint(value % rhs, true);
}
template <typename RhsType>
[[nodiscard]] CP_LIBRARY_USE_CONSTEXPR static_modint operator&(const RhsType rhs) const {
CP_LIBRARY_WARN("static_modint::operator& : Are you sure you want to do this?");
return static_modint(value & rhs, true);
}
template <typename RhsType>
[[nodiscard]] CP_LIBRARY_USE_CONSTEXPR static_modint operator|(const RhsType rhs) const {
CP_LIBRARY_WARN("static_modint::operator| : Are you sure you want to do this?");
return static_modint(value | rhs);
}
template <typename RhsType>
[[nodiscard]] CP_LIBRARY_USE_CONSTEXPR static_modint operator^(const RhsType rhs) const {
CP_LIBRARY_WARN("static_modint::operator^ : Are you sure you want to do this?");
return static_modint(value ^ rhs);
}
template <typename RhsType>
[[nodiscard]] CP_LIBRARY_USE_CONSTEXPR static_modint operator<<(const RhsType rhs) const {
CP_LIBRARY_WARN("static_modint::operator<< : Are you sure you want to do this?");
return static_modint(static_cast<std::int_least64_t>(value) << rhs);
}
template <typename RhsType>
[[nodiscard]] CP_LIBRARY_USE_CONSTEXPR static_modint operator>>(const RhsType rhs) const {
CP_LIBRARY_WARN("static_modint::operator>> : Are you sure you want to do this?");
return static_modint(value >> rhs, true);
}
template <typename RhsType>
constexpr static_modint& operator+=(const RhsType rhs) noexcept {
value = clamp_ll(static_cast<std::int_least64_t>(value) + rhs);
return *this;
}
template <typename RhsType>
constexpr static_modint& operator-=(const RhsType rhs) noexcept {
value = clamp_ll(static_cast<std::int_least64_t>(value) - rhs);
return *this;
}
template <typename RhsType>
constexpr static_modint& operator*=(const RhsType rhs) noexcept {
value = clamp_ll(static_cast<std::int_least64_t>(value) * clamp_ll(rhs));
return *this;
}
template <typename RhsType>
static_modint& operator/=(const RhsType rhs) {
std::int_least64_t mul = (rhs > 0) ? calc_inverse(rhs) : -calc_inverse(-rhs);
value = clamp_ll(value * mul);
return *this;
}
template <typename RhsType>
CP_LIBRARY_USE_CONSTEXPR static_modint& operator%=(const RhsType rhs) {
CP_LIBRARY_WARN("static_modint::operator%= : Are you sure you want to do this?");
value %= rhs;
return *this;
}
template <typename RhsType>
CP_LIBRARY_USE_CONSTEXPR static_modint& operator&=(const RhsType rhs) {
CP_LIBRARY_WARN("static_modint::operator&= : Are you sure you want to do this?");
value &= rhs;
return *this;
}
template <typename RhsType>
CP_LIBRARY_USE_CONSTEXPR static_modint& operator|=(const RhsType rhs) {
CP_LIBRARY_WARN("static_modint::operator|= : Are you sure you want to do this?");
value |= rhs;
clamp_self();
return *this;
}
template <typename RhsType>
CP_LIBRARY_USE_CONSTEXPR static_modint& operator^=(const RhsType rhs) {
CP_LIBRARY_WARN("static_modint::operator^= : Are you sure you want to do this?");
value ^= rhs;
clamp_self();
return *this;
}
template <typename RhsType>
CP_LIBRARY_USE_CONSTEXPR static_modint& operator<<=(const RhsType rhs) {
CP_LIBRARY_WARN("operator<<= : Are you sure you want to do this?");
value = clamp_ll(static_cast<std::int_least64_t>(value) << rhs);
return *this;
}
template <typename RhsType>
CP_LIBRARY_USE_CONSTEXPR static_modint& operator>>=(const RhsType rhs) {
CP_LIBRARY_WARN("operator>>= : Are you sure you want to do this?");
value >>= rhs;
return *this;
}
[[nodiscard]] CP_LIBRARY_USE_CONSTEXPR bool operator!() const {
CP_LIBRARY_WARN("static_modint::operator! : Are you sure you want to do this?");
return value == 0;
}
[[nodiscard]] CP_LIBRARY_USE_CONSTEXPR static_modint operator~() const {
CP_LIBRARY_WARN("static_modint::operator~ : Are you sure you want to do this?");
return static_modint(~value);
}
[[nodiscard]] constexpr static_modint operator-() const noexcept {
return static_modint(value == 0 ? 0 : modulo - value, true);
}
[[nodiscard]] constexpr static_modint& operator+() const noexcept {
return *this;
}
constexpr static_modint& operator++() noexcept {
value = ((value + 1 == modulo) ? 0 : value + 1);
return *this;
}
constexpr static_modint& operator--() noexcept {
value = ((value == 0) ? modulo - 1 : value - 1);
return *this;
}
constexpr static_modint operator++(int) noexcept {
std::int_least32_t res = value;
++(*this);
return static_modint(res, true);
}
constexpr static_modint operator--(int) noexcept {
std::int_least32_t res = value;
--(*this);
return static_modint(res, true);
}
[[nodiscard]] constexpr bool operator==(const static_modint rhs) const noexcept {
return value == rhs.value;
}
[[nodiscard]] constexpr bool operator!=(const static_modint rhs) const noexcept {
return value != rhs.value;
}
[[nodiscard]] CP_LIBRARY_USE_CONSTEXPR bool operator<(const static_modint rhs) const {
CP_LIBRARY_WARN("static_modint::operator< : Are you sure you want to do this?");
return value < rhs.value;
}
[[nodiscard]] CP_LIBRARY_USE_CONSTEXPR bool operator<=(const static_modint rhs) const {
CP_LIBRARY_WARN("static_modint::operator<= : Are you sure you want to do this?");
return value <= rhs.value;
}
[[nodiscard]] CP_LIBRARY_USE_CONSTEXPR bool operator>(const static_modint rhs) const {
CP_LIBRARY_WARN("static_modint::operator> : Are you sure you want to do this?");
return value > rhs.value;
}
[[nodiscard]] CP_LIBRARY_USE_CONSTEXPR bool operator>=(const static_modint rhs) const {
CP_LIBRARY_WARN("static_modint::operator>= : Are you sure you want to do this?");
return value >= rhs.value;
}
template <typename RhsType>
[[nodiscard]] constexpr bool operator==(const RhsType rhs) const noexcept {
return value == rhs;
}
template <typename RhsType>
[[nodiscard]] constexpr bool operator!=(const RhsType rhs) const noexcept {
return value != rhs;
}
template <typename RhsType>
[[nodiscard]] CP_LIBRARY_USE_CONSTEXPR bool operator<(const RhsType rhs) const {
CP_LIBRARY_WARN("static_modint::operator< : Are you sure you want to do this?");
return value < rhs;
}
template <typename RhsType>
[[nodiscard]] CP_LIBRARY_USE_CONSTEXPR bool operator<=(const RhsType rhs) const {
CP_LIBRARY_WARN("static_modint::operator<= : Are you sure you want to do this?");
return value <= rhs;
}
template <typename RhsType>
[[nodiscard]] CP_LIBRARY_USE_CONSTEXPR bool operator>(const RhsType rhs) const {
CP_LIBRARY_WARN("static_modint::operator> : Are you sure you want to do this?");
return value > rhs;
}
template <typename RhsType>
[[nodiscard]] CP_LIBRARY_USE_CONSTEXPR bool operator>=(const RhsType rhs) const {
CP_LIBRARY_WARN("static_modint::operator>= : Are you sure you want to do this?");
return value >= rhs;
}
[[nodiscard]] constexpr operator std::int_least32_t() const {
CP_LIBRARY_WARN("A value of type static_modint has been cast to type std::int_lease32_t.");
return value;
}
//! @brief Read value (64-bit signed integer) from std::istream& is, take modulo, and store it in rhs.
//! @return std::istream& is
friend std::istream& operator>>(std::istream& is, static_modint& rhs) {
std::int_least64_t tmp;
is >> tmp;
if (tmp < -modulo || modulo <= tmp)
tmp %= modulo;
if (tmp < 0)
tmp += modulo;
rhs.value = static_cast<std::int_least32_t>(tmp);
return is;
}
//! @brief Print value to std::ostream& os
//! @return std::ostream& os
friend std::ostream& operator<<(std::ostream& os, static_modint& rhs) {
return os << rhs.value;
}
//! @return multiplicative inverse
[[nodiscard]] static_modint inv() const {
return static_modint(calc_inverse(value), true);
}
//! @tparam index_positive_guaranteed set true if and only if you can promise that index is positive
//! @tparam Tp integer type (deduced from parameter)
//! @param index index. This must be an integer, but doesn't have to be primitive.
//! @return index-th power of the value
//! @note Time complexity: O(log(index))
template <bool index_positive_guaranteed = true, typename Tp = std::int_least32_t>
[[nodiscard]] static_modint pow(Tp index) const noexcept {
if constexpr (!index_positive_guaranteed) {
if (value == 0)
return static_modint(0, true);
if (index == 0)
return static_modint(1, true);
if (index < 0)
return static_modint(value, true).inv().pow<true>(-index);
}
static_modint res(1, true), base(value, true);
while (index > 0) {
if ((index & 1) == 1)
res *= base;
base *= base;
index >>= 1;
}
return res;
}
//! @return a pair (a, b) such that b > 0, value is equal to a * (mult inverse of b), and (a + b) is minimal
[[nodiscard]] constexpr std::pair<std::int_least32_t, std::int_least32_t> to_frac() const noexcept {
std::int_least32_t x = modulo - value, y = value, u = 1, v = 1;
std::pair<std::int_least32_t, std::int_least32_t> res {value, 1};
std::int_least32_t num = value, den = 1;
std::int_least32_t cost = num + den;
while (x > 0) {
if (x <= num) {
std::int_least32_t q = num / x;
num = num % x;
den += q * u;
if (num == 0)
break;
if (num + den < cost) {
cost = num + den;
res.first = num;
res.second = den;
}
}
std::int_least32_t q = y / x;
y = y % x;
v += q * u;
q = x / y;
x = x % y;
u += q * v;
}
return res;
}
};
template <typename LhsType, std::int_least32_t modulo>
[[nodiscard]] constexpr static_modint<modulo> operator+(const LhsType lhs, const static_modint<modulo> rhs) noexcept {
return rhs + lhs;
}
template <typename LhsType, std::int_least32_t modulo>
[[nodiscard]] constexpr static_modint<modulo> operator-(const LhsType lhs, const static_modint<modulo> rhs) noexcept {
return -rhs + lhs;
}
template <typename LhsType, std::int_least32_t modulo>
[[nodiscard]] constexpr static_modint<modulo> operator*(const LhsType lhs, const static_modint<modulo> rhs) noexcept {
return rhs * lhs;
}
template <typename LhsType, std::int_least32_t modulo>
[[nodiscard]] static_modint<modulo> operator/(const LhsType lhs, const static_modint<modulo> rhs) {
return rhs.inv() * lhs;
}
template <typename LhsType, std::int_least32_t modulo>
[[nodiscard]] CP_LIBRARY_USE_CONSTEXPR static_modint<modulo>
operator%(const LhsType lhs, const static_modint<modulo> rhs) {
CP_LIBRARY_WARN("static_modint::operator% : Are you sure you want to do this?");
return static_modint<modulo>(lhs % static_cast<std::int_least32_t>(rhs), true);
}
template <typename LhsType, std::int_least32_t modulo, std::enable_if_t<std::is_integral_v<LhsType>, std::nullptr_t> = nullptr>
[[nodiscard]] CP_LIBRARY_USE_CONSTEXPR static_modint<modulo>
operator<<(const LhsType lhs, const static_modint<modulo> rhs) {
CP_LIBRARY_WARN("static_modint::operator<< : Are you sure you want to do this?");
return static_modint<modulo>(static_cast<std::int_least64_t>(lhs) << static_cast<std::int_least32_t>(rhs));
}
template <typename LhsType, std::int_least32_t modulo, std::enable_if_t<std::is_integral_v<LhsType>, std::nullptr_t> = nullptr>
[[nodiscard]] CP_LIBRARY_USE_CONSTEXPR static_modint<modulo>
operator>>(const LhsType lhs, const static_modint<modulo> rhs) {
CP_LIBRARY_WARN("static_modint::operator>> : Are you sure you want to do this?");
return static_modint<modulo>(lhs >> static_cast<std::int_least32_t>(rhs));
}
template <typename LhsType, std::int_least32_t modulo>
constexpr LhsType& operator+=(LhsType& lhs, const static_modint<modulo> rhs) noexcept {
return lhs += static_cast<std::int_least32_t>(rhs);
}
template <typename LhsType, std::int_least32_t modulo>
constexpr LhsType& operator-=(LhsType& lhs, const static_modint<modulo> rhs) noexcept {
return lhs -= static_cast<std::int_least32_t>(rhs);
}
template <typename LhsType, std::int_least32_t modulo>
constexpr LhsType& operator*=(LhsType& lhs, const static_modint<modulo> rhs) noexcept {
return lhs *= static_cast<std::int_least32_t>(rhs);
}
template <typename LhsType, std::int_least32_t modulo>
constexpr LhsType& operator/=(LhsType& lhs, const static_modint<modulo> rhs) {
return lhs /= static_cast<std::int_least32_t>(rhs);
}
template <typename LhsType, std::int_least32_t modulo>
CP_LIBRARY_USE_CONSTEXPR LhsType& operator%=(LhsType& lhs, const static_modint<modulo> rhs) {
CP_LIBRARY_WARN("static_modint::operator%= : Are you sure you want to do this?");
return lhs %= static_cast<std::int_least32_t>(rhs);
}
template <typename LhsType, std::int_least32_t modulo>
CP_LIBRARY_USE_CONSTEXPR LhsType& operator&=(LhsType& lhs, const static_modint<modulo> rhs) {
CP_LIBRARY_WARN("static_modint::operator&= : Are you sure you want to do this?");
return lhs &= static_cast<std::int_least32_t>(rhs);
}
template <typename LhsType, std::int_least32_t modulo>
CP_LIBRARY_USE_CONSTEXPR LhsType& operator|=(LhsType& lhs, const static_modint<modulo> rhs) {
CP_LIBRARY_WARN("static_modint::operator|= : Are you sure you want to do this?");
return lhs |= static_cast<std::int_least32_t>(rhs);
}
template <typename LhsType, std::int_least32_t modulo>
CP_LIBRARY_USE_CONSTEXPR LhsType& operator^=(LhsType& lhs, const static_modint<modulo> rhs) {
CP_LIBRARY_WARN("static_modint::operator^= : Are you sure you want to do this?");
return lhs ^= static_cast<std::int_least32_t>(rhs);
}
template <typename LhsType, std::int_least32_t modulo>
CP_LIBRARY_USE_CONSTEXPR LhsType& operator<<=(LhsType& lhs, const static_modint<modulo> rhs) {
CP_LIBRARY_WARN("operator<<= : Are you sure you want to do this?");
return lhs <<= static_cast<std::int_least32_t>(rhs);
}
template <typename LhsType, std::int_least32_t modulo>
CP_LIBRARY_USE_CONSTEXPR LhsType& operator>>=(LhsType& lhs, const static_modint<modulo> rhs) {
CP_LIBRARY_WARN("operator>>= : Are you sure you want to do this?");
return lhs >>= static_cast<std::int_least32_t>(rhs);
}
template <typename LhsType, std::int_least32_t modulo>
[[nodiscard]] CP_LIBRARY_USE_CONSTEXPR bool operator<(const LhsType lhs, const static_modint<modulo> rhs) {
CP_LIBRARY_WARN("static_modint::operator< : Are you sure you want to do this?");
return lhs < static_cast<std::int_least32_t>(rhs);
}
template <typename LhsType, std::int_least32_t modulo>
[[nodiscard]] CP_LIBRARY_USE_CONSTEXPR bool operator<=(const LhsType lhs, const static_modint<modulo> rhs) {
CP_LIBRARY_WARN("static_modint::operator<= : Are you sure you want to do this?");
return lhs < static_cast<std::int_least32_t>(rhs);
}
template <typename LhsType, std::int_least32_t modulo>
[[nodiscard]] CP_LIBRARY_USE_CONSTEXPR bool operator>(const LhsType lhs, const static_modint<modulo> rhs) {
CP_LIBRARY_WARN("static_modint::operator> : Are you sure you want to do this?");
return lhs < static_cast<std::int_least32_t>(rhs);
}
template <typename LhsType, std::int_least32_t modulo>
[[nodiscard]] CP_LIBRARY_USE_CONSTEXPR bool operator>=(const LhsType lhs, const static_modint<modulo> rhs) {
CP_LIBRARY_WARN("static_modint::operator>= : Are you sure you want to do this?");
return lhs < static_cast<std::int_least32_t>(rhs);
}
} // namespace lib
#undef CP_LIBRARY_USE_CONSTEXPR
#ifdef CP_LIBRARY_WARN_NOT_DEFINED
# undef CP_LIBRARY_WARN
# undef CP_LIBRARY_WARN_NOT_DEFINED
# ifdef CP_LIBRARY_WARN
# undef CP_LIBRARY_WARN
# endif
#endif
#ifdef CP_LIBRARY_ASSERT_NOT_DEFINED
# undef CP_LIBRARY_ASSERT
# undef CP_LIBRARY_ASSERT_NOT_DEFINED
#endif
#endif // CP_LIBRARY_STATIC_MODINT_HPP
#line 1 "include/data_structure/binary_indexed_tree.hpp"
//! @file binary_indexed_tree.hpp
#ifndef CP_LIBRARY_BINARY_INDEXED_TREE_HPP
#define CP_LIBRARY_BINARY_INDEXED_TREE_HPP
#include <cassert>
#line 9 "include/data_structure/binary_indexed_tree.hpp"
#include <string>
#line 11 "include/data_structure/binary_indexed_tree.hpp"
#ifndef CP_LIBRARY_ASSERT
//! @brief Assert macro
# define CP_LIBRARY_ASSERT(...) assert(__VA_ARGS__)
# define CP_LIBRARY_ASSERT_NOT_DEFINED
#endif
namespace lib {
namespace internal::binary_indexed_tree_hpp {
//! @brief Normal binary indexed tree.
//! @tparam Elem Element type. Watch out for overflows.
template <typename Elem>
class binary_indexed_tree_impl {
private:
int length;
std::vector<Elem> data;
//! @return Sum of the elements within [0, index) (0-indexed, half-open interval)
[[nodiscard]] Elem partial_sum(int index) const {
Elem res = 0;
for (; index > 0; index -= (index & -index))
res += data[index];
return res;
}
public:
//! @brief Construct a vector of n zeroes.
//! @param n vector size
explicit binary_indexed_tree_impl(const int n) : length(n), data(n + 1, (Elem) 0) {}
//! @brief Construct a vector from an existing container.
//! @tparam Container container type (deduced from parameter).
//! @param src Source (container)
template <typename Container>
explicit binary_indexed_tree_impl(const Container& src)
: length(static_cast<int>(std::size(src))), data(length + 1, (Elem) 0) {
for (int i = 0; i < length; ++i)
add(i, src[i]);
}
//! @brief Construct a vector of length n filled with initial_values.
//! @param n vector size
//! @param initial_value initial value for all elements
binary_indexed_tree_impl(const int n, const Elem& initial_value) : length(n), data(n + 1, (Elem) 0) {
for (int i = 0; i < length; ++i)
add(i, initial_value);
}
//! @return Vector length
[[nodiscard]] int size() const noexcept {
return length;
}
//! @brief Add value to the index-th element.
//! @param index index of the element to be added (0-indexed)
//! @param value value to be added
//! @note Time complexity: O(log size)
void add(int index, const Elem& value) {
CP_LIBRARY_ASSERT(0 <= index && index < length);
for (++index; index <= length; index += (index & -index))
data[index] += value;
}
//! @brief Calculate interval sum.
//! @param left lower limit of interval (0-indexed)
//! @param right upper limit of interval (0-indexed)
//! @return Sum of the elements within [left, right) (half-open interval)
//! @note Time complexity: O(log size)
[[nodiscard]] Elem sum(int left, int right) const {
CP_LIBRARY_ASSERT(0 <= left && left <= right && right <= length);
if (left == 0)
return partial_sum(right);
else
return partial_sum(right) - partial_sum(left - 1);
}
//! @brief Get the value of the index-th element.
//! @param index index (0-indexed)
//! @note Time complexity: O(log size)
[[nodiscard]] Elem get(int index) const {
return partial_sum(index + 1) - partial_sum(index);
}
//! @brief Set the value of the index-th element to value.
//! @param index index (0-indexed)
//! @param value value to be set
//! @note Time complexity: O(log size)
void set(const int index, const Elem& value) {
add(index, value - get(index));
}
};
} // namespace internal::binary_indexed_tree_hpp
//! @brief Binary indexed tree with uniform add function.
//! @tparam Elem Element type. Watch out for overflows.
template <typename Elem>
class binary_indexed_tree {
private:
internal::binary_indexed_tree_hpp::binary_indexed_tree_impl<Elem> bit_0, bit_1;
//! @return Sum of the elements within [0, index) (0-indexed, half-open interval)
[[nodiscard]] Elem partial_sum(int index) const {
return bit_0.sum(0, index) + bit_1.sum(0, index) * (index - 1);
}
public:
//! @brief Construct a vector of n zeroes.
//! @param n vector size
explicit binary_indexed_tree(const int n) : bit_0(n), bit_1(n) {}
//! @brief Construct a vector from an existing container.
//! @tparam Container container type (deduced from parameter).
//! @param src Source (container)
template <typename Container>
explicit binary_indexed_tree(const Container& src) : bit_0(src), bit_1(static_cast<int>(std::size(src))) {}
//! @brief Construct a vector of length n filled with initial_values.
//! @param n vector size
//! @param initial_value initial value for all elements
binary_indexed_tree(const int n, const Elem& initial_value) : bit_0(n, initial_value), bit_1(n) {}
//! @return Vector size (length)
[[nodiscard]] int size() const noexcept {
return bit_0.size();
}
//! @brief Add value to the index-th element.
//! @param index index of the element to be added (0-indexed)
//! @param value value to be added
//! @note Time complexity: O(log size)
void add(int index, const Elem& value) {
bit_0.add(index, value);
}
//! @brief Add value to the elements within [left, right) (half-open interval)
//! @param left lower limit of interval (0-indexed)
//! @param right upper limit of interval (0-indexed)
//! @param value value to be added
//! @note Time complexity: O(log size)
void uniform_add(int left, int right, const Elem& value) {
CP_LIBRARY_ASSERT(0 <= left && left <= right && right <= size());
if (left != size()) {
bit_0.add(left, value * (-1) * (left - 1));
bit_1.add(left, value);
}
if (right != size()) {
bit_0.add(right, value * (right - 1));
bit_1.add(right, value * (-1));
}
}
//! @brief Calculate interval sum.
//! @param left lower limit of interval (0-indexed)
//! @param right upper limit of interval (0-indexed)
//! @return Sum of the elements within [left, right) (half-open interval)
//! @note Time complexity: O(log size)
[[nodiscard]] Elem sum(int left, int right) const {
if (left == 0)
return partial_sum(right);
else
return partial_sum(right) - partial_sum(left - 1);
}
//! @brief Get the value of the index-th element.
//! @param index index (0-indexed)
//! @note Time complexity: O(log size)
[[nodiscard]] Elem get(int index) const {
return partial_sum(index + 1) - partial_sum(index);
}
//! @brief Set the value of the index-th element to value.
//! @param index index (0-indexed)
//! @param value value to be set
//! @note Time complexity: O(log size)
void set(const int index, const Elem& value) {
bit_0.add(index, value - get(index));
}
//! @brief Print debug information.
//! @param name variable name
//! @param os output stream
void debug_print([[maybe_unused]] const std::string& name = "", [[maybe_unused]] std::ostream& os = std::cerr) const {
#if (CP_LIBRARY_DEBUG_LEVEL >= 1)
if (!name.empty())
os << name << ": ";
os << "val [ ";
for (int i = 0; i < size(); ++i)
os << get(i) << ' ';
os << "]\n";
if (!name.empty())
os << std::string(std::size(name) + 2, ' ');
os << "sum [ ";
for (int i = 0; i <= size(); ++i)
os << partial_sum(i) << ' ';
os << "]\n";
#endif
}
};
} // namespace lib
#ifdef CP_LIBRARY_ASSERT_NOT_DEFINED
# undef CP_LIBRARY_ASSERT
# undef CP_LIBRARY_ASSERT_NOT_DEFINED
#endif
#endif // CP_LIBRARY_BINARY_INDEXED_TREE_HPP
#line 7 "test/data_structure/binary_indexed_tree/2.test.cpp"
int main() {
int N, K;
std::cin >> N >> K;
std::vector<int> L(K), R(K);
for (int i = 0; i < K; ++i)
std::cin >> L[i] >> R[i];
lib::binary_indexed_tree<lib::static_modint<998244353>> tree(N);
tree.add(0, 1);
for (int i = 0; i < N; ++i) {
const auto current = tree.get(i);
for (int j = 0; j < K; ++j) {
const int l = i + L[j];
const int r = std::min(N, i + 1 + R[j]);
if (l < N)
tree.uniform_add(l, r, current);
}
}
std::cout << tree.get(N - 1) << '\n';
}