QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#615882#9441. Many Common Segment Problemsucup-team4435#AC ✓2229ms30652kbC++2038.2kb2024-10-05 20:47:572024-10-05 20:47:59

Judging History

This is the latest submission verdict.

  • [2024-10-05 20:47:59]
  • Judged
  • Verdict: AC
  • Time: 2229ms
  • Memory: 30652kb
  • [2024-10-05 20:47:57]
  • Submitted

answer

#include <bits/stdc++.h>
using namespace std;

using ll = long long;
using ld = long double;

#define all(a) begin(a), end(a)
#define len(a) int((a).size())

template<typename Fun>
struct y_combinator {
    const Fun fun;

    explicit y_combinator(const Fun&& fun) : fun(std::forward<const Fun>(fun)) {}

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

#ifdef LOCAL
    #include "debug.h"
#else
    #define dbg(...)
    #define dprint(...)
    #define debug if constexpr (false)
    #define draw_tree(...)
    #define draw_array(...)
#endif // LOCAL

/*
 ! WARNING: MOD must be prime.
 ! WARNING: MOD must be less than 2^30.
 * Use .get() to get the stored value.
 */
template<uint32_t mod>
class montgomery {
    static_assert(mod < uint32_t(1) << 30, "mod < 2^30");
    using mint = montgomery<mod>;

private:
    uint32_t value;

    static constexpr uint32_t inv_neg_mod = []() {
        uint32_t x = mod;
        for (int i = 0; i < 4; ++i) {
            x *= uint32_t(2) - mod * x;
        }
        return -x;
    }();
    static_assert(mod * inv_neg_mod == -1);

    static constexpr uint32_t neg_mod = (-uint64_t(mod)) % mod;

    static uint32_t reduce(const uint64_t &value) {
        return (value + uint64_t(uint32_t(value) * inv_neg_mod) * mod) >> 32;
    }

    inline static const mint ONE = mint(1);

public:
    montgomery() : value(0) {}
    montgomery(const mint &x) : value(x.value) {}

    template<typename T, typename U = std::enable_if_t<std::is_integral<T>::value>>
    montgomery(const T &x) : value(!x ? 0 : reduce(int64_t(x % int32_t(mod) + int32_t(mod)) * neg_mod)) {}

    static constexpr uint32_t get_mod() {
        return mod;
    }

    uint32_t get() const {
        auto real_value = reduce(value);
        return real_value < mod ? real_value : real_value - mod;
    }

    template<typename T>
    mint power(T degree) const {
        degree = (degree % int32_t(mod - 1) + int32_t(mod - 1)) % int32_t(mod - 1);
        mint prod = 1, a = *this;
        for (; degree > 0; degree >>= 1, a *= a)
            if (degree & 1)
                prod *= a;

        return prod;
    }

    mint inv() const {
        return power(-1);
    }

    mint& operator=(const mint &x) {
        value = x.value;
        return *this;
    }

    mint& operator+=(const mint &x) {
        if (int32_t(value += x.value - (mod << 1)) < 0) {
            value += mod << 1;
        }
        return *this;
    }

    mint& operator-=(const mint &x) {
        if (int32_t(value -= x.value) < 0) {
            value += mod << 1;
        }
        return *this;
    }

    mint& operator*=(const mint &x) {
        value = reduce(uint64_t(value) * x.value);
        return *this;
    }

    mint& operator/=(const mint &x) {
        return *this *= x.inv();
    }

    friend mint operator+(const mint &x, const mint &y) {
        return mint(x) += y;
    }

    friend mint operator-(const mint &x, const mint &y) {
        return mint(x) -= y;
    }

    friend mint operator*(const mint &x, const mint &y) {
        return mint(x) *= y;
    }

    friend mint operator/(const mint &x, const mint &y) {
        return mint(x) /= y;
    }

    mint& operator++() {
        return *this += ONE;
    }

    mint& operator--() {
        return *this -= ONE;
    }

    mint operator++(int) {
        mint prev = *this;
        *this += ONE;
        return prev;
    }

    mint operator--(int) {
        mint prev = *this;
        *this -= ONE;
        return prev;
    }

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

    bool operator==(const mint &x) const {
        return get() == x.get();
    }

    bool operator!=(const mint &x) const {
        return get() != x.get();
    }

    bool operator<(const mint &x) const {
        return get() < x.get();
    }

    template<typename T>
    explicit operator T() {
        return get();
    }

    friend std::istream& operator>>(std::istream &in, mint &x) {
        std::string s;
        in >> s;
        x = 0;
        bool neg = s[0] == '-';
        for (const auto c : s)
            if (c != '-')
                x = x * 10 + (c - '0');

        if (neg)
            x *= -1;

        return in;
    }

    friend std::ostream& operator<<(std::ostream &out, const mint &x) {
        return out << x.get();
    }

    static int32_t primitive_root() {
        if constexpr (mod == 1'000'000'007)
            return 5;
        if constexpr (mod == 998'244'353)
            return 3;
        if constexpr (mod == 786433)
            return 10;

        static int root = -1;
        if (root != -1)
            return root;

        std::vector<int> primes;
        int value = mod - 1;
        for (int i = 2; i * i <= value; i++)
            if (value % i == 0) {
                primes.push_back(i);
                while (value % i == 0)
                    value /= i;
            }

        if (value != 1)
            primes.push_back(value);

        for (int r = 2;; r++) {
            bool ok = true;
            for (auto p : primes)
                if ((mint(r).power((mod - 1) / p)).get() == 1) {
                    ok = false;
                    break;
                }

            if (ok)
                return root = r;
        }
    }
};

// constexpr uint32_t MOD = 1'000'000'007;
constexpr uint32_t MOD = 998'244'353;
using mint = montgomery<MOD>;

/*
 ! WARNING: (MOD - 1) must be divisible by (2n), where n is max degree of polynomials.
 * Include static_modular_int or montgomery (faster) to use it.
 * Don't need to care about precomputing primitive root.
 * Use it like std::vector<mint> with extra methods.
 */
template<typename mint>
class polynom_t : public std::vector<mint> {
public:
    static constexpr int RANK = __builtin_ctz(mint::get_mod() - 1);
    static_assert(RANK >= 15, "MOD doesn't seem fft-friendly.");

    using value_type = mint;

    using std::vector<mint>::empty;
    using std::vector<mint>::back;
    using std::vector<mint>::pop_back;
    using std::vector<mint>::size;
    using std::vector<mint>::clear;
    using std::vector<mint>::begin;
    using std::vector<mint>::end;

private:
    static constexpr int EVAL_BRUTE_SIZE = 1 << 4;
    static constexpr int MUL_MIN_CUT = 20;
    static constexpr int MUL_MAX_CUT = 1 << 6;
    static constexpr int DIV_N_CUT = 1 << 7;
    static constexpr int DIV_M_CUT = 1 << 6;
    static constexpr int INV_BRUTE_FORCE_SIZE = 1 << 3;

    class fft_precalc {
    private:
        std::vector<mint> inv_{0, 1};

    public:
        mint root;
        // roots[x] = root.power((MOD - 1) / 2^x)
        // inv_roots[x] = roots[x].inv()
        std::array<mint, RANK> roots, inv_roots;
        // inv_l[x] = 2^{-x}
        std::array<mint, RANK> inv_l;
        // r_transition[x] = root.power((MOD - 1) * (3 - 2^{x + 1}) / 2^{x + 2})
        // inv_r_transition[x] = r_transition[x].inv()
        std::array<mint, RANK> r_transition, inv_r_transition;

        fft_precalc() {
            root = mint::primitive_root();
            for (int x = 0; x < RANK; x++) {
                roots[x] = mint(root).power((mint::get_mod() - 1) >> x);
                inv_roots[x] = roots[x].inv();
                inv_l[x] = mint(1 << x).inv();
                r_transition[x] = root.power(((mint::get_mod() - 1) >> (x + 2)) * (3 + (1 << (x + 1))));
                inv_r_transition[x] = r_transition[x].inv();
            }
        }

        mint inv(int x) {
            for (int i = inv_.size(); i <= x; i++) {
                inv_.push_back(-inv_[mint::get_mod() % i] * mint(mint::get_mod() / i));
            }
            return inv_[x];
        }
    };

    inline static fft_precalc fft_data;

public:
    /*
     * Let r be the primitive root of the given MOD.
     * Let rev(i) be the reversed i as a binary mask of size log(n).
     * Let R[i] = r.power((MOD - 1) / a.size() * rev(i)).
     * It replaces a[i] with a.eval(R[i]).
     */
    static void fft(polynom_t<mint> &a) {
        assert(!a.empty());
        int n = a.size(), l = __builtin_ctz(n);
        for (int len = 0; len < l; len++) {
            int m = n >> (len + 1);
            mint r = 1;
            for (int i = 0; i < (1 << len); i++) {
                // rot = r^((MOD - 1) / n * rev(2 * i))
                int id = i << (l - len);
                for (int j = 0; j < m; j++) {
                    auto u = a[id + j];
                    auto v = a[id + j + m] * r;
                    a[id + j] = u + v;
                    a[id + j + m] = u - v;
                }
                if (i + 1 < (1 << len)) {
                    r *= fft_data.r_transition[__builtin_ctz(~i)];
                }
            }
        }
    }

    // inv_fft(fft(a)) = a
    static void inv_fft(polynom_t<mint> &a) {
        assert(!a.empty());
        int n = a.size(), l = __builtin_ctz(n);
        for (int len = l - 1; len >= 0; len--) {
            int m = n >> (len + 1);
            mint ir = 1;
            for (int i = 0; i < (1 << len); i++) {
                int id = i << (l - len);
                for (int j = 0; j < m; j++) {
                    auto u = a[id + j];
                    auto v = a[id + j + m];
                    a[id + j] = u + v;
                    a[id + j + m] = (u - v) * ir;
                }
                if (i + 1 < (1 << len)) {
                    ir *= fft_data.inv_r_transition[__builtin_ctz(~i)];
                }
            }
        }
        for (auto &value : a) {
            value *= fft_data.inv_l[l];
        }
    }

private:
    // Completes fft assuming that a is the right half of the polynomial after the first iteration of the fft.
    static void right_half_fft(polynom_t<mint> &a) {
        assert(!a.empty());
        int n = a.size(), l = __builtin_ctz(n);
        for (int len = 0; len < l; len++) {
            int m = n >> (len + 1);
            mint r = fft_data.roots[len + 2];
            for (int i = 0; i < (1 << len); i++) {
                int id = i << (l - len);
                for (int j = 0; j < m; j++) {
                    auto u = a[id + j];
                    auto v = a[id + j + m] * r;
                    a[id + j] = u + v;
                    a[id + j + m] = u - v;
                }
                if (i + 1 < (1 << len)) {
                    r *= fft_data.r_transition[__builtin_ctz(~i)];
                }
            }
        }
    }

    // inv_right_half_fft(right_half_fft(a)) = a
    static void inv_right_half_fft(polynom_t<mint> &a) {
        assert(!a.empty());
        int n = a.size(), l = __builtin_ctz(n);
        for (int len = l - 1; len >= 0; len--) {
            int m = n >> (len + 1);
            mint ir = fft_data.inv_roots[len + 2];
            for (int i = 0; i < (1 << len); i++) {
                int id = i << (l - len);
                for (int j = 0; j < m; j++) {
                    auto u = a[id + j];
                    auto v = a[id + j + m];
                    a[id + j] = u + v;
                    a[id + j + m] = (u - v) * ir;
                }
                if (i + 1 < (1 << len)) {
                    ir *= fft_data.inv_r_transition[__builtin_ctz(~i)];
                }
            }
        }
        for (auto &value : a) {
            value *= fft_data.inv_l[l];
        }
    }

    class multipoint_evaluation_tree {
    private:
        int n_points, l;
        std::vector<polynom_t<mint>> segtree;

    public:
        multipoint_evaluation_tree(int n, const std::vector<mint> &points) : n_points(points.size()), l(1) {
            while ((1 << l) < n) {
                l++;
            }
            polynom_t<mint> aux;
            aux.reserve(1 << l);

            segtree.assign(l + 1, polynom_t<mint>(1 << (l + 1)));
            for (int i = 0; i < (1 << l); i++) {
                aux = {i < n_points ? -points[i] : 0, 1};
                fft(aux);
                segtree[0][i << 1] = aux[0];
                segtree[0][i << 1 | 1] = aux[1];
            }

            for (int len = 0; len < l; len++) {
                aux.resize(1 << (len + 1));
                for (int i = 0; i < (1 << (l + 1)); i += (1 << (len + 2))) {
                    for (int j = 0; j < static_cast<int>(aux.size()); j++) {
                        aux[j] = segtree[len][i + j] * segtree[len][i + j + aux.size()];
                    }
                    if (len + 1 < l) {
                        std::copy(aux.begin(), aux.end(), segtree[len + 1].begin() + i);
                        inv_fft(aux);
                        aux[0] -= 2;
                        right_half_fft(aux);
                        std::copy(aux.begin(), aux.end(), segtree[len + 1].begin() + i + aux.size());
                    } else {
                        inv_fft(aux);
                        aux[0]--;
                        std::copy(aux.begin(), aux.end(), segtree[len + 1].begin() + i);
                        segtree[len + 1][1 << (len + 1)]++;
                    }
                }
            }
        }

        std::vector<mint> evaluate(polynom_t<mint> f) const {
            if (static_cast<int>(f.size()) > (1 << l)) {
                f %= segtree[l];
            }
            assert(static_cast<int>(f.size()) <= (1 << l));
            f.resize(1 << (l + 1));
            std::rotate(f.begin(), std::prev(f.end()), f.end());
            fft(f);

            auto g = segtree[l];
            std::reverse(g.begin(), g.begin() + 1 + (1 << l));
            g.resize(1 << l);
            g = g.inv(1 << l);
            std::reverse(g.begin(), g.end());
            g.resize(1 << (l + 1));
            fft(g);
            for (int i = 0; i < (1 << (l + 1)); i++) {
                g[i] *= f[i];
            }

            polynom_t<mint> aux;
            aux.reserve(1 << l);
            for (int len = l - 1; len >= 0; len--) {
                aux.resize(1 << (len + 1));
                for (int i = 0; i < (1 << (l + 1)); i += (1 << (len + 2))) {
                    for (int j = 0; j < static_cast<int>(aux.size()); j++) {
                        aux[j] = g[i + j + (1 << (len + 1))];
                    }
                    inv_right_half_fft(aux);
                    fft(aux);
                    std::copy(aux.begin(), aux.end(), g.begin() + i + (1 << (len + 1)));
                    for (int j = 0; j < (1 << (len + 1)); j++) {
                        auto x = g[i + j] - g[i + j + (1 << (len + 1))];
                        g[i + j + (1 << (len + 1))] = x * segtree[len][i + j];
                        g[i + j] = x * segtree[len][i + j + (1 << (len + 1))];
                    }
                }
            }

            std::vector<mint> eval(n_points);
            for (int i = 0; i < n_points; i++) {
                eval[i] = (g[2 * i] - g[2 * i + 1]) * fft_data.inv_l[l + 1];
            }
            return eval;
        }

        polynom_t<mint> interpolate(const std::vector<mint> &y) const {
            auto prod = segtree[l];
            prod.erase(prod.begin(), prod.begin() + ((1 << l) - n_points));
            auto values = evaluate(prod.derivative());

            polynom_t<mint> aux;
            aux.reserve(1 << l);
            polynom_t<mint> result(1 << (l + 1));
            for (int i = 0; i < (1 << l); i++) {
                aux = {i < n_points ? y[i] / values[i] : 0, 0};
                fft(aux);
                result[i << 1] = aux[0];
                result[i << 1 | 1] = aux[1];
            }

            polynom_t<mint> next(1 << (l + 1));
            for (int len = 0; len < l; len++) {
                aux.resize(1 << (len + 1));
                for (int i = 0; i < (1 << (l + 1)); i += (1 << (len + 2))) {
                    for (int j = 0; j < (1 << (len + 1)); j++) {
                        aux[j] = segtree[len][i + j] * result[i + j + aux.size()]
                               + segtree[len][i + j + aux.size()] * result[i + j];
                    }
                    if (len + 1 < l) {
                        std::copy(aux.begin(), aux.end(), next.begin() + i);
                        inv_fft(aux);
                        right_half_fft(aux);
                        std::copy(aux.begin(), aux.end(), next.begin() + i + (1 << (len + 1)));
                    } else {
                        inv_fft(aux);
                        std::copy(aux.begin(), aux.end(), next.begin());
                    }
                }
                result.swap(next);
            }
            result.erase(result.begin(), result.begin() + ((1 << l) - n_points));
            return result.resize(n_points);
        }
    };

public:
    polynom_t() : std::vector<mint>() {}
    polynom_t(size_t n, mint value = 0) : std::vector<mint>(n, value) {}
    polynom_t(std::initializer_list<mint> values) : std::vector<mint>(values.begin(), values.end()) {}

    template<typename Iterator>
    polynom_t(Iterator first, Iterator last) : std::vector<mint>(first, last) {}

    polynom_t<mint>& resize(int n) {
        std::vector<mint>::resize(n);
        return *this;
    }

    // Removes extra zeroes.
    void normalize() {
        while (!empty() && back() == mint(0)) {
            pop_back();
        }
    }

    // Returns -1 if all coefficients are zeroes (not O(1)!).
    int degree() const {
        int deg = static_cast<int>(size()) - 1;
        while (deg >= 0 && (*this)[deg] == mint(0)) {
            deg--;
        }
        return deg;
    }

    mint eval(const mint &x) const {
        mint power = 1, value = 0;
        for (int i = 0; i < static_cast<int>(size()); i++, power *= x) {
            value += (*this)[i] * power;
        }
        return value;
    }

    // Calculates eval at the given points.
    // O(n log^2).
    std::vector<mint> multipoint_evaluation(const std::vector<mint> &points) const {
        if (std::min(size(), points.size()) <= EVAL_BRUTE_SIZE) {
            std::vector<mint> eval(points.size());
            for (int i = 0; i < static_cast<int>(points.size()); i++) {
                eval[i] = this->eval(points[i]);
            }
            return eval;
        }
        return multipoint_evaluation_tree(std::max(size(), points.size()), points).evaluate(*this);
    }

    // Interpolates polynomial f, such that f(x[i]) = y[i].
    // O(n log^2).
    static polynom_t<mint> interpolate(const std::vector<mint> &x, const std::vector<mint> &y) {
        assert(x.size() == y.size());
        return multipoint_evaluation_tree(x.size(), x).interpolate(y);
    }

    polynom_t<mint> operator-() const {
        return polynom_t(*this) * mint(-1);
    }

    polynom_t<mint>& operator+=(const polynom_t<mint> &another) {
        if (size() < another.size()) {
            resize(another.size());
        }
        for (int i = 0; i < static_cast<int>(another.size()); i++) {
            (*this)[i] += another[i];
        }
        return *this;
    }

    polynom_t<mint>& operator-=(const polynom_t<mint> &another) {
        if (size() < another.size()) {
            resize(another.size());
        }
        for (int i = 0; i < static_cast<int>(another.size()); i++) {
            (*this)[i] -= another[i];
        }
        return *this;
    }

    polynom_t<mint>& operator*=(const polynom_t<mint> &another) {
        if (empty() || another.empty()) {
            clear();
            return *this;
        }

        if (std::min(size(), another.size()) <= MUL_MIN_CUT
            || std::max(size(), another.size()) <= MUL_MAX_CUT) {
            polynom_t<mint> product(int(size() + another.size()) - 1);
            for (int i = 0; i < static_cast<int>(size()); i++) {
                for (int j = 0; j < static_cast<int>(another.size()); j++) {
                    product[i + j] += (*this)[i] * another[j];
                }
            }
            product.swap(*this);
            return *this;
        }

        int real_size = static_cast<int>(size() + another.size()) - 1, n = 1;
        while (n < real_size) {
            n <<= 1;
        }

        if ((*this) == another) {
            resize(n);
            fft(*this);
            for (int i = 0; i < n; i++) {
                (*this)[i] *= (*this)[i];
            }
        } else {
            resize(n);
            polynom_t b = another;
            b.resize(n);
            fft(*this), fft(b);
            for (int i = 0; i < n; i++) {
                (*this)[i] *= b[i];
            }
        }
        inv_fft(*this);
        return resize(real_size);
    }

    // Division with remainder.
    // O(n log).
    polynom_t<mint>& operator/=(const polynom_t<mint> &another) {
        polynom_t<mint> a(*this), b(another);
        a.normalize(), b.normalize();
        assert(!b.empty());
        int n = a.size(), m = b.size();
        if (n < m) {
            return *this = {};
        }
        if (n <= DIV_N_CUT || m <= DIV_M_CUT) {
            polynom_t<mint> quotient(n - m + 1);
            mint inv_b = mint(1) / b.back();
            for (int i = n - 1; i >= m - 1; i--) {
                int pos = i - m + 1;
                quotient[pos] = a[i] * inv_b;
                for (int j = 0; j < m; j++) {
                    a[pos + j] -= b[j] * quotient[pos];
                }
            }
            quotient.normalize();
            return *this = quotient;
        }

        std::reverse(a.begin(), a.end());
        std::reverse(b.begin(), b.end());
        polynom_t<mint> quotient = a * b.inv(n - m + 1);
        quotient.resize(n - m + 1);
        std::reverse(quotient.begin(), quotient.end());
        quotient.normalize();
        return *this = quotient;
    }

    // O(n log).
    polynom_t<mint>& operator%=(const polynom_t<mint> &another) {
        *this -= (*this) / another * another;
        normalize();
        return *this;
    }

    // Returns derivative.
    // O(n).
    polynom_t<mint> derivative() const {
        polynom_t<mint> der(std::max(0, static_cast<int>(size()) - 1));
        for (int i = 0; i < static_cast<int>(der.size()); i++) {
            der[i] = mint(i + 1) * (*this)[i + 1];
        }
        return der;
    }

    // Returns integral.
    // O(n).
    polynom_t<mint> integral(const mint &constant = mint(0)) const {
        polynom_t<mint> in(size() + 1);
        in[0] = constant;
        for (int i = 1; i < static_cast<int>(in.size()); i++) {
            in[i] = (*this)[i - 1] * fft_data.inv(i);
        }
        return in;
    }

    // Returns p^{-1} modulo x^{degree}.
    // O(n log).
    polynom_t<mint> inv(int degree) const {
        assert(!empty() && (*this)[0] != mint(0) && "polynom is not invertable");

        int result_size = 1;
        while (result_size < degree) {
            result_size <<= 1;
        }
        polynom_t<mint> inv(result_size);
        inv[0] = (*this)[0].inv();

        int brute_calculation = std::min(degree, INV_BRUTE_FORCE_SIZE);
        mint start_inv = (*this)[0].inv();

        polynom_t<mint> have(brute_calculation);
        for (int i = 0; i < brute_calculation; i++) {
            inv[i] = ((i == 0 ? 1 : 0) - have[i]) * start_inv;
            int steps = std::min<int>(size(), have.size() - i);
            for (int j = 0; j < steps; j++) {
                have[i + j] += inv[i] * (*this)[j];
            }
        }

        polynom_t<mint> this_copy, inv_copy;
        this_copy.reserve(result_size);
        inv_copy.reserve(result_size);

        for (int power = brute_calculation; power < degree; power <<= 1) {
            this_copy.resize(power << 1);
            std::fill(this_copy.begin() + std::min<int>(size(), power << 1), this_copy.end(), 0);
            std::copy(begin(), begin() + std::min<int>(size(), power << 1), this_copy.begin());
            inv_copy.resize(power << 1);
            std::copy(inv.begin(), inv.begin() + power, inv_copy.begin());

            fft(this_copy);
            fft(inv_copy);
            for (int i = 0; i < (power << 1); i++) {
                this_copy[i] *= inv_copy[i];
            }
            inv_fft(this_copy);
            std::fill(this_copy.begin(), this_copy.begin() + power, 0);
            fft(this_copy);
            for (int i = 0; i < (power << 1); i++) {
                this_copy[i] *= inv_copy[i];
            }
            inv_fft(this_copy);
            for (int i = power; i < (power << 1); i++) {
                inv[i] = -this_copy[i];
            }
        }
        return inv.resize(degree);
    }

    // Returns log(p) modulo x^{degree}.
    // O(n log).
    polynom_t<mint> log(int degree) const {
        assert(!empty() && (*this)[0] == mint(1) && "log is not defined");
        return (derivative().resize(std::min<int>(degree, size())) * inv(degree)).resize(degree).integral(mint(0)).resize(degree);
    }

    // Returns exp(p) modulo x^{degree}.
    // O(n log).
    polynom_t<mint> exp(int degree) const {
        assert(!empty() && (*this)[0] == mint(0) && "exp is not defined");

        int result_size = 1;
        while (result_size < degree) {
            result_size <<= 1;
        }

        polynom_t<mint> exp, exp_fft, inv_exp, inv_exp_fft;
        exp = exp_fft = inv_exp = inv_exp_fft = {1};
        polynom_t<mint> h(1), q(1);
        auto diff = derivative().resize(result_size);

        for (int power = 1; power < degree; power <<= 1) {
            exp_fft = polynom_t<mint>(exp).resize(power << 1);
            fft(exp_fft);
            for (int i = 0; i < power; i++) {
                h[i] = exp_fft[i] * inv_exp_fft[i];
            }
            inv_fft(h);

            std::fill(h.begin(), h.begin() + ((power + 1) >> 1), 0);
            fft(h);
            for (int i = 0; i < power; i++) {
                h[i] *= -inv_exp_fft[i];
            }
            inv_fft(h);

            for (int i = (power + 1) / 2; i < power; i++) {
                inv_exp.push_back(h[i]);
            }
            inv_exp_fft = polynom_t<mint>(inv_exp).resize(power << 1);
            fft(inv_exp_fft);

            h.assign(power, 0);
            std::copy(diff.begin(), diff.begin() + power - 1, h.begin());
            fft(h);
            for (int i = 0; i < power; i++) {
                q[i] = exp_fft[i] * h[i];
            }
            inv_fft(q);

            h.resize(power << 1);
            for (int i = 1; i < power; i++) {
                h[i] = exp[i] * i;
            }
            for (int i = 0; i < power; i++) {
                h[i + 1] -= q[i];
            }
            h[0] = -q[power - 1];
            fft(h);

            q.resize(power << 1);
            for (int i = 0; i < (power << 1); i++) {
                q[i] = inv_exp_fft[i] * h[i];
            }
            inv_fft(q);

            if (static_cast<int>(size()) >= power) {
                h.assign(begin() + power, begin() + std::min<int>(power << 1, size()));
            } else {
                h.clear();
            }
            h.resize(power);

            for (int i = power - 1; i >= 0; i--) {
                h[i] -= q[i] * fft_data.inv(i + power);
            }
            h.resize(power << 1);
            fft(h);
            for (int i = 0; i < (power << 1); i++) {
                q[i] = exp_fft[i] * h[i];
            }
            inv_fft(q);

            exp.resize(power << 1);
            for (int i = 0; i < power; i++) {
                exp[power + i] = q[i];
            }
        }
        return exp.resize(degree);
    }

    // Returns p^{d} modulo x^{degree}.
    // O(n log).
    polynom_t<mint> power(int64_t d, int degree) const {
        if (!d || !degree) {
            return polynom_t<mint>{1}.resize(degree);
        }
        int pos = 0;
        while (pos < static_cast<int>(size()) && (*this)[pos] == mint(0)) {
            pos++;
        }
        if (pos == static_cast<int>(size()) || pos >= (degree + d - 1) / d) {
            return polynom_t<mint>(degree);
        }

        int coeffs_left = degree - d * pos;
        polynom_t<mint> result = ((polynom_t<mint>(begin() + pos, end()) / (*this)[pos]).log(coeffs_left)
                               * mint(d)).exp(coeffs_left) * (*this)[pos].power(d);
        result.resize(degree);
        for (int i = degree - 1; i - (degree - coeffs_left) >= 0; i--) {
            result[i] = result[i - (degree - coeffs_left)];
        }
        std::fill(result.begin(), result.end() - coeffs_left, mint(0));
        return result;
    }

    // Returns f(x + c).
    // O(n log).
    polynom_t<mint> change_of_variable(mint c) const {
        int n = size();
        polynom_t<mint> p(n), q(n);
        mint fact = 1, ifact = 1, power = 1;
        for (int i = 0; i < n; i++) {
            p[n - 1 - i] = (*this)[i] * fact;
            q[i] = power * ifact;
            fact *= i + 1;
            ifact *= fft_data.inv(i + 1);
            power *= c;
        }

        auto result = p * q;
        result.resize(n);
        std::reverse(result.begin(), result.end());
        ifact = 1;
        for (int i = 0; i < n; i++) {
            result[i] *= ifact;
            ifact *= fft_data.inv(i + 1);
        }
        return result;
    }

    template<typename V>
    std::enable_if_t<!std::is_same_v<V, polynom_t<mint>>, polynom_t<mint>&> operator*=(const V &value) {
        for (auto &x : *this) {
            x *= value;
        }
        return *this;
    }

    template<typename V>
    std::enable_if_t<!std::is_same_v<V, polynom_t<mint>>, polynom_t<mint>&> operator/=(const V &value) {
        for (auto &x : *this) {
            x /= value;
        }
        return *this;
    }

    friend std::ostream& operator<<(std::ostream &out, const polynom_t<mint> &p) {
        for (int i = 0; i < static_cast<int>(p.size()); i++) {
            if (i) out << ' ';
            out << p[i];
        }
        return out;
    }

    friend polynom_t<mint> operator+(const polynom_t<mint> &p, const polynom_t<mint> &q) {
        return polynom_t(p) += q;
    }

    friend polynom_t<mint> operator-(const polynom_t<mint> &p, const polynom_t<mint> &q) {
        return polynom_t(p) -= q;
    }

    friend polynom_t<mint> operator*(const polynom_t<mint> &p, const polynom_t<mint> &q) {
        return polynom_t(p) *= q;
    }

    friend polynom_t<mint> operator/(const polynom_t<mint> &p, const polynom_t<mint> &q) {
        return polynom_t(p) /= q;
    }

    friend polynom_t<mint> operator%(const polynom_t<mint> &p, const polynom_t<mint> &q) {
        return polynom_t(p) %= q;
    }

    template<typename V>
    friend std::enable_if_t<!std::is_same_v<V, polynom_t<mint>>, polynom_t<mint>>
        operator*(const V &value, const polynom_t<mint> &p) {
        return polynom_t<mint>(p) *= value;
    }

    template<typename V>
    friend std::enable_if_t<!std::is_same_v<V, polynom_t<mint>>, polynom_t<mint>>
        operator*(const polynom_t<mint> &p, const V &value) {
        return polynom_t<mint>(p) *= value;
    }

    template<typename V>
    friend std::enable_if_t<!std::is_same_v<V, polynom_t<mint>>, polynom_t<mint>>
        operator/(const V &value, const polynom_t<mint> &p) {
        return polynom_t<mint>(p) /= value;
    }

    template<typename V>
    friend std::enable_if_t<!std::is_same_v<V, polynom_t<mint>>, polynom_t<mint>>
        operator/(const polynom_t<mint> &p, const V &value) {
        return polynom_t<mint>(p) /= value;
    }
};

using polynom = polynom_t<mint>;

/*
 ! WARNING: MOD must be prime.
 * Define modular int class above it.
 * No need to run any init function, it dynamically resizes the data.
 */
namespace combinatorics {
    std::vector<mint> fact_, ifact_, inv_;

    void resize_data(int size) {
        if (fact_.empty()) {
            fact_ = {mint(1), mint(1)};
            ifact_ = {mint(1), mint(1)};
            inv_ = {mint(0), mint(1)};
        }
        for (int pos = fact_.size(); pos <= size; pos++) {
            fact_.push_back(fact_.back() * mint(pos));
            inv_.push_back(-inv_[MOD % pos] * mint(MOD / pos));
            ifact_.push_back(ifact_.back() * inv_[pos]);
        }
    }

    struct combinatorics_info {
        std::vector<mint> &data;

        combinatorics_info(std::vector<mint> &data) : data(data) {}

        mint operator[](int pos) {
            if (pos >= static_cast<int>(data.size())) {
                resize_data(pos);
            }
            return data[pos];
        }
    } fact(fact_), ifact(ifact_), inv(inv_);

    // From n choose k.
    // O(max(n)) in total.
    mint choose(int n, int k) {
        if (n < k || k < 0 || n < 0) {
            return mint(0);
        }
        return fact[n] * ifact[k] * ifact[n - k];
    }

    // From n choose k.
    // O(min(k, n - k)).
    mint choose_slow(int64_t n, int64_t k) {
        if (n < k || k < 0 || n < 0) {
            return mint(0);
        }
        k = std::min(k, n - k);
        mint result = 1;
        for (int i = k; i >= 1; i--) {
            result *= (n - i + 1);
            result *= inv[i];
        }
        return result;
    }

    // Number of balanced bracket sequences with n open and m closing brackets.
    mint catalan(int n, int m) {
        if (m > n || m < 0) {
            return mint(0);
        }
        return choose(n + m, m) - choose(n + m, m - 1);
    }

    // Number of balanced bracket sequences with n open and closing brackets.
    mint catalan(int n) {
        return catalan(n, n);
    }
} // namespace combinatorics

using namespace combinatorics;

/*
 * Zero based.
 * Type T must have operator += T.
 * Type T must have default constructor, which sets neutral value.
 * Operation += must be commutative.
 * For segment query type T must have operator -= T.
 */
template<typename T>
struct binary_indexed_tree {
    std::vector<T> bit;

    // Fills the array with default values.
    binary_indexed_tree(int n = 0) : bit(n + 1) {}

    int size() const {
        return int(bit.size()) - 1;
    }

    // Adds delta at the position pos.
    void add(int pos, T delta) {
        for (pos++; pos < static_cast<int>(bit.size()); pos += pos & -pos) {
            bit[pos] += delta;
        }
    }

    // Returns the sum on the segment [0, pref].
    T query(int pref) {
        T sum = T();
        for (pref++; pref > 0; pref -= pref & -pref) {
            sum += bit[pref];
        }
        return sum;
    }

    // Returns the sum on the interval [l, r).
    T query(int l, int r) {
        if (r <= l) {
            return T();
        }
        T res = query(r - 1);
        res -= query(l - 1);
        return res;
    }
};

int main() {
    cin.tie(nullptr)->sync_with_stdio(false);

    int n, m;
    cin >> n >> m;

    vector<int> ls, rs;
    int both = 0;
    vector<pair<int, int>> segs;

    while (n--) {
        int l, r;
        cin >> l >> r;
        if (l == -1 && r == -1) {
            both++;
        } else if (r == -1) {
            ls.push_back(l - 1);
        } else if (l == -1) {
            rs.push_back(r - 1);
        } else {
            segs.emplace_back(l - 1, r - 1);
        }
    }

    mint total_segs = mint(m) * (m + 1) / 2;

    mint ways2 = 1;
    for (auto l : ls) {
        ways2 *= m - l;
    }
    for (auto r : rs) {
        ways2 *= r + 1;
    }
    ways2 *= total_segs.power(both);

    vector<int> cnt_left(m);
    for (auto l : ls) {
        cnt_left[l]++;
    }

    vector<mint> prod_left(m, 1);

    y_combinator([&](auto solve, int l, int r) -> polynom {
        if (r - l == 1) {
            polynom base{mint(m - l + m) * inv[m - l], -inv[m - l]};
            polynom here = base.power(cnt_left[l], cnt_left[l] + 1);
            prod_left[l] *= here.eval(l);
            return here;
        }

        int mid = (l + r) / 2;
        auto left = solve(l, mid);
        auto right = solve(mid, r);
        vector<mint> rp(r - mid);
        iota(all(rp), mint(mid));
        auto vals = left.multipoint_evaluation(rp);
        for (int i = 0; i < r - mid; i++) {
            prod_left[mid + i] *= vals[i];
        }
        return left * right;
    })(0, m);

    vector<int> cnt_right(m);
    for (auto r : rs) {
        cnt_right[r]++;
    }

    vector<mint> prod_right(m, 1);

    y_combinator([&](auto solve, int l, int r) -> polynom {
        if (r - l == 1) {
            polynom base{mint(l + 1 + 1) * inv[l + 1], inv[l + 1]};
            polynom here = base.power(cnt_right[l], cnt_right[l] + 1);
            prod_right[l] *= here.eval(l);
            return here;
        }

        int mid = (l + r) / 2;
        auto left = solve(l, mid);
        auto right = solve(mid, r);
        vector<mint> lp(mid - l);
        iota(all(lp), mint(l));
        auto vals = right.multipoint_evaluation(lp);
        for (int i = 0; i < mid - l; i++) {
            prod_right[l + i] *= vals[i];
        }
        return left * right;
    })(0, m);

    vector<vector<int>> events(m);
    for (auto [l, r] : segs) {
        events[l].emplace_back(r);
    }
    binary_indexed_tree<int> bit(m);

    mint ans = 0;
    for (int x = 0; x < m; x++) {
        for (auto r : events[x]) {
            bit.add(r, 1);
        }

        {
            mint ways1 = ways2;
            ways1 *= mint(2).power(bit.query(x, m));

            ways1 *= prod_left[x];
            ways1 *= prod_right[x];

            ways1 *= ((total_segs + mint(x + 1) * (m - x)) / total_segs).power(both);
            ans += ways1 - ways2;
        }

        if (x + 1 < m) {
            mint ways1 = ways2;
            ways1 *= mint(2).power(bit.query(x + 1, m));

            ways1 *= prod_left[x + 1];
            ways1 *= (mint(m - (x + 1) + m - (x + 1)) / (m - (x + 1))).power(cnt_left[x + 1]).inv();

            ways1 *= prod_right[x];
            ways1 *= (mint(x + 1 + x + 1) / (x + 1)).power(cnt_right[x]).inv();

            ways1 *= ((total_segs + mint(x + 1) * (m - (x + 1))) / total_segs).power(both);
            ans -= ways1 - ways2;
        }
    }
    cout << ans << '\n';
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3612kb

input:

3 3
1 -1
2 2
2 3

output:

18

result:

ok "18"

Test #2:

score: 0
Accepted
time: 0ms
memory: 3572kb

input:

5 8
1 7
2 3
4 8
6 8
1 5

output:

15

result:

ok "15"

Test #3:

score: 0
Accepted
time: 0ms
memory: 3576kb

input:

10 13
4 -1
-1 -1
7 11
-1 -1
-1 -1
-1 -1
11 -1
3 8
-1 9
-1 -1

output:

841024210

result:

ok "841024210"

Test #4:

score: 0
Accepted
time: 31ms
memory: 5368kb

input:

46508 20888
9935 17879
803 14990
5348 5434
2630 15632
6302 16990
4875 20297
15220 17881
10385 16908
7395 13312
4794 5956
1867 13086
5261 14262
506 19423
18148 20403
1083 6648
13858 18123
4036 14289
7743 11040
15055 20527
1576 8846
10614 12995
2111 16084
6669 14966
1704 16041
8030 16085
6939 9047
281...

output:

622373905

result:

ok "622373905"

Test #5:

score: 0
Accepted
time: 19ms
memory: 4660kb

input:

71468 8417
1491 3032
3940 4632
4208 6407
419 5971
3498 5578
1905 3096
1962 3199
1291 2756
694 8294
2090 4946
7008 7851
2751 2882
2706 6889
108 5225
136 7934
2980 7661
4680 4716
1442 6931
610 2433
2029 7632
7493 8090
3186 5781
381 884
3605 6949
2658 4522
3990 5039
581 1842
2834 7073
969 7024
2753 637...

output:

624258105

result:

ok "624258105"

Test #6:

score: 0
Accepted
time: 82ms
memory: 7876kb

input:

33867 68520
14011 22082
27837 41559
2144 15734
12979 31839
886 12147
24281 49957
9826 65576
1722 19415
14491 47918
50636 58028
17563 41887
16942 39177
24530 40332
1552 34825
14639 29619
3990 12925
47753 51870
40028 53008
2544 30228
8858 41307
21578 60354
50609 60612
20338 21716
40758 41397
26456 648...

output:

222682065

result:

ok "222682065"

Test #7:

score: 0
Accepted
time: 57ms
memory: 7032kb

input:

75153 39190
26291 36182
5293 32997
7346 9934
2591 35269
19354 29051
22682 33232
2834 11921
15097 26586
21097 22576
16043 37502
6017 38992
13072 36070
31124 38395
11041 29593
3057 25268
20445 29246
32902 33740
22225 23893
21068 35059
13229 23256
33091 34091
28800 38407
21094 23905
6683 32400
3521 341...

output:

644912633

result:

ok "644912633"

Test #8:

score: 0
Accepted
time: 61ms
memory: 7196kb

input:

38983 51827
23950 48250
8451 21390
34709 41670
8577 19224
28009 38802
8116 46250
33417 41876
6012 27827
20506 28824
32508 36718
9519 42347
16217 47490
27201 43904
28345 36254
18056 51005
32416 48961
3944 44653
35051 46634
7354 28377
184 18868
5637 41072
17151 32335
46925 51578
38617 43416
28959 4596...

output:

66873446

result:

ok "66873446"

Test #9:

score: 0
Accepted
time: 49ms
memory: 5904kb

input:

7679 44174
248 9943
15956 26442
2725 33239
10294 16406
1756 43460
17823 36584
4116 8280
1378 8294
2284 6085
14532 29528
131 29456
8000 15758
7967 36529
18153 22142
24272 42715
5906 43341
15538 30632
30706 39656
578 37881
4671 42928
12276 35991
11872 39136
27705 33517
21108 27545
2303 27196
38175 400...

output:

346752121

result:

ok "346752121"

Test #10:

score: 0
Accepted
time: 94ms
memory: 8988kb

input:

64986 73724
3612 17490
57957 71291
9395 47466
16850 17666
34093 69220
9241 43326
3408 50034
25 43682
15457 38280
11293 12231
23016 60834
15987 24580
7089 70748
46238 49344
56579 71985
19218 59431
56899 64041
15137 38936
9921 34761
11644 28437
22451 55339
33303 61478
4834 11432
9944 49814
20282 35353...

output:

773636281

result:

ok "773636281"

Test #11:

score: 0
Accepted
time: 130ms
memory: 11232kb

input:

100000 100000
31015 42574
31826 52090
83087 85955
23220 37841
56013 70940
34751 70547
62376 76457
31649 91712
18505 47662
85040 98454
13121 30466
1256 3470
3980 85011
57880 71144
32147 38601
31379 50646
72392 87906
48476 76451
40774 58685
64093 68937
32329 80656
8177 25150
15432 60258
22018 69969
48...

output:

941648147

result:

ok "941648147"

Test #12:

score: 0
Accepted
time: 126ms
memory: 11352kb

input:

100000 100000
21014 66363
7456 75478
5229 68612
15284 54049
29655 88817
65818 66444
33870 76176
29544 90569
62304 90461
83356 99748
24455 94172
57249 66657
44630 50799
65039 67617
4906 38962
7679 51541
59316 74310
28441 76551
19291 98970
7367 65494
62031 78933
21758 59135
46144 98556
21049 55166
573...

output:

526465770

result:

ok "526465770"

Test #13:

score: 0
Accepted
time: 129ms
memory: 11424kb

input:

100000 100000
28862 53149
9949 69392
28285 43896
10353 80816
34142 87078
63124 67907
41674 71792
48558 72019
7933 14947
74512 93023
31561 85833
15508 33590
49247 51145
14356 99398
22644 66661
7140 85438
66105 67726
5276 71801
42804 71017
79393 96156
1822 11277
23399 70023
93290 99802
28361 43488
245...

output:

491623414

result:

ok "491623414"

Test #14:

score: 0
Accepted
time: 122ms
memory: 11352kb

input:

100000 100000
14799 99589
42148 79991
40478 73277
20849 87949
45939 72422
41866 46646
4066 26608
33598 57285
17116 42252
32284 66953
77926 88983
5776 86665
28968 39697
18131 25212
27377 42980
29537 93995
18671 55017
7758 76277
17700 62026
24191 54409
41589 55516
72711 73804
31813 37554
24757 72146
1...

output:

394684748

result:

ok "394684748"

Test #15:

score: 0
Accepted
time: 127ms
memory: 11360kb

input:

100000 100000
55015 73375
3596 29679
19231 76062
82350 90597
19070 96441
66671 80639
47379 62902
38990 66802
37706 64205
4989 34650
20110 59898
6647 99705
41498 42886
7404 33540
36363 42005
37631 75016
68188 82917
21072 43390
36562 68641
21837 95043
65523 85407
21441 41841
4913 98767
29560 94781
196...

output:

962695745

result:

ok "962695745"

Test #16:

score: 0
Accepted
time: 129ms
memory: 11284kb

input:

100000 100000
48836 52965
22215 37796
26537 29092
90657 98341
2131 7342
17641 64091
46683 79182
1165 35743
10331 23332
2889 41333
56769 75505
63440 94599
40276 80762
14501 68386
3239 8420
3295 69001
42668 83714
43199 90350
39921 65865
16402 19637
48565 85097
44750 83786
59138 84536
61461 96164
65218...

output:

269046725

result:

ok "269046725"

Test #17:

score: 0
Accepted
time: 130ms
memory: 11348kb

input:

100000 100000
1089 99857
8077 43835
37678 60147
2950 63548
84341 95606
4257 36034
15407 30716
27902 71808
18112 93015
608 75716
53770 66511
54636 89625
41858 47390
45083 76588
1172 53992
19357 42200
37722 52861
61126 92664
48696 88494
48676 89136
33660 90563
9778 25918
53528 97743
7510 96964
5597 33...

output:

86816533

result:

ok "86816533"

Test #18:

score: 0
Accepted
time: 113ms
memory: 10172kb

input:

100000 100000
1 100000
1 100000
1 100000
1 100000
1 100000
1 100000
1 100000
1 100000
1 100000
1 100000
1 100000
1 100000
1 100000
1 100000
1 100000
1 100000
1 100000
1 100000
1 100000
1 100000
1 100000
1 100000
1 100000
1 100000
1 100000
1 100000
1 100000
1 100000
1 100000
1 100000
1 100000
1 10000...

output:

538261387

result:

ok "538261387"

Test #19:

score: 0
Accepted
time: 4ms
memory: 3644kb

input:

1744 831
2 -1
430 692
-1 661
-1 145
282 330
-1 -1
27 -1
-1 579
232 538
-1 -1
318 508
315 -1
-1 -1
715 -1
524 -1
-1 555
-1 172
445 -1
-1 -1
-1 -1
79 -1
-1 -1
-1 -1
-1 231
-1 695
-1 654
63 -1
206 -1
-1 44
352 787
7 351
591 686
781 -1
313 -1
-1 154
576 -1
185 239
555 611
-1 721
-1 550
102 204
19 -1
436...

output:

603361949

result:

ok "603361949"

Test #20:

score: 0
Accepted
time: 1ms
memory: 3836kb

input:

645 233
-1 -1
33 227
-1 137
57 -1
51 -1
21 -1
44 108
41 123
-1 140
72 -1
-1 88
6 -1
21 56
-1 182
-1 160
-1 168
102 127
134 -1
-1 -1
-1 -1
181 -1
17 165
54 190
-1 97
187 -1
-1 147
78 -1
10 52
-1 169
75 -1
63 157
-1 100
21 155
-1 -1
-1 208
155 -1
38 196
-1 161
-1 231
-1 109
-1 206
-1 -1
-1 104
-1 -1
9...

output:

167490773

result:

ok "167490773"

Test #21:

score: 0
Accepted
time: 1ms
memory: 3848kb

input:

282 279
-1 131
-1 -1
113 115
35 256
9 268
-1 105
261 -1
-1 -1
89 -1
18 -1
65 182
-1 -1
-1 -1
167 191
-1 194
-1 -1
-1 -1
-1 -1
-1 105
32 51
-1 211
111 133
-1 -1
-1 -1
29 276
-1 35
29 276
-1 -1
54 -1
79 -1
-1 -1
142 164
82 -1
51 -1
-1 -1
262 -1
6 119
-1 -1
209 -1
-1 184
-1 -1
-1 -1
-1 -1
67 124
-1 124...

output:

269738573

result:

ok "269738573"

Test #22:

score: 0
Accepted
time: 8ms
memory: 4040kb

input:

2000 2000
904 -1
119 -1
-1 -1
175 722
-1 1469
692 1062
49 425
-1 783
1825 -1
-1 1133
733 1325
1946 -1
127 1559
694 1287
-1 -1
583 -1
-1 -1
1423 1626
12 -1
-1 -1
613 1547
-1 1057
368 1509
574 1515
1405 -1
971 -1
560 1370
430 1802
-1 1934
-1 795
-1 117
-1 -1
1367 1878
-1 1468
1118 1723
-1 -1
-1 647
-1...

output:

417815992

result:

ok "417815992"

Test #23:

score: 0
Accepted
time: 8ms
memory: 4080kb

input:

2000 2000
-1 -1
32 -1
327 -1
1220 1869
-1 1444
200 1066
1522 -1
1049 -1
1541 -1
1630 -1
-1 267
-1 482
-1 -1
-1 -1
1005 1847
131 303
24 1933
568 1176
772 -1
-1 -1
-1 -1
1920 -1
1676 -1
1697 1847
-1 -1
-1 -1
-1 973
1357 -1
886 1735
974 -1
-1 1943
-1 456
912 1531
916 -1
820 964
-1 -1
-1 96
404 1575
-1 ...

output:

946305065

result:

ok "946305065"

Test #24:

score: 0
Accepted
time: 5ms
memory: 3860kb

input:

2000 2000
842 1676
820 1103
155 -1
691 -1
763 1757
540 1818
-1 -1
-1 -1
-1 -1
813 -1
942 -1
-1 -1
939 -1
1600 -1
67 1656
-1 1365
691 -1
1245 -1
330 866
399 1774
1761 -1
831 1310
-1 1639
48 1572
-1 -1
-1 395
-1 47
-1 520
680 993
-1 -1
885 1449
117 757
924 -1
-1 -1
-1 1053
1752 1893
-1 907
-1 882
940 ...

output:

504771084

result:

ok "504771084"

Test #25:

score: 0
Accepted
time: 4ms
memory: 3868kb

input:

2000 2000
1846 -1
-1 354
363 1151
678 -1
-1 -1
606 1154
154 1931
633 857
1557 -1
124 1798
934 -1
-1 -1
-1 -1
1334 -1
-1 -1
-1 633
571 -1
834 944
1107 1545
390 1566
296 561
160 1589
745 -1
611 1142
287 779
-1 1546
1766 1807
-1 -1
-1 -1
541 -1
-1 -1
-1 562
1705 -1
1496 -1
-1 291
143 1934
-1 -1
342 -1
...

output:

164655804

result:

ok "164655804"

Test #26:

score: 0
Accepted
time: 8ms
memory: 3740kb

input:

2000 2000
1304 -1
1697 -1
-1 116
-1 -1
432 467
-1 225
-1 1135
-1 -1
56 -1
1949 -1
174 -1
-1 1742
-1 -1
1730 1947
-1 1008
-1 1404
1092 1267
-1 -1
-1 -1
-1 883
1602 -1
-1 -1
1124 1810
502 -1
-1 1557
396 -1
400 729
-1 -1
-1 -1
-1 1705
286 1719
277 -1
-1 -1
421 -1
-1 -1
574 1285
1265 1668
-1 794
-1 -1
5...

output:

433544803

result:

ok "433544803"

Test #27:

score: 0
Accepted
time: 3ms
memory: 3816kb

input:

2000 2000
651 1106
707 924
638 968
1080 1581
97 1257
261 1531
1160 1733
26 1571
743 1741
86 1283
1245 1536
393 511
793 1051
881 1755
1437 1613
15 795
337 1343
1723 1823
1059 1875
476 1927
524 1109
1870 1983
916 1750
363 1115
18 779
306 371
47 115
216 951
128 407
229 1986
1253 1683
397 1909
1785 1891...

output:

275736124

result:

ok "275736124"

Test #28:

score: 0
Accepted
time: 3ms
memory: 3780kb

input:

2000 2000
488 525
1086 1118
1812 1888
86 1836
459 491
782 1449
238 436
125 598
403 941
341 1296
358 1985
57 568
423 652
874 1862
324 1597
985 1746
22 753
251 642
172 378
487 1486
810 1564
874 1789
380 1097
215 1625
744 1532
1589 1952
9 1764
4 330
559 562
940 1836
364 386
91 1562
1191 1806
484 1823
5...

output:

647106319

result:

ok "647106319"

Test #29:

score: 0
Accepted
time: 9ms
memory: 4024kb

input:

2000 2000
-1 -1
-1 -1
-1 107
-1 -1
-1 -1
618 -1
-1 1936
42 -1
-1 456
-1 443
31 -1
-1 -1
750 -1
-1 -1
-1 141
-1 47
-1 515
-1 606
-1 -1
-1 -1
-1 40
581 -1
-1 482
-1 -1
790 -1
-1 -1
-1 557
-1 -1
-1 1308
1238 -1
-1 53
-1 -1
-1 -1
1956 -1
217 -1
-1 1972
-1 -1
1070 -1
-1 -1
-1 82
706 -1
-1 16
-1 -1
12 -1
...

output:

178213118

result:

ok "178213118"

Test #30:

score: 0
Accepted
time: 9ms
memory: 4024kb

input:

2000 2000
-1 -1
844 -1
-1 1719
-1 1682
1041 -1
1549 -1
-1 -1
-1 459
1404 -1
1862 -1
-1 1275
344 -1
1003 -1
-1 -1
-1 -1
1738 -1
804 -1
1481 -1
-1 1096
-1 -1
-1 -1
191 -1
-1 432
-1 -1
-1 -1
1538 -1
1525 -1
-1 -1
-1 946
-1 -1
-1 1292
1973 -1
1343 -1
-1 769
1129 -1
1722 -1
-1 -1
-1 -1
1295 -1
-1 741
-1 ...

output:

326294367

result:

ok "326294367"

Test #31:

score: 0
Accepted
time: 10ms
memory: 3784kb

input:

2000 2000
1019 -1
707 -1
551 -1
1755 -1
1231 -1
513 -1
1042 -1
-1 460
1557 -1
-1 899
-1 1083
-1 1262
-1 444
1811 -1
1831 -1
671 -1
105 -1
-1 1299
-1 1559
1491 -1
980 -1
1054 -1
-1 300
-1 934
1660 -1
-1 186
355 -1
1463 -1
-1 653
-1 678
-1 1569
1342 -1
-1 884
-1 126
1777 -1
-1 971
-1 13
-1 569
1072 -1...

output:

751548938

result:

ok "751548938"

Test #32:

score: 0
Accepted
time: 10ms
memory: 3868kb

input:

2000 2000
1539 -1
1305 -1
1660 -1
1742 -1
-1 1535
184 -1
211 -1
1908 -1
531 -1
1548 -1
1676 -1
1346 -1
-1 893
156 -1
1790 -1
170 -1
283 -1
1573 -1
-1 35
-1 838
1741 -1
1289 -1
-1 1650
-1 1410
1163 -1
1189 -1
1592 -1
-1 180
1829 -1
-1 574
1000 -1
-1 342
1928 -1
1498 -1
1572 -1
1138 -1
-1 1970
-1 1856...

output:

186475814

result:

ok "186475814"

Test #33:

score: 0
Accepted
time: 8ms
memory: 3784kb

input:

2000 2000
-1 1009
-1 1594
-1 1522
-1 311
-1 1679
-1 679
-1 103
-1 78
-1 797
-1 1530
-1 829
-1 1813
-1 383
-1 930
-1 712
-1 988
-1 1023
-1 1257
-1 1162
-1 17
-1 966
-1 1815
-1 680
-1 1315
-1 1348
-1 171
-1 200
-1 779
-1 1320
-1 1506
-1 937
-1 779
-1 161
-1 1640
-1 1999
-1 761
-1 871
-1 1934
-1 1228
-...

output:

63681103

result:

ok "63681103"

Test #34:

score: 0
Accepted
time: 8ms
memory: 3816kb

input:

2000 2000
-1 1505
-1 857
-1 54
-1 194
-1 368
-1 1154
-1 629
-1 175
-1 1287
-1 770
-1 1243
-1 1451
-1 507
-1 1328
-1 102
-1 75
-1 1357
-1 415
-1 499
-1 1736
-1 1214
-1 1702
-1 1521
-1 717
-1 863
-1 1534
-1 1349
-1 606
-1 1466
-1 1314
-1 1526
-1 1024
-1 579
-1 1788
-1 155
-1 615
-1 517
-1 569
-1 744
-...

output:

630266191

result:

ok "630266191"

Test #35:

score: 0
Accepted
time: 8ms
memory: 4080kb

input:

2000 2000
1047 -1
384 -1
1758 -1
1389 -1
740 -1
816 -1
203 -1
489 -1
569 -1
1276 -1
1531 -1
154 -1
1999 -1
379 -1
329 -1
203 -1
1452 -1
161 -1
1384 -1
559 -1
79 -1
208 -1
92 -1
1125 -1
1616 -1
384 -1
1082 -1
204 -1
1811 -1
1105 -1
58 -1
613 -1
1619 -1
1243 -1
1392 -1
1631 -1
653 -1
1373 -1
632 -1
15...

output:

114835083

result:

ok "114835083"

Test #36:

score: 0
Accepted
time: 9ms
memory: 4020kb

input:

2000 2000
1513 -1
3 -1
640 -1
1744 -1
1998 -1
960 -1
1676 -1
608 -1
56 -1
1794 -1
334 -1
999 -1
1739 -1
106 -1
711 -1
1579 -1
641 -1
1257 -1
544 -1
797 -1
479 -1
374 -1
1358 -1
1643 -1
1278 -1
714 -1
302 -1
1520 -1
1916 -1
67 -1
1581 -1
898 -1
778 -1
767 -1
1651 -1
76 -1
1235 -1
742 -1
1523 -1
370 -...

output:

429309272

result:

ok "429309272"

Test #37:

score: 0
Accepted
time: 0ms
memory: 3792kb

input:

2000 2000
-1 -1
-1 -1
-1 -1
-1 -1
-1 -1
-1 -1
-1 -1
-1 -1
-1 -1
-1 -1
-1 -1
-1 -1
-1 -1
-1 -1
-1 -1
-1 -1
-1 -1
-1 -1
-1 -1
-1 -1
-1 -1
-1 -1
-1 -1
-1 -1
-1 -1
-1 -1
-1 -1
-1 -1
-1 -1
-1 -1
-1 -1
-1 -1
-1 -1
-1 -1
-1 -1
-1 -1
-1 -1
-1 -1
-1 -1
-1 -1
-1 -1
-1 -1
-1 -1
-1 -1
-1 -1
-1 -1
-1 -1
-1 -1
-1...

output:

708459481

result:

ok "708459481"

Test #38:

score: 0
Accepted
time: 839ms
memory: 16500kb

input:

39371 73505
-1 67150
-1 57770
42317 -1
-1 -1
65127 -1
48039 57936
16817 -1
46051 52105
-1 44100
33469 43807
-1 66673
-1 -1
83 68755
-1 65324
11453 31180
-1 2443
35823 -1
-1 -1
-1 -1
8433 24104
37008 55794
21490 -1
-1 23813
59037 -1
-1 43665
-1 -1
25001 42758
18631 -1
29828 -1
-1 -1
-1 -1
43963 -1
37...

output:

417807573

result:

ok "417807573"

Test #39:

score: 0
Accepted
time: 354ms
memory: 15936kb

input:

516 68842
17469 -1
23357 -1
8760 46430
7432 -1
-1 48636
19607 -1
41983 -1
-1 17512
47499 -1
11213 38541
21538 -1
32032 68432
-1 35755
-1 54811
37739 63152
-1 21436
585 5674
-1 -1
-1 -1
-1 -1
51261 51348
-1 28993
3606 22514
-1 64998
20340 48659
35722 65937
-1 -1
-1 64396
-1 -1
-1 -1
1482 50269
28310 ...

output:

944392637

result:

ok "944392637"

Test #40:

score: 0
Accepted
time: 345ms
memory: 10016kb

input:

11928 51104
7727 -1
12632 13001
-1 -1
-1 -1
6272 50896
23549 -1
-1 -1
-1 43881
27531 48840
-1 45956
-1 41853
27809 33690
-1 16089
27024 39291
-1 -1
5238 -1
36436 -1
-1 22559
40105 -1
20976 37615
-1 -1
-1 6246
17537 47844
8564 9869
-1 2246
24489 -1
-1 -1
10003 17239
-1 -1
-1 -1
3415 25065
3283 25074
...

output:

760980906

result:

ok "760980906"

Test #41:

score: 0
Accepted
time: 505ms
memory: 10676kb

input:

87869 56606
24516 39428
26004 -1
-1 48577
-1 17564
-1 -1
33985 -1
55338 -1
-1 51915
20824 -1
-1 17939
7402 45187
31118 -1
8297 20482
32329 36079
10683 36084
-1 5542
30020 -1
12935 13532
-1 25603
15811 -1
-1 6534
38966 -1
-1 -1
13907 -1
23761 45077
-1 14130
35441 51955
-1 -1
-1 -1
21280 -1
7405 32321...

output:

876373628

result:

ok "876373628"

Test #42:

score: 0
Accepted
time: 216ms
memory: 6764kb

input:

40641 26318
7109 13581
25097 -1
23267 -1
1102 13576
12052 -1
12801 18315
-1 9329
-1 -1
4332 -1
10112 20954
666 -1
14616 -1
-1 25123
-1 14856
-1 -1
-1 4259
4865 -1
-1 -1
253 -1
9566 -1
7063 -1
-1 -1
5486 -1
1559 -1
9327 22486
9521 13718
862 13292
4843 7815
14881 16553
25182 -1
-1 -1
-1 -1
10534 -1
11...

output:

603095629

result:

ok "603095629"

Test #43:

score: 0
Accepted
time: 979ms
memory: 17608kb

input:

100000 100000
80910 85031
-1 46621
61333 99990
89041 -1
-1 46204
48529 79326
-1 -1
2747 55365
-1 -1
-1 5862
3891 54076
-1 90774
50016 -1
-1 13462
41283 -1
7290 8385
961 78037
61802 -1
-1 20292
72341 -1
-1 -1
98901 -1
32950 -1
61160 -1
-1 -1
84609 -1
60542 -1
54713 -1
35383 88820
58869 -1
14739 -1
49...

output:

875564287

result:

ok "875564287"

Test #44:

score: 0
Accepted
time: 992ms
memory: 17616kb

input:

100000 100000
-1 -1
-1 -1
58262 66732
58914 61320
67630 69377
11169 40789
52235 76000
-1 38693
-1 -1
-1 51837
-1 -1
-1 -1
-1 20873
-1 86015
61867 -1
-1 79893
-1 43481
32490 -1
-1 -1
-1 -1
-1 34584
15429 52880
-1 96933
5109 -1
373 -1
-1 -1
73425 -1
31557 -1
-1 24025
75347 -1
-1 -1
-1 -1
14028 -1
3929...

output:

711299685

result:

ok "711299685"

Test #45:

score: 0
Accepted
time: 996ms
memory: 17668kb

input:

100000 100000
75183 78136
35617 -1
-1 18706
57048 -1
-1 -1
-1 27906
31253 -1
86063 -1
-1 53125
20587 -1
-1 -1
88024 -1
-1 92960
16945 83958
-1 47260
48746 -1
-1 -1
30210 -1
54460 75100
8786 -1
-1 -1
-1 -1
68101 -1
12156 93667
-1 -1
44107 73979
54642 70186
-1 -1
-1 95729
-1 20306
42929 -1
28087 -1
-1...

output:

51364591

result:

ok "51364591"

Test #46:

score: 0
Accepted
time: 988ms
memory: 17616kb

input:

100000 100000
-1 29231
27579 -1
-1 -1
-1 56749
-1 -1
-1 -1
-1 -1
42070 -1
-1 15181
-1 5211
72963 -1
-1 -1
72323 -1
-1 50903
-1 97727
-1 -1
-1 -1
-1 17913
-1 83302
-1 27183
-1 -1
-1 88000
19902 -1
78405 -1
61740 -1
36441 37528
24828 95216
-1 -1
-1 99257
82763 -1
85843 -1
7610 26057
-1 -1
54130 -1
532...

output:

749999568

result:

ok "749999568"

Test #47:

score: 0
Accepted
time: 974ms
memory: 17508kb

input:

100000 100000
-1 -1
-1 -1
71455 87994
-1 -1
-1 70805
-1 6368
39972 68521
-1 46049
21208 91408
68 39582
-1 36681
63072 -1
76257 -1
-1 -1
-1 -1
-1 -1
-1 9393
-1 63750
-1 99364
-1 1479
-1 13423
-1 3420
10066 -1
-1 -1
-1 -1
-1 -1
-1 -1
58639 -1
-1 18319
-1 80225
25804 -1
-1 -1
68756 -1
-1 -1
16180 -1
40...

output:

930185694

result:

ok "930185694"

Test #48:

score: 0
Accepted
time: 130ms
memory: 11296kb

input:

100000 100000
56199 91291
40437 82521
19552 81440
12739 83409
53402 77939
42064 63128
4342 65251
38574 74433
4013 13097
3550 70914
52773 56209
32166 76985
95408 97749
57359 84113
66381 70787
39433 68501
12977 13199
8762 22385
1055 18446
36373 92518
925 39093
23947 59419
44418 92857
22872 80638
47058...

output:

49931541

result:

ok "49931541"

Test #49:

score: 0
Accepted
time: 127ms
memory: 11260kb

input:

100000 100000
50106 86783
60571 70716
47204 54138
55639 62784
36078 84941
6279 21915
4868 83374
28166 58000
1682 67802
9121 23778
59200 84282
28024 78232
38001 46997
24442 72617
18579 29514
46860 80122
26555 91391
80518 97471
7833 38804
72763 96909
7043 60172
35749 96351
68580 75643
18826 83843
1136...

output:

658828321

result:

ok "658828321"

Test #50:

score: 0
Accepted
time: 1042ms
memory: 17632kb

input:

100000 100000
52142 -1
-1 -1
-1 -1
-1 30266
-1 -1
35323 -1
79660 -1
13258 -1
34358 -1
36621 -1
26780 -1
-1 -1
50361 -1
-1 -1
65478 -1
-1 -1
-1 -1
11658 -1
-1 -1
-1 -1
-1 76128
-1 19735
4957 -1
-1 -1
-1 -1
-1 313
34883 -1
79739 -1
40675 -1
-1 -1
-1 86093
-1 -1
-1 -1
-1 -1
-1 -1
-1 -1
-1 -1
-1 -1
-1 -...

output:

208320674

result:

ok "208320674"

Test #51:

score: 0
Accepted
time: 1028ms
memory: 17536kb

input:

100000 100000
53906 -1
-1 -1
-1 90057
-1 -1
-1 67689
-1 -1
-1 80045
-1 -1
-1 -1
1418 -1
65159 -1
-1 32307
-1 -1
-1 23951
-1 -1
-1 -1
-1 -1
-1 64225
-1 19711
80412 -1
-1 -1
-1 -1
-1 -1
-1 60840
-1 71629
-1 93515
35923 -1
-1 -1
51987 -1
-1 -1
65197 -1
12027 -1
-1 -1
-1 -1
-1 75962
83550 -1
-1 -1
-1 12...

output:

383054357

result:

ok "383054357"

Test #52:

score: 0
Accepted
time: 1078ms
memory: 17860kb

input:

100000 100000
63575 -1
-1 7400
79813 -1
98399 -1
13873 -1
14572 -1
19061 -1
70999 -1
77559 -1
65265 -1
4346 -1
-1 74813
-1 7985
-1 35710
35861 -1
79524 -1
30778 -1
67501 -1
-1 22305
-1 44157
-1 62332
-1 5628
-1 18973
27455 -1
-1 48414
66056 -1
24981 -1
68294 -1
21015 -1
-1 46011
-1 5575
4417 -1
6624...

output:

503409689

result:

ok "503409689"

Test #53:

score: 0
Accepted
time: 1070ms
memory: 17992kb

input:

100000 100000
-1 33901
-1 66021
62619 -1
84968 -1
5973 -1
221 -1
74746 -1
-1 8218
66325 -1
81801 -1
-1 19236
93697 -1
10390 -1
-1 37512
-1 28713
57237 -1
84838 -1
18557 -1
57091 -1
74599 -1
46617 -1
-1 64742
-1 3585
23920 -1
96818 -1
91194 -1
-1 34039
-1 76341
63222 -1
-1 4070
52549 -1
52320 -1
2529...

output:

542286132

result:

ok "542286132"

Test #54:

score: 0
Accepted
time: 1079ms
memory: 17800kb

input:

100000 100000
-1 74707
-1 79497
80831 -1
-1 13660
-1 2355
-1 3193
93434 -1
46852 -1
-1 71434
56165 -1
72138 -1
-1 60877
53744 -1
-1 29414
-1 79316
-1 34371
-1 83159
70157 -1
-1 10459
44011 -1
-1 89285
2721 -1
-1 81322
64654 -1
-1 55226
-1 88643
55364 -1
-1 29667
-1 88510
-1 68812
-1 95576
-1 36116
-...

output:

915309845

result:

ok "915309845"

Test #55:

score: 0
Accepted
time: 1062ms
memory: 17528kb

input:

100000 100000
-1 30060
20079 -1
-1 23063
21687 -1
-1 69283
-1 60835
42463 -1
69629 -1
-1 74776
36689 -1
89922 -1
-1 8379
214 -1
3715 -1
77111 -1
28023 -1
-1 12596
98730 -1
-1 1776
52953 -1
50069 -1
-1 51070
-1 83557
-1 4535
-1 50618
16148 -1
70826 -1
50838 -1
-1 59590
-1 88742
24648 -1
55639 -1
2375...

output:

923853144

result:

ok "923853144"

Test #56:

score: 0
Accepted
time: 1306ms
memory: 18000kb

input:

100000 100000
-1 99984
-1 99213
145 -1
-1 99053
318 -1
-1 99009
-1 99565
-1 99220
-1 99334
-1 99870
-1 99547
342 -1
621 -1
568 -1
526 -1
26 -1
916 -1
565 -1
526 -1
-1 99107
-1 99362
462 -1
-1 99471
-1 99151
-1 99022
487 -1
-1 99020
458 -1
907 -1
676 -1
927 -1
-1 99216
253 -1
589 -1
-1 99880
-1 99874...

output:

128192368

result:

ok "128192368"

Test #57:

score: 0
Accepted
time: 669ms
memory: 18080kb

input:

100000 100000
-1 32146
-1 6895
-1 43504
-1 94964
-1 5082
-1 69541
-1 58879
-1 9425
-1 9722
-1 19108
-1 77933
-1 30806
-1 10667
-1 90570
-1 77965
-1 65368
-1 82544
-1 78636
-1 91380
-1 50002
-1 66292
-1 23842
-1 38060
-1 90192
-1 4081
-1 94755
-1 57313
-1 55556
-1 20864
-1 51424
-1 17220
-1 51489
-1 ...

output:

461531218

result:

ok "461531218"

Test #58:

score: 0
Accepted
time: 681ms
memory: 18052kb

input:

100000 100000
-1 24112
-1 29415
-1 59008
-1 59525
-1 72057
-1 32796
-1 53056
-1 39914
-1 60183
-1 29066
-1 30319
-1 22292
-1 75168
-1 30075
-1 730
-1 57883
-1 71736
-1 22823
-1 33880
-1 27859
-1 42291
-1 80947
-1 25413
-1 38888
-1 50895
-1 51239
-1 40782
-1 48036
-1 20833
-1 6017
-1 11031
-1 13706
-...

output:

685855997

result:

ok "685855997"

Test #59:

score: 0
Accepted
time: 1394ms
memory: 30380kb

input:

100000 100000
-1 99946
-1 99374
-1 99695
-1 99908
-1 99425
-1 99298
-1 99444
-1 99478
-1 99074
-1 99338
-1 99540
-1 99466
-1 99773
-1 99256
-1 99117
-1 99903
-1 99889
-1 99727
-1 99067
-1 99458
-1 99391
-1 99324
-1 99169
-1 99185
-1 99213
-1 99811
-1 99974
-1 99273
-1 99299
-1 99296
-1 99418
-1 9934...

output:

741344412

result:

ok "741344412"

Test #60:

score: 0
Accepted
time: 682ms
memory: 17432kb

input:

100000 100000
88122 -1
22419 -1
72915 -1
16790 -1
82911 -1
68163 -1
97964 -1
99368 -1
62160 -1
81235 -1
27503 -1
57865 -1
5780 -1
32671 -1
91062 -1
96378 -1
36135 -1
88493 -1
26161 -1
50422 -1
20914 -1
84193 -1
33641 -1
73209 -1
65369 -1
87331 -1
31318 -1
62567 -1
68644 -1
31605 -1
82937 -1
22518 -1...

output:

274568803

result:

ok "274568803"

Test #61:

score: 0
Accepted
time: 671ms
memory: 17064kb

input:

100000 100000
65732 -1
11100 -1
71587 -1
75320 -1
9952 -1
47148 -1
20555 -1
64361 -1
43541 -1
4169 -1
95423 -1
83066 -1
71053 -1
68728 -1
14445 -1
34486 -1
67223 -1
94492 -1
15553 -1
22406 -1
35564 -1
40226 -1
72709 -1
93475 -1
38314 -1
18136 -1
47848 -1
6843 -1
14004 -1
68815 -1
81926 -1
88686 -1
4...

output:

110203772

result:

ok "110203772"

Test #62:

score: 0
Accepted
time: 1383ms
memory: 29476kb

input:

100000 100000
138 -1
533 -1
344 -1
58 -1
284 -1
760 -1
235 -1
908 -1
285 -1
402 -1
516 -1
780 -1
412 -1
128 -1
869 -1
95 -1
375 -1
577 -1
444 -1
474 -1
714 -1
538 -1
196 -1
58 -1
628 -1
764 -1
389 -1
209 -1
673 -1
352 -1
921 -1
725 -1
781 -1
108 -1
560 -1
158 -1
7 -1
310 -1
433 -1
214 -1
824 -1
103 ...

output:

399221708

result:

ok "399221708"

Test #63:

score: 0
Accepted
time: 119ms
memory: 8760kb

input:

100000 100000
-1 -1
-1 -1
-1 -1
-1 -1
-1 -1
-1 -1
-1 -1
-1 -1
-1 -1
-1 -1
-1 -1
-1 -1
-1 -1
-1 -1
-1 -1
-1 -1
-1 -1
-1 -1
-1 -1
-1 -1
-1 -1
-1 -1
-1 -1
-1 -1
-1 -1
-1 -1
-1 -1
-1 -1
-1 -1
-1 -1
-1 -1
-1 -1
-1 -1
-1 -1
-1 -1
-1 -1
-1 -1
-1 -1
-1 -1
-1 -1
-1 -1
-1 -1
-1 -1
-1 -1
-1 -1
-1 -1
-1 -1
-1 -...

output:

682834676

result:

ok "682834676"

Test #64:

score: 0
Accepted
time: 167ms
memory: 12588kb

input:

100000 100000
-1 1
-1 1
-1 1
-1 1
-1 1
-1 1
-1 1
-1 1
-1 1
-1 1
-1 1
-1 1
-1 1
-1 1
-1 1
-1 1
-1 1
-1 1
-1 1
-1 1
-1 1
-1 1
-1 1
-1 1
-1 1
-1 1
-1 1
-1 1
-1 1
-1 1
-1 1
-1 1
-1 1
-1 1
-1 1
-1 1
-1 1
-1 1
-1 1
-1 1
-1 1
-1 1
-1 1
-1 1
-1 1
-1 1
-1 1
-1 1
-1 1
-1 1
-1 1
-1 1
-1 1
-1 1
-1 1
-1 1
-1 1
-...

output:

538261387

result:

ok "538261387"

Test #65:

score: 0
Accepted
time: 1666ms
memory: 30640kb

input:

100000 100000
-1 100000
-1 100000
-1 100000
-1 100000
-1 100000
-1 100000
-1 100000
-1 100000
-1 100000
-1 100000
-1 100000
-1 100000
-1 100000
-1 100000
-1 100000
-1 100000
-1 100000
-1 100000
-1 100000
-1 100000
-1 100000
-1 100000
-1 100000
-1 100000
-1 100000
-1 100000
-1 100000
-1 100000
-1 100...

output:

168783817

result:

ok "168783817"

Test #66:

score: 0
Accepted
time: 1669ms
memory: 30440kb

input:

100000 100000
-1 99999
-1 99999
-1 99999
-1 99999
-1 99999
-1 99999
-1 99999
-1 99999
-1 99999
-1 99999
-1 99999
-1 99999
-1 99999
-1 99999
-1 99999
-1 99999
-1 99999
-1 99999
-1 99999
-1 99999
-1 99999
-1 99999
-1 99999
-1 99999
-1 99999
-1 99999
-1 99999
-1 99999
-1 99999
-1 99999
-1 99999
-1 9999...

output:

911213386

result:

ok "911213386"

Test #67:

score: 0
Accepted
time: 1055ms
memory: 30568kb

input:

100000 100000
-1 65536
-1 65536
-1 65536
-1 65536
-1 65536
-1 65536
-1 65536
-1 65536
-1 65536
-1 65536
-1 65536
-1 65536
-1 65536
-1 65536
-1 65536
-1 65536
-1 65536
-1 65536
-1 65536
-1 65536
-1 65536
-1 65536
-1 65536
-1 65536
-1 65536
-1 65536
-1 65536
-1 65536
-1 65536
-1 65536
-1 65536
-1 6553...

output:

958020328

result:

ok "958020328"

Test #68:

score: 0
Accepted
time: 1037ms
memory: 30652kb

input:

100000 100000
-1 65535
-1 65535
-1 65535
-1 65535
-1 65535
-1 65535
-1 65535
-1 65535
-1 65535
-1 65535
-1 65535
-1 65535
-1 65535
-1 65535
-1 65535
-1 65535
-1 65535
-1 65535
-1 65535
-1 65535
-1 65535
-1 65535
-1 65535
-1 65535
-1 65535
-1 65535
-1 65535
-1 65535
-1 65535
-1 65535
-1 65535
-1 6553...

output:

376123473

result:

ok "376123473"

Test #69:

score: 0
Accepted
time: 1027ms
memory: 30624kb

input:

100000 100000
-1 65537
-1 65537
-1 65537
-1 65537
-1 65537
-1 65537
-1 65537
-1 65537
-1 65537
-1 65537
-1 65537
-1 65537
-1 65537
-1 65537
-1 65537
-1 65537
-1 65537
-1 65537
-1 65537
-1 65537
-1 65537
-1 65537
-1 65537
-1 65537
-1 65537
-1 65537
-1 65537
-1 65537
-1 65537
-1 65537
-1 65537
-1 6553...

output:

558998334

result:

ok "558998334"

Test #70:

score: 0
Accepted
time: 1652ms
memory: 30536kb

input:

100000 100000
53486 -1
39997 88519
84325 -1
37606 -1
66183 79560
-1 10303
-1 53157
8761 -1
12793 67803
33675 -1
12761 47448
-1 93895
49265 67228
7378 -1
-1 57452
67968 76339
47096 -1
49571 62188
31863 91277
35138 -1
73437 94561
46238 60564
-1 62721
15721 38820
39037 43534
85549 -1
76266 -1
70417 892...

output:

234032781

result:

ok "234032781"

Test #71:

score: 0
Accepted
time: 1669ms
memory: 29632kb

input:

100000 100000
1 -1
1 -1
1 -1
1 -1
1 -1
1 -1
1 -1
1 -1
1 -1
1 -1
1 -1
1 -1
1 -1
1 -1
1 -1
1 -1
1 -1
1 -1
1 -1
1 -1
1 -1
1 -1
1 -1
1 -1
1 -1
1 -1
1 -1
1 -1
1 -1
1 -1
1 -1
1 -1
1 -1
1 -1
1 -1
1 -1
1 -1
1 -1
1 -1
1 -1
1 -1
1 -1
1 -1
1 -1
1 -1
1 -1
1 -1
1 -1
1 -1
1 -1
1 -1
1 -1
1 -1
1 -1
1 -1
1 -1
1 -1
1...

output:

168783817

result:

ok "168783817"

Test #72:

score: 0
Accepted
time: 1679ms
memory: 29836kb

input:

100000 100000
2 -1
2 -1
2 -1
2 -1
2 -1
2 -1
2 -1
2 -1
2 -1
2 -1
2 -1
2 -1
2 -1
2 -1
2 -1
2 -1
2 -1
2 -1
2 -1
2 -1
2 -1
2 -1
2 -1
2 -1
2 -1
2 -1
2 -1
2 -1
2 -1
2 -1
2 -1
2 -1
2 -1
2 -1
2 -1
2 -1
2 -1
2 -1
2 -1
2 -1
2 -1
2 -1
2 -1
2 -1
2 -1
2 -1
2 -1
2 -1
2 -1
2 -1
2 -1
2 -1
2 -1
2 -1
2 -1
2 -1
2 -1
2...

output:

911213386

result:

ok "911213386"

Test #73:

score: 0
Accepted
time: 169ms
memory: 11744kb

input:

100000 100000
100000 -1
100000 -1
100000 -1
100000 -1
100000 -1
100000 -1
100000 -1
100000 -1
100000 -1
100000 -1
100000 -1
100000 -1
100000 -1
100000 -1
100000 -1
100000 -1
100000 -1
100000 -1
100000 -1
100000 -1
100000 -1
100000 -1
100000 -1
100000 -1
100000 -1
100000 -1
100000 -1
100000 -1
100000...

output:

538261387

result:

ok "538261387"

Test #74:

score: 0
Accepted
time: 1052ms
memory: 29784kb

input:

100000 100000
34465 -1
34465 -1
34465 -1
34465 -1
34465 -1
34465 -1
34465 -1
34465 -1
34465 -1
34465 -1
34465 -1
34465 -1
34465 -1
34465 -1
34465 -1
34465 -1
34465 -1
34465 -1
34465 -1
34465 -1
34465 -1
34465 -1
34465 -1
34465 -1
34465 -1
34465 -1
34465 -1
34465 -1
34465 -1
34465 -1
34465 -1
34465 -...

output:

958020328

result:

ok "958020328"

Test #75:

score: 0
Accepted
time: 1024ms
memory: 29720kb

input:

100000 100000
34466 -1
34466 -1
34466 -1
34466 -1
34466 -1
34466 -1
34466 -1
34466 -1
34466 -1
34466 -1
34466 -1
34466 -1
34466 -1
34466 -1
34466 -1
34466 -1
34466 -1
34466 -1
34466 -1
34466 -1
34466 -1
34466 -1
34466 -1
34466 -1
34466 -1
34466 -1
34466 -1
34466 -1
34466 -1
34466 -1
34466 -1
34466 -...

output:

376123473

result:

ok "376123473"

Test #76:

score: 0
Accepted
time: 1031ms
memory: 29700kb

input:

100000 100000
34464 -1
34464 -1
34464 -1
34464 -1
34464 -1
34464 -1
34464 -1
34464 -1
34464 -1
34464 -1
34464 -1
34464 -1
34464 -1
34464 -1
34464 -1
34464 -1
34464 -1
34464 -1
34464 -1
34464 -1
34464 -1
34464 -1
34464 -1
34464 -1
34464 -1
34464 -1
34464 -1
34464 -1
34464 -1
34464 -1
34464 -1
34464 -...

output:

558998334

result:

ok "558998334"

Test #77:

score: 0
Accepted
time: 2229ms
memory: 29712kb

input:

100000 100000
-1 67653
29813 -1
8485 85934
12402 23734
-1 84172
7904 30261
71842 -1
-1 58480
7175 84465
65363 95224
20337 81386
-1 91396
741 51360
-1 52196
3940 86117
1982 9289
18819 65495
4767 -1
24735 99523
34668 60575
6755 83029
80722 87776
55520 79079
32800 39607
45123 50000
33895 38990
31019 -1...

output:

367637584

result:

ok "367637584"

Extra Test:

score: 0
Extra Test Passed