結果

問題 No.789 範囲の合計
コンテスト
ユーザー Nzt3
提出日時 2025-12-19 17:58:16
言語 C++23
(gcc 13.3.0 + boost 1.89.0)
結果
RE  
実行時間 -
コード長 3,261 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 2,907 ms
コンパイル使用メモリ 279,564 KB
実行使用メモリ 32,000 KB
最終ジャッジ日時 2025-12-19 17:58:23
合計ジャッジ時間 6,308 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 6 RE * 9
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

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

using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vi = vector<int>;
using vl = vector<ll>;
#define rep3(i, a, b, c) for (ll i = (a); i < (b); i += (c))
#define rep2(i, a, b) rep3(i, a, b, 1)
#define rep1(i, n) rep2(i, 0, n)
#define rep0(n) rep1(aaaaa, n)
#define ov4(a, b, c, d, name, ...) name
#define rep(...) ov4(__VA_ARGS__, rep3, rep2, rep1, rep0)(__VA_ARGS__)
#define per(i, a, b) for (ll i = (a) - 1; i >= (b); i--)
#define fore(e, v) for (auto &&e : v)
#define all(a) begin(a), end(a)
#define sz(a) (int)(size(a))
#define lb(v, x) (lower_bound(all(v), x) - begin(v))
#define eb emplace_back

template <typename T, typename S> bool chmin(T &a, const S &b) {
  return a > b ? a = b, 1 : 0;
}
template <typename T, typename S> bool chmax(T &a, const S &b) {
  return a < b ? a = b, 1 : 0;
}
const int INF = 1e9 + 100;
const ll INFL = 3e18 + 100;
#define i128 __int128_t
struct _ {
  _() { cin.tie(0)->sync_with_stdio(0), cout.tie(0); }
} __;

namespace Lib {
template <class S, auto op, auto e> struct dynamic_segtree {
  static_assert(std::is_convertible_v<decltype(op), std::function<S(S, S)>>,
                "op must work as S(S, S)");
  static_assert(std::is_convertible_v<decltype(e), std::function<S()>>,
                "e must work as S()");
  static const ll K = 62;
  static const ll segsize = 1ll << K;
  dynamic_segtree() : root(), N(0) {};
  S get(ll p) { return _get(p); }
  S prod(ll l, ll r) { return _prod(root, l, r); }
  void set(ll p, S val) { return _set(root, p, val); }
  size_t size() { return N; }

private:
  struct node;
  using node_ptr = node *;
  struct node {
    S val;
    node_ptr left, right;
    node() : val(e()), left(nullptr), right(nullptr) {}
  };
  node_ptr root;
  ll N;

  S _prod(node_ptr &nd, ll l, ll r, ll btm = 0, ll top = segsize) {
    if (r <= btm || top <= l)
      return e();
    if (l <= btm && top <= r)
      return nd->val;
    ll mid = (btm + top) / 2;
    return op(nd->left ? _prod(nd->left, l, r, btm, mid) : e(),
              nd->right ? _prod(nd->right, l, r, mid, top) : e());
  }

  S _get(ll p) {
    node_ptr nd = root;
    per(i, K, 0) {
      if (!nd)
        return e();
      if (p >> i & 1)
        nd = nd->right;
      else
        nd = nd->left;
    }
    if (!nd)
      return e();
    return nd->val;
  }

  void _set(node_ptr &nd, ll p, S val, ll btm = 0, ll top = segsize) {
    if (p + 1 <= btm || top <= p)
      return;
    if (!nd) {
      nd = new node();
      if (btm + 1 == top)
        N++;
    }
    if (p <= btm && top <= p + 1) {
      nd->val = val;
      return;
    }
    ll mid = (btm + top) / 2;
    _set(nd->left, p, val, btm, mid);
    _set(nd->right, p, val, mid, top);

    nd->val = op((nd->left ? nd->left->val : e()),
                 (nd->right ? nd->right->val : e()));
  }
};
} // namespace Lib

int main() {
  auto const op = [&](ll a, ll b) -> ll { return a + b; };
  auto const e = [&]() { return 0ll; };
  Lib::dynamic_segtree<ll, op, e> seg;
  int N;
  cin >> N;
  ll ans = 0;
  rep(N) {
    int Q, A, B;
    cin >> Q >> A >> B;
    if (Q == 0) {
      seg.set(A, seg.get(A) + B);
    } else {
      ans += seg.prod(A, B + 1);
    }
  }
  cout << ans << '\n';
}
0