結果

問題 No.789 範囲の合計
ユーザー HoyHoyCharhangHoyHoyCharhang
提出日時 2023-02-06 08:39:58
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 137 ms / 1,000 ms
コード長 2,508 bytes
コンパイル時間 2,198 ms
コンパイル使用メモリ 172,724 KB
実行使用メモリ 6,556 KB
最終ジャッジ日時 2023-09-17 20:40:40
合計ジャッジ時間 4,711 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 131 ms
5,836 KB
testcase_03 AC 81 ms
4,380 KB
testcase_04 AC 137 ms
6,152 KB
testcase_05 AC 115 ms
5,668 KB
testcase_06 AC 119 ms
5,884 KB
testcase_07 AC 68 ms
4,376 KB
testcase_08 AC 107 ms
6,556 KB
testcase_09 AC 102 ms
6,152 KB
testcase_10 AC 129 ms
4,904 KB
testcase_11 AC 97 ms
6,372 KB
testcase_12 AC 99 ms
6,120 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 2 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define fi first
#define se second
#define rep(i,s,n) for (int i = (s); i < (n); ++i)
#define rrep(i,n,g) for (int i = (n)-1; i >= (g); --i)
#define all(a) a.begin(),a.end()
#define rall(a) a.rbegin(),a.rend()
#define len(x) (int)(x).size()
#define dup(x,y) (((x)+(y)-1)/(y))
#define pb push_back
#define eb emplace_back
#define Field(T) vector<vector<T>>
#define pq(T) priority_queue<T, vector<T>, greater<T>>
using namespace std;
using ll = long long;
using P = pair<int,int>;

template <typename S>
struct DynamicSegTree {
  struct Node {
    Node *l, *r;
    ll idx;
    S val, sum;
    Node(S val_, ll p) : l(nullptr), r(nullptr), idx(p), val(val_), sum(val_) {}
    void update() {
      sum = val;
      if (l) sum = l->sum + sum;
      if (r) sum = sum + r->sum;
    }
  };

  ll n;
  Node* root;
  const S e = S();

  DynamicSegTree(ll _n) : n(_n), root(nullptr) {}

  void set(ll p, S x) {
    _set(root, 0, n, p, x);
  }
  S get(ll p) {
    return _get(root, 0, n, p);
  }
  S prod(ll l, ll r) {
    return _prod(root, 0, n, l, r);
  }
  S all_prod() {
    if (!root) return e;
    else return root->sum;
  }

 private:

  void _set(Node* &t, ll a, ll b, ll p, S x) {
    if (!t) {
      t = new Node(x, p);
      return;
    }
    if (p == t->idx) {
      t->val = x;
      t->update();
      return;
    }
    ll c = a + (b - a)/2;
    if (p < c) {
      if (t->idx < p) {
        swap(t->idx, p);
        swap(t->val, x);
      }
      _set(t->l, a, c, p, x);
    } else {
      if (t->idx > p) {
        swap(t->idx, p);
        swap(t->val, x);
      }
      _set(t->r, c, b, p, x);
    }
    t->update();
  }

  S _get(Node *&t, ll a, ll b, ll p) {
    if (!t) return e;
    if (p == t->idx) {
      return t->val;
    }
    ll c = a + (b - a)/2;
    if (p < c) {
      return _get(t->l, a, c, p);
    } else {
      return _get(t->r, c, b, p);
    }
  }

 S _prod(Node *&t, ll a, ll b, ll l, ll r) {
    if (!t || b <= l || r <= a) return e;
    if (l <= a && b <= r) return t->sum;
    ll c = a + (b - a)/2;
    return _prod(t->l, a, c, l, r) + ((l <= t->idx && t->idx < r) ? t->val : e) + _prod(t->r, c, b, l, r);
  }
};

int main() {
  DynamicSegTree<ll> seg(1000000001);
  ll n;
  cin >> n;
  ll ans = 0;
  while(n--) {
    int t;
    cin >> t;
    if (t == 0) {
      int x, y;
      cin >> x >> y;
      seg.set(x, seg.get(x) + y);
    } else {
      int l, r;
      cin >> l >> r;
      ans += seg.prod(l, r+1);
    }
  }
  cout << ans << endl;
  return 0;
}
0