結果

問題 No.2101 [Cherry Alpha N] ずっとこの数列だったらいいのに
ユーザー PachicobuePachicobue
提出日時 2022-10-14 22:40:35
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,363 ms / 6,000 ms
コード長 21,462 bytes
コンパイル時間 2,481 ms
コンパイル使用メモリ 226,924 KB
実行使用メモリ 89,076 KB
最終ジャッジ日時 2024-06-26 16:14:23
合計ジャッジ時間 45,231 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 3 ms
5,376 KB
testcase_02 AC 3 ms
5,376 KB
testcase_03 AC 558 ms
54,836 KB
testcase_04 AC 917 ms
69,304 KB
testcase_05 AC 498 ms
44,792 KB
testcase_06 AC 812 ms
65,280 KB
testcase_07 AC 940 ms
73,428 KB
testcase_08 AC 858 ms
68,552 KB
testcase_09 AC 515 ms
40,488 KB
testcase_10 AC 365 ms
36,996 KB
testcase_11 AC 595 ms
48,708 KB
testcase_12 AC 571 ms
49,188 KB
testcase_13 AC 408 ms
39,048 KB
testcase_14 AC 364 ms
28,288 KB
testcase_15 AC 1,061 ms
77,648 KB
testcase_16 AC 909 ms
70,324 KB
testcase_17 AC 260 ms
24,176 KB
testcase_18 AC 1,238 ms
89,000 KB
testcase_19 AC 1,363 ms
89,004 KB
testcase_20 AC 1,210 ms
88,812 KB
testcase_21 AC 1,350 ms
88,816 KB
testcase_22 AC 1,326 ms
89,000 KB
testcase_23 AC 1,238 ms
88,972 KB
testcase_24 AC 1,203 ms
88,944 KB
testcase_25 AC 1,263 ms
88,972 KB
testcase_26 AC 1,333 ms
88,940 KB
testcase_27 AC 1,272 ms
88,964 KB
testcase_28 AC 1,237 ms
88,872 KB
testcase_29 AC 1,224 ms
88,920 KB
testcase_30 AC 1,321 ms
88,872 KB
testcase_31 AC 1,190 ms
88,952 KB
testcase_32 AC 1,240 ms
88,876 KB
testcase_33 AC 1,246 ms
88,880 KB
testcase_34 AC 1,252 ms
88,920 KB
testcase_35 AC 1,189 ms
88,832 KB
testcase_36 AC 1,196 ms
88,880 KB
testcase_37 AC 1,217 ms
89,076 KB
testcase_38 AC 140 ms
29,824 KB
testcase_39 AC 255 ms
29,224 KB
testcase_40 AC 177 ms
27,932 KB
testcase_41 AC 2 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using i32 = int;
using u32 = unsigned int;
using i64 = long long;
using u64 = unsigned long long;
using i128 = __int128_t;
using u128 = __uint128_t;
using f64 = double;
using f80 = long double;
using f128 = __float128;
constexpr i32 operator"" _i32(u64 v) { return v; }
constexpr u32 operator"" _u32(u64 v) { return v; }
constexpr i64 operator"" _i64(u64 v) { return v; }
constexpr u64 operator"" _u64(u64 v) { return v; }
constexpr f64 operator"" _f64(f80 v) { return v; }
constexpr f80 operator"" _f80(f80 v) { return v; }
using Istream = std::istream;
using Ostream = std::ostream;
using Str = std::string;
template<typename T>
using Lt = std::less<T>;
template<typename T>
using Gt = std::greater<T>;
template<typename T>
using IList = std::initializer_list<T>;
template<int n>
using BSet = std::bitset<n>;
template<typename T1, typename T2>
using Pair = std::pair<T1, T2>;
template<typename... Ts>
using Tup = std::tuple<Ts...>;
template<typename T, int N>
using Arr = std::array<T, N>;
template<typename... Ts>
using Deq = std::deque<Ts...>;
template<typename... Ts>
using Set = std::set<Ts...>;
template<typename... Ts>
using MSet = std::multiset<Ts...>;
template<typename... Ts>
using USet = std::unordered_set<Ts...>;
template<typename... Ts>
using UMSet = std::unordered_multiset<Ts...>;
template<typename... Ts>
using Map = std::map<Ts...>;
template<typename... Ts>
using MMap = std::multimap<Ts...>;
template<typename... Ts>
using UMap = std::unordered_map<Ts...>;
template<typename... Ts>
using UMMap = std::unordered_multimap<Ts...>;
template<typename... Ts>
using Vec = std::vector<Ts...>;
template<typename... Ts>
using Stack = std::stack<Ts...>;
template<typename... Ts>
using Queue = std::queue<Ts...>;
template<typename T>
using MaxHeap = std::priority_queue<T>;
template<typename T>
using MinHeap = std::priority_queue<T, Vec<T>, Gt<T>>;
using NSec = std::chrono::nanoseconds;
using USec = std::chrono::microseconds;
using MSec = std::chrono::milliseconds;
using Sec = std::chrono::seconds;
constexpr bool LOCAL = false;
constexpr bool OJ = not LOCAL;
template<typename T>
static constexpr T OjLocal(T oj, T local)
{
    return LOCAL ? local : oj;
}
template<typename T>
constexpr T LIMMIN = std::numeric_limits<T>::min();
template<typename T>
constexpr T LIMMAX = std::numeric_limits<T>::max();
template<typename T>
constexpr T INF = (LIMMAX<T> - 1) / 2;
template<typename T>
constexpr T PI = T{3.141592653589793238462643383279502884};
template<typename T = u64>
constexpr T TEN(int n)
{
    return n == 0 ? T{1} : TEN<T>(n - 1) * T{10};
}
template<typename T>
constexpr Vec<T>& operator+=(Vec<T>& vs1, const Vec<T>& vs2)
{
    return vs1.insert(vs1.end(), vs2.begin(), vs2.end()), vs1;
}
template<typename T>
constexpr Vec<T> operator+(const Vec<T>& vs1, const Vec<T>& vs2)
{
    auto vs = vs1;
    vs += vs2;
    return vs;
}
template<typename T>
constexpr bool chmin(T& a, const T& b)
{
    return (a > b ? (a = b, true) : false);
}
template<typename T>
constexpr bool chmax(T& a, const T& b)
{
    return (a < b ? (a = b, true) : false);
}
template<typename T>
constexpr T floorDiv(T x, T y)
{
    assert(y != 0);
    if (y < T{}) { x = -x, y = -y; }
    return x >= T{} ? x / y : (x - y + 1) / y;
}
template<typename T>
constexpr T ceilDiv(T x, T y)
{
    assert(y != 0);
    if (y < T{}) { x = -x, y = -y; }
    return x >= T{} ? (x + y - 1) / y : x / y;
}
template<typename T, typename I>
constexpr T powerMonoid(T v, I n, const T& e)
{
    assert(n >= 0);
    T ans = e;
    for (; n > 0; n >>= 1, v *= v) {
        if (n % 2 == 1) { ans *= v; }
    }
    return ans;
}
template<typename T, typename I>
constexpr T powerInt(T v, I n)
{
    return powerMonoid(v, n, T{1});
}
template<typename Vs, typename V>
constexpr void fillAll(Vs& arr, const V& v)
{
    if constexpr (std::is_convertible<V, Vs>::value) {
        arr = v;
    } else {
        for (auto& subarr : arr) { fillAll(subarr, v); }
    }
}
template<typename Vs>
constexpr void sortAll(Vs& vs)
{
    std::sort(std::begin(vs), std::end(vs));
}
template<typename Vs, typename C>
constexpr void sortAll(Vs& vs, C comp)
{
    std::sort(std::begin(vs), std::end(vs), comp);
}
template<typename Vs>
constexpr void reverseAll(Vs& vs)
{
    std::reverse(std::begin(vs), std::end(vs));
}
template<typename V, typename Vs>
constexpr V sumAll(const Vs& vs)
{
    if constexpr (std::is_convertible<Vs, V>::value) {
        return static_cast<V>(vs);
    } else {
        V ans = 0;
        for (const auto& v : vs) { ans += sumAll<V>(v); }
        return ans;
    }
}
template<typename Vs>
constexpr int minInd(const Vs& vs)
{
    return std::min_element(std::begin(vs), std::end(vs)) - std::begin(vs);
}
template<typename Vs>
constexpr int maxInd(const Vs& vs)
{
    return std::max_element(std::begin(vs), std::end(vs)) - std::begin(vs);
}
template<typename Vs, typename V>
constexpr int lbInd(const Vs& vs, const V& v)
{
    return std::lower_bound(std::begin(vs), std::end(vs), v) - std::begin(vs);
}
template<typename Vs, typename V>
constexpr int ubInd(const Vs& vs, const V& v)
{
    return std::upper_bound(std::begin(vs), std::end(vs), v) - std::begin(vs);
}
template<typename Vs, typename V>
constexpr void plusAll(Vs& vs, const V& v)
{
    for (auto& v_ : vs) { v_ += v; }
}
template<typename T, typename F>
constexpr Vec<T> genVec(int n, F gen)
{
    Vec<T> ans;
    std::generate_n(std::back_insert_iterator(ans), n, gen);
    return ans;
}
template<typename T = int>
constexpr Vec<T> iotaVec(int n, T offset = 0)
{
    Vec<T> ans(n);
    std::iota(ans.begin(), ans.end(), offset);
    return ans;
}
Ostream& operator<<(Ostream& os, i128 v)
{
    bool minus = false;
    if (v < 0) { minus = true, v = -v; }
    Str ans;
    if (v == 0) { ans = "0"; }
    while (v) { ans.push_back('0' + v % 10), v /= 10; }
    std::reverse(ans.begin(), ans.end());
    return os << (minus ? "-" : "") << ans;
}
Ostream& operator<<(Ostream& os, u128 v)
{
    Str ans;
    if (v == 0) { ans = "0"; }
    while (v) { ans.push_back('0' + v % 10), v /= 10; }
    std::reverse(ans.begin(), ans.end());
    return os << ans;
}
constexpr int popcount(u64 v) { return v ? __builtin_popcountll(v) : 0; }
constexpr int log2p1(u64 v) { return v ? 64 - __builtin_clzll(v) : 0; }
constexpr int lsbp1(u64 v) { return __builtin_ffsll(v); }
constexpr int ceillog(u64 v) { return v ? log2p1(v - 1) : 0; }
constexpr u64 ceil2(u64 v)
{
    assert(v <= (1_u64 << 63));
    return 1_u64 << ceillog(v);
}
constexpr u64 floor2(u64 v) { return v ? (1_u64 << (log2p1(v) - 1)) : 0_u64; }
constexpr bool ispow2(u64 v) { return (v > 0) and ((v & (v - 1)) == 0); }
constexpr bool btest(u64 mask, int ind) { return (mask >> ind) & 1_u64; }
template<typename F>
struct Fix : F
{
    constexpr Fix(F&& f) : F{std::forward<F>(f)} {}
    template<typename... Args>
    constexpr auto operator()(Args&&... args) const
    {
        return F::operator()(*this, std::forward<Args>(args)...);
    }
};
class irange
{
private:
    struct itr
    {
        constexpr itr(i64 start = 0, i64 step = 1) : m_cnt{start}, m_step{step} {}
        constexpr bool operator!=(const itr& it) const { return m_cnt != it.m_cnt; }
        constexpr i64 operator*() { return m_cnt; }
        constexpr itr& operator++() { return m_cnt += m_step, *this; }
        i64 m_cnt, m_step;
    };
    i64 m_start, m_end, m_step;
public:
    static constexpr i64 cnt(i64 start, i64 end, i64 step)
    {
        if (step == 0) { return -1; }
        const i64 d = (step > 0 ? step : -step);
        const i64 l = (step > 0 ? start : end);
        const i64 r = (step > 0 ? end : start);
        i64 n = (r - l) / d + ((r - l) % d ? 1 : 0);
        if (l >= r) { n = 0; }
        return n;
    }
    constexpr irange(i64 start, i64 end, i64 step = 1)
        : m_start{start}, m_end{m_start + step * cnt(start, end, step)}, m_step{step}
    {
        assert(step != 0);
    }
    constexpr itr begin() const { return itr{m_start, m_step}; }
    constexpr itr end() const { return itr{m_end, m_step}; }
};
constexpr irange rep(i64 end) { return irange(0, end, 1); }
constexpr irange per(i64 rend) { return irange(rend - 1, -1, -1); }
class Scanner
{
public:
    Scanner(Istream& is = std::cin) : m_is{is} { m_is.tie(nullptr)->sync_with_stdio(false); }
    template<typename T>
    T val()
    {
        T v;
        return m_is >> v, v;
    }
    template<typename T>
    T val(T offset)
    {
        return val<T>() - offset;
    }
    template<typename T>
    Vec<T> vec(int n)
    {
        return genVec<T>(n, [&]() { return val<T>(); });
    }
    template<typename T>
    Vec<T> vec(int n, T offset)
    {
        return genVec<T>(n, [&]() { return val<T>(offset); });
    }
    template<typename T>
    Vec<Vec<T>> vvec(int n, int m)
    {
        return genVec<Vec<T>>(n, [&]() { return vec<T>(m); });
    }
    template<typename T>
    Vec<Vec<T>> vvec(int n, int m, const T offset)
    {
        return genVec<Vec<T>>(n, [&]() { return vec<T>(m, offset); });
    }
    template<typename... Args>
    auto tup()
    {
        return Tup<Args...>{val<Args>()...};
    }
    template<typename... Args>
    auto tup(const Args&... offsets)
    {
        return Tup<Args...>{val<Args>(offsets)...};
    }
private:
    Istream& m_is;
};
Scanner in;
class Printer
{
public:
    Printer(Ostream& os = std::cout) : m_os{os} { m_os << std::fixed << std::setprecision(15); }
    template<typename... Args>
    int operator()(const Args&... args)
    {
        return dump(args...), 0;
    }
    template<typename... Args>
    int ln(const Args&... args)
    {
        return dump(args...), m_os << '\n', 0;
    }
    template<typename... Args>
    int el(const Args&... args)
    {
        return dump(args...), m_os << std::endl, 0;
    }
    int YES(bool b = true) { return ln(b ? "YES" : "NO"); }
    int NO(bool b = true) { return YES(not b); }
    int Yes(bool b = true) { return ln(b ? "Yes" : "No"); }
    int No(bool b = true) { return Yes(not b); }
private:
    template<typename T>
    void dump(const T& v)
    {
        m_os << v;
    }
    template<typename T>
    void dump(const Vec<T>& vs)
    {
        for (int i : rep(vs.size())) { m_os << (i ? " " : ""), dump(vs[i]); }
    }
    template<typename T>
    void dump(const Vec<Vec<T>>& vss)
    {
        for (int i : rep(vss.size())) { m_os << (i ? "\n" : ""), dump(vss[i]); }
    }
    template<typename T, typename... Ts>
    int dump(const T& v, const Ts&... args)
    {
        return dump(v), m_os << ' ', dump(args...), 0;
    }
    Ostream& m_os;
};
Printer out;
template<typename T, int n, int i = 0>
auto ndVec(int const (&szs)[n], const T x = T{})
{
    if constexpr (i == n) {
        return x;
    } else {
        return std::vector(szs[i], ndVec<T, n, i + 1>(szs, x));
    }
}
template<typename T, typename F>
T binSearch(T ng, T ok, F check)
{
    while (std::abs(ok - ng) > 1) {
        const T mid = (ok + ng) / 2;
        (check(mid) ? ok : ng) = mid;
    }
    return ok;
}
template<u32 mod_, u32 root_, u32 max2p_>
class modint
{
    template<typename U = u32&>
    static U modRef()
    {
        static u32 s_mod = 0;
        return s_mod;
    }
    template<typename U = u32&>
    static U rootRef()
    {
        static u32 s_root = 0;
        return s_root;
    }
    template<typename U = u32&>
    static U max2pRef()
    {
        static u32 s_max2p = 0;
        return s_max2p;
    }
public:
    static constexpr bool isDynamic() { return (mod_ == 0); }
    template<typename U = const u32>
    static constexpr std::enable_if_t<mod_ != 0, U> mod()
    {
        return mod_;
    }
    template<typename U = const u32>
    static std::enable_if_t<mod_ == 0, U> mod()
    {
        return modRef();
    }
    template<typename U = const u32>
    static constexpr std::enable_if_t<mod_ != 0, U> root()
    {
        return root_;
    }
    template<typename U = const u32>
    static std::enable_if_t<mod_ == 0, U> root()
    {
        return rootRef();
    }
    template<typename U = const u32>
    static constexpr std::enable_if_t<mod_ != 0, U> max2p()
    {
        return max2p_;
    }
    template<typename U = const u32>
    static std::enable_if_t<mod_ == 0, U> max2p()
    {
        return max2pRef();
    }
    template<typename U = u32>
    static void setMod(std::enable_if_t<mod_ == 0, U> m)
    {
        modRef() = m;
    }
    template<typename U = u32>
    static void setRoot(std::enable_if_t<mod_ == 0, U> r)
    {
        rootRef() = r;
    }
    template<typename U = u32>
    static void setMax2p(std::enable_if_t<mod_ == 0, U> m)
    {
        max2pRef() = m;
    }
    constexpr modint() : m_val{0} {}
    constexpr modint(i64 v) : m_val{normll(v)} {}
    constexpr void setRaw(u32 v) { m_val = v; }
    constexpr modint operator-() const { return modint{0} - (*this); }
    constexpr modint& operator+=(const modint& m)
    {
        m_val = norm(m_val + m.val());
        return *this;
    }
    constexpr modint& operator-=(const modint& m)
    {
        m_val = norm(m_val + mod() - m.val());
        return *this;
    }
    constexpr modint& operator*=(const modint& m)
    {
        m_val = normll((i64)m_val * (i64)m.val() % (i64)mod());
        return *this;
    }
    constexpr modint& operator/=(const modint& m) { return *this *= m.inv(); }
    constexpr modint operator+(const modint& m) const
    {
        auto v = *this;
        return v += m;
    }
    constexpr modint operator-(const modint& m) const
    {
        auto v = *this;
        return v -= m;
    }
    constexpr modint operator*(const modint& m) const
    {
        auto v = *this;
        return v *= m;
    }
    constexpr modint operator/(const modint& m) const
    {
        auto v = *this;
        return v /= m;
    }
    constexpr bool operator==(const modint& m) const { return m_val == m.val(); }
    constexpr bool operator!=(const modint& m) const { return not(*this == m); }
    friend Istream& operator>>(Istream& is, modint& m)
    {
        i64 v;
        return is >> v, m = v, is;
    }
    friend Ostream& operator<<(Ostream& os, const modint& m) { return os << m.val(); }
    constexpr u32 val() const { return m_val; }
    template<typename I>
    constexpr modint pow(I n) const
    {
        return powerInt(*this, n);
    }
    constexpr modint inv() const { return pow(mod() - 2); }
    static modint sinv(u32 n)
    {
        static Vec<modint> is{1, 1};
        for (u32 i = (u32)is.size(); i <= n; i++) { is.push_back(-is[mod() % i] * (mod() / i)); }
        return is[n];
    }
    static modint fact(u32 n)
    {
        static Vec<modint> fs{1, 1};
        for (u32 i = (u32)fs.size(); i <= n; i++) { fs.push_back(fs.back() * i); }
        return fs[n];
    }
    static modint ifact(u32 n)
    {
        static Vec<modint> ifs{1, 1};
        for (u32 i = (u32)ifs.size(); i <= n; i++) { ifs.push_back(ifs.back() * sinv(i)); }
        return ifs[n];
    }
    static modint comb(int n, int k)
    {
        return k > n or k < 0 ? modint{0} : fact(n) * ifact(n - k) * ifact(k);
    }
private:
    static constexpr u32 norm(u32 x) { return x < mod() ? x : x - mod(); }
    static constexpr u32 normll(i64 x) { return norm(u32(x % (i64)mod() + (i64)mod())); }
    u32 m_val;
};
using modint_1000000007 = modint<1000000007, 5, 1>;
using modint_998244353 = modint<998244353, 3, 23>;
template<int id>
using modint_dynamic = modint<0, 0, id>;
template<typename T = int>
class Graph
{
    struct Edge
    {
        Edge() = default;
        Edge(int i, int t, T c) : id{i}, to{t}, cost{c} {}
        int id;
        int to;
        T cost;
        operator int() const { return to; }
    };
public:
    Graph(int n) : m_v{n}, m_edges(n) {}
    void addEdge(int u, int v, bool bi = false)
    {
        assert(0 <= u and u < m_v);
        assert(0 <= v and v < m_v);
        m_edges[u].emplace_back(m_e, v, 1);
        if (bi) { m_edges[v].emplace_back(m_e, u, 1); }
        m_e++;
    }
    void addEdge(int u, int v, const T& c, bool bi = false)
    {
        assert(0 <= u and u < m_v);
        assert(0 <= v and v < m_v);
        m_edges[u].emplace_back(m_e, v, c);
        if (bi) { m_edges[v].emplace_back(m_e, u, c); }
        m_e++;
    }
    const Vec<Edge>& operator[](const int u) const
    {
        assert(0 <= u and u < m_v);
        return m_edges[u];
    }
    Vec<Edge>& operator[](const int u)
    {
        assert(0 <= u and u < m_v);
        return m_edges[u];
    }
    int v() const { return m_v; }
    int e() const { return m_e; }
    friend Ostream& operator<<(Ostream& os, const Graph& g)
    {
        for (int u : rep(g.v())) {
            for (const auto& [id, v, c] : g[u]) {
                os << "[" << id << "]: ";
                os << u << "->" << v << "(" << c << ")\n";
            }
        }
        return os;
    }
    Vec<T> sizes(int root = 0) const
    {
        const int N = v();
        assert(0 <= root and root < N);
        Vec<T> ss(N, 1);
        Fix([&](auto dfs, int u, int p) -> void {
            for ([[maybe_unused]] const auto& [_temp_name_0, v, c] : m_edges[u]) {
                if (v == p) { continue; }
                dfs(v, u);
                ss[u] += ss[v];
            }
        })(root, -1);
        return ss;
    }
    Vec<T> depths(int root = 0) const
    {
        const int N = v();
        assert(0 <= root and root < N);
        Vec<T> ds(N, 0);
        Fix([&](auto dfs, int u, int p) -> void {
            for ([[maybe_unused]] const auto& [_temp_name_1, v, c] : m_edges[u]) {
                if (v == p) { continue; }
                ds[v] = ds[u] + c;
                dfs(v, u);
            }
        })(root, -1);
        return ds;
    }
    Vec<int> parents(int root = 0) const
    {
        const int N = v();
        assert(0 <= root and root < N);
        Vec<int> ps(N, -1);
        Fix([&](auto dfs, int u, int p) -> void {
            for ([[maybe_unused]] const auto& [_temp_name_2, v, c] : m_edges[u]) {
                if (v == p) { continue; }
                ps[v] = u;
                dfs(v, u);
            }
        })(root, -1);
        return ps;
    }
private:
    int m_v;
    int m_e = 0;
    Vec<Vec<Edge>> m_edges;
};
template<typename MergeMonoid>
class SegTree
{
    using T = typename MergeMonoid::T;
    static constexpr T e() { return MergeMonoid::e(); }
public:
    SegTree(const Vec<T>& vs)
        : m_size(vs.size()), m_half(ceil2(m_size)), m_vals(m_half << 1, MergeMonoid::e())
    {
        std::copy(vs.begin(), vs.end(), m_vals.begin() + m_half);
        for (int i = m_half - 1; i >= 1; i--) { up(i); }
    }
    SegTree(int N, const T& v = MergeMonoid::e()) : SegTree{Vec<T>(N, v)} {}
    T get(int i) const
    {
        assert(0 <= i and i < m_size);
        return m_vals[i + m_half];
    }
    void set(int i, const T& v)
    {
        assert(0 <= i and i < m_size);
        m_vals[i += m_half] = v;
        while (i >>= 1) { up(i); }
    }
    T fold(int l, int r) const
    {
        assert(0 <= l and l <= r and r <= m_size);
        T lv = e(), rv = e();
        int li = l + m_half, ri = r + m_half;
        for (; li < ri; li >>= 1, ri >>= 1) {
            if (li & 1) { lv = merge(lv, m_vals[li++]); }
            if (ri & 1) { rv = merge(m_vals[--ri], rv); }
        }
        return merge(lv, rv);
    }
    friend Ostream& operator<<(Ostream& os, const SegTree& seg)
    {
        os << "[";
        for (int i : rep(seg.m_size)) { os << (i == 0 ? "" : ",") << seg.m_vals[i + seg.m_half]; }
        return (os << "]");
    }
private:
    void up(int i) { m_vals[i] = merge(m_vals[i << 1], m_vals[i << 1 | 1]); }
    int m_size, m_half;
    Vec<T> m_vals;
    static inline MergeMonoid merge;
};
struct Sum
{
    using T = i64;
    static constexpr T e()
    {
        return 0;
    }
    T operator()(const T& x1, const T& x2) const
    {
        return x1 + x2;
    }
};
int main()
{
    const auto N = in.val<int>();
    Vec<i64> As(N);
    Vec<i64> Ts(N);
    for (int i : rep(N)) {
        const auto [A, T] = in.tup<i64, i64>();
        As[i] = A, Ts[i] = T;
        if (Ts[i] == 0) { As[i]++; }
    }
    const auto Q = in.val<int>();
    Vec<i64> Ds(Q);
    Vec<int> Ls(Q), Rs(Q);
    for (int q : rep(Q)) {
        const auto [D, L, R] = in.tup<i64, int, int>(0, 1, 0);
        Ds[q] = D, Ls[q] = L, Rs[q] = R;
    }
    Map<i64, Vec<Pair<int, int>>> pss;
    for (int i : rep(N)) {
        pss[Ts[i]].push_back({0, i + 1});
        pss[As[i] + Ts[i]].push_back({0, -(i + 1)});
    }
    for (int q : rep(Q)) {
        pss[Ds[q]].push_back({1, q});
    }
    SegTree<Sum> seg1(As);
    SegTree<Sum> seg2(N);
    SegTree<Sum> seg3(N);
    Vec<i64> ans(Q);
    for (const auto& [t, ps] : pss) {
        for (const auto& [b, p] : ps) {
            if (b == 0) {
                if (p > 0) {
                    const int i = p - 1;
                    seg2.set(i, Ts[i]);
                    seg3.set(i, 1);
                } else {
                    const int i = -p - 1;
                    seg1.set(i, 0);
                    seg2.set(i, 0);
                    seg3.set(i, 0);
                }
            } else {
                const int q = p;
                const i64 d = Ds[q];
                const int l = Ls[q], r = Rs[q];
                const i64 minus = seg3.fold(l, r) * (d + 1) - seg2.fold(l, r);
                ans[q] = seg1.fold(l, r) - minus;
            }
        }
    }
    for (i64 an : ans) {
        out.ln(an);
    }
    return 0;
}
0