結果

問題 No.789 範囲の合計
ユーザー kuhakukuhaku
提出日時 2023-07-17 01:42:26
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 172 ms / 1,000 ms
コード長 9,600 bytes
コンパイル時間 3,593 ms
コンパイル使用メモリ 232,832 KB
実行使用メモリ 50,588 KB
最終ジャッジ日時 2023-10-17 23:41:19
合計ジャッジ時間 6,001 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 156 ms
41,520 KB
testcase_03 AC 41 ms
4,348 KB
testcase_04 AC 172 ms
46,296 KB
testcase_05 AC 123 ms
39,800 KB
testcase_06 AC 131 ms
41,572 KB
testcase_07 AC 36 ms
4,348 KB
testcase_08 AC 140 ms
50,588 KB
testcase_09 AC 127 ms
46,296 KB
testcase_10 AC 135 ms
28,688 KB
testcase_11 AC 98 ms
40,868 KB
testcase_12 AC 100 ms
40,860 KB
testcase_13 AC 2 ms
4,348 KB
testcase_14 AC 2 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#line 1 "a.cpp"
#define PROBLEM ""
#line 2 "/home/kuhaku/home/github/algo/lib/template/template.hpp"
#pragma GCC target("sse4.2,avx2,bmi2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
template <class T, class U>
bool chmax(T &a, const U &b) {
    return a < (T)b ? a = (T)b, true : false;
}
template <class T, class U>
bool chmin(T &a, const U &b) {
    return (T)b < a ? a = (T)b, true : false;
}
constexpr int64_t INF = 1000000000000000003;
constexpr int Inf = 1000000003;
constexpr int MOD = 1000000007;
constexpr int MOD_N = 998244353;
constexpr double EPS = 1e-7;
constexpr double PI = M_PI;
#line 3 "/home/kuhaku/home/github/algo/lib/math/pow.hpp"

std::int64_t pow_int(std::int64_t a, std::int64_t n) {
    assert(n >= 0);
    std::int64_t res = 1, mul = a;
    for (; n > 0; n >>= 1) {
        if (n & 1)
            res *= mul;
        mul *= mul;
    }
    return res;
}

std::int64_t inv_mod(std::int64_t a, std::int64_t mod) {
    std::int64_t b = mod, u = 1, v = 0, t;
    while (b > 0) {
        t = a / b;
        swap(a -= t * b, b);
        swap(u -= t * v, v);
    }
    return u >= 0 ? u % mod : (mod - (-u) % mod) % mod;
}

std::int64_t pow_mod(std::int64_t a, std::int64_t n, std::int64_t mod) {
    if (n < 0)
        return inv_mod(pow_mod(a, -n, mod), mod);
    std::int64_t res = 1, mul = a;
    for (; n > 0; n >>= 1) {
        if (n & 1)
            (res *= mul) %= mod;
        (mul *= mul) %= mod;
    }
    return res;
}

int ceil_pow2(std::int64_t n) {
    int x = 0;
    while ((std::uint64_t(1) << x) < (std::uint64_t)(n)) ++x;
    return x;
}
#line 2 "/home/kuhaku/home/github/algo/lib/segment_tree/monoid.hpp"

template <class T>
struct Add {
    using value_type = T;
    static constexpr T id = T(0);
    static constexpr T op(const T &lhs, const T &rhs) {
        return lhs + rhs;
    }

    template <class U>
    static constexpr U f(T lhs, U rhs) {
        return lhs + rhs;
    }
};

template <class T>
struct And {
    using value_type = T;
    static constexpr T id = T(0);
    static constexpr T op(const T &lhs, const T &rhs) {
        return lhs & rhs;
    }

    template <class U>
    static constexpr U f(T lhs, U rhs) {
        return lhs & rhs;
    }
};

template <class T>
struct Or {
    using value_type = T;
    static constexpr T id = T(0);
    static constexpr T op(const T &lhs, const T &rhs) {
        return lhs | rhs;
    }

    template <class U>
    static constexpr U f(T lhs, U rhs) {
        return lhs | rhs;
    }
};

template <class T>
struct Xor {
    using value_type = T;
    static constexpr T id = T(0);
    static constexpr T op(const T &lhs, const T &rhs) {
        return lhs ^ rhs;
    }

    template <class U>
    static constexpr U f(T lhs, U rhs) {
        return lhs ^ rhs;
    }
};

template <class T>
struct Min {
    using value_type = T;
    static constexpr T id = numeric_limits<T>::max();
    static constexpr T op(const T &lhs, const T &rhs) {
        return min(lhs, rhs);
    }

    template <class U>
    static constexpr U f(T lhs, U rhs) {
        return min((U)lhs, rhs);
    }
};

template <class T>
struct Max {
    using value_type = T;
    static constexpr T id = numeric_limits<T>::min();
    static constexpr T op(const T &lhs, const T &rhs) {
        return max(lhs, rhs);
    }

    template <class U>
    static constexpr U f(T lhs, U rhs) {
        return max((U)lhs, rhs);
    }
};

template <class T>
struct Update {
    using value_type = T;
    static constexpr T id = numeric_limits<T>::max();
    static constexpr T op(const T &lhs, const T &rhs) {
        return lhs == Update::id ? rhs : lhs;
    }

    template <class U>
    static constexpr U f(T lhs, U rhs) {
        return lhs == Update::id ? rhs : lhs;
    }
};
#line 4 "/home/kuhaku/home/github/algo/lib/segment_tree/dynamic_segment_tree.hpp"

template <class M>
struct dynamic_segment_tree {
  private:
    using T = typename M::value_type;

    struct _node {
        using pointer = _node *;
        pointer parent, left, right;
        T value;

        _node(pointer ptr) : parent(ptr), left(nullptr), right(nullptr), value(M::id) {}
    };

  public:
    using node_ptr = typename _node::pointer;

    dynamic_segment_tree(std::int64_t n) : root(), _size(), _log() {
        this->init(n);
    }

    T operator[](int k) const {
        node_ptr node = this->root;
        for (int i = this->_log - 1; i >= 0; --i) {
            if (k >> i & 1) {
                if (!node->right)
                    return M::id;
                node = node->right;
            } else {
                if (!node->left)
                    return M::id;
                node = node->left;
            }
        }
        return node->value;
    }
    T at(int k) const {
        return this->operator[](k);
    }
    T get(int k) const {
        return this->operator[](k);
    }

    void init(std::int64_t n) {
        this->_log = ceil_pow2(n);
        this->_size = 1LL << this->_log;
        this->root = new _node(nullptr);
    }

    void set(std::int64_t k, T x) {
        assert(0 <= k && k < this->_size);
        node_ptr node = this->root;
        for (int i = this->_log - 1; i >= 0; --i) {
            if (k >> i & 1) {
                if (!node->right)
                    node->right = new _node(node);
                node = node->right;
            } else {
                if (!node->left)
                    node->left = new _node(node);
                node = node->left;
            }
        }

        node->value = x;
        while (node->parent) {
            node = node->parent;
            node->value = M::op(node->left ? node->left->value : M::id,
                                node->right ? node->right->value : M::id);
        }
    }
    void reset(std::int64_t k) {
        this->set(k, M::id);
    }

    T all_prod() const {
        return this->root ? this->root->value : M::id;
    }
    T prod(int64_t a, int64_t b) const {
        assert(0 <= a && a <= this->_size);
        assert(0 <= b && b <= this->_size);
        return this->prod(a, b, this->root, 0, this->_size);
    }

  private:
    node_ptr root;
    std::int64_t _size, _log;

    T prod(int64_t a, int64_t b, node_ptr node, int64_t l, int64_t r) const {
        if (a <= l && r <= b)
            return node->value;
        if (r <= a || b <= l)
            return M::id;

        return M::op(node->left ? this->prod(a, b, node->left, l, (l + r) >> 1) : M::id,
                     node->right ? this->prod(a, b, node->right, (l + r) >> 1, r) : M::id);
    }
};
#line 3 "/home/kuhaku/home/github/algo/lib/template/macro.hpp"
#define FOR(i, m, n) for (int i = (m); i < int(n); ++i)
#define FORR(i, m, n) for (int i = (m)-1; i >= int(n); --i)
#define FORL(i, m, n) for (int64_t i = (m); i < int64_t(n); ++i)
#define rep(i, n) FOR (i, 0, n)
#define repn(i, n) FOR (i, 1, n + 1)
#define repr(i, n) FORR (i, n, 0)
#define repnr(i, n) FORR (i, n + 1, 1)
#define all(s) (s).begin(), (s).end()
#line 3 "/home/kuhaku/home/github/algo/lib/template/sonic.hpp"
struct Sonic {
    Sonic() {
        std::ios::sync_with_stdio(false);
        std::cin.tie(nullptr);
    }

    constexpr void operator()() const {}
} sonic;
#line 5 "/home/kuhaku/home/github/algo/lib/template/atcoder.hpp"
using ll = int64_t;
using ld = long double;
template <class T, class U>
std::istream &operator>>(std::istream &is, std::pair<T, U> &p) {
    return is >> p.first >> p.second;
}
template <class T>
std::istream &operator>>(std::istream &is, std::vector<T> &v) {
    for (T &i : v) is >> i;
    return is;
}
template <class T, class U>
std::ostream &operator<<(std::ostream &os, const std::pair<T, U> &p) {
    return os << '(' << p.first << ',' << p.second << ')';
}
template <class T>
std::ostream &operator<<(std::ostream &os, const std::vector<T> &v) {
    for (auto it = v.begin(); it != v.end(); ++it) {
        os << (it == v.begin() ? "" : " ") << *it;
    }
    return os;
}
template <class Head, class... Tail>
void co(Head &&head, Tail &&...tail) {
    if constexpr (sizeof...(tail) == 0)
        std::cout << head << '\n';
    else
        std::cout << head << ' ', co(std::forward<Tail>(tail)...);
}
template <class Head, class... Tail>
void ce(Head &&head, Tail &&...tail) {
    if constexpr (sizeof...(tail) == 0)
        std::cerr << head << '\n';
    else
        std::cerr << head << ' ', ce(std::forward<Tail>(tail)...);
}
template <typename T, typename... Args>
auto make_vector(T x, int arg, Args... args) {
    if constexpr (sizeof...(args) == 0)
        return std::vector<T>(arg, x);
    else
        return std::vector(arg, make_vector<T>(x, args...));
}
void setp(int n) {
    std::cout << std::fixed << std::setprecision(n);
}
void Yes(bool is_correct = true) {
    std::cout << (is_correct ? "Yes" : "No") << '\n';
}
void No(bool is_not_correct = true) {
    Yes(!is_not_correct);
}
void YES(bool is_correct = true) {
    std::cout << (is_correct ? "YES" : "NO") << '\n';
}
void NO(bool is_not_correct = true) {
    YES(!is_not_correct);
}
void Takahashi(bool is_correct = true) {
    std::cout << (is_correct ? "Takahashi" : "Aoki") << '\n';
}
void Aoki(bool is_not_correct = true) {
    Takahashi(!is_not_correct);
}
#line 4 "a.cpp"

int main(void) {
    int n;
    cin >> n;
    dynamic_segment_tree<Add<int>> st(Inf);
    ll ans = 0;
    rep (i, n) {
        int a, x, y;
        cin >> a >> x >> y;
        if (a == 0) {
            st.set(x, st[x] + y);
        } else {
            ans += st.prod(x, y + 1);
        }
    }
    co(ans);

    return 0;
}
0