結果

問題 No.789 範囲の合計
ユーザー KPCCoiLKPCCoiL
提出日時 2019-02-17 13:19:31
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 480 ms / 1,000 ms
コード長 2,342 bytes
コンパイル時間 1,193 ms
コンパイル使用メモリ 95,196 KB
実行使用メモリ 117,260 KB
最終ジャッジ日時 2023-10-25 22:16:37
合計ジャッジ時間 5,344 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 410 ms
98,360 KB
testcase_03 AC 65 ms
4,348 KB
testcase_04 AC 385 ms
91,052 KB
testcase_05 AC 338 ms
94,360 KB
testcase_06 AC 349 ms
95,212 KB
testcase_07 AC 59 ms
4,348 KB
testcase_08 AC 167 ms
35,040 KB
testcase_09 AC 152 ms
32,156 KB
testcase_10 AC 480 ms
117,260 KB
testcase_11 AC 360 ms
91,812 KB
testcase_12 AC 358 ms
91,764 KB
testcase_13 AC 2 ms
4,348 KB
testcase_14 AC 2 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <memory>
#define NDEBUG
#include <cassert>

template <class Monoid>
class segtree_node {
  public:
    using value_type = typename Monoid::value_type;
  private:
    using node_t = std::unique_ptr<segtree_node<Monoid>>;
    node_t lch, rch;
    int n;
    value_type value;

    void require_lch() {
      if (!lch) {
        lch = std::make_unique<segtree_node>(n / 2);
      }
    }
    void require_rch() {
      if (!rch) {
        rch = std::make_unique<segtree_node>(n - n / 2);
      }
    }

  public:
    segtree_node(int n):
      n(n),
      value(Monoid::zero)
    {}

    value_type get(int i) {
      assert(0 <= i && i < n);
      if (n == 1) {
        return value;
      }
      else if (i < n / 2) {
        if (!lch) return Monoid::zero;
        return lch->get(i);
      }
      else {
        if (!rch) return Monoid::zero;
        return rch->get(i - n / 2);
      }
    }

    void update(int i, value_type x) {
      assert(0 <= i && i < n);
      if (n == 1) {
        value = x;
      }
      else {
        if (i < n / 2) {
          require_lch();
          lch->update(i, x);
        }
        else {
          require_rch();
          rch->update(i - n / 2, x);
        }
        auto lv = lch ? lch->value : Monoid::zero,
             rv = rch ? rch->value : Monoid::zero;
        value = Monoid::product(lv, rv);
      }
    }

    value_type product(int from, int to) {
      if (to <= 0 || n <= from)
        return Monoid::zero;
      if (from <= 0 && n <= to)
        return value;
      require_lch();
      require_rch();
      auto lp = lch->product(from, to),
           rp = rch->product(from - n / 2, to - n / 2);
      return Monoid::product(lp, rp);
    }
};

struct int_sum {
  using value_type = int;
  static constexpr value_type zero = 0;
  static value_type product(value_type x, value_type y) {
    return x + y;
  }
};

int main() {
  std::ios_base::sync_with_stdio(false);
  std::cin.tie(nullptr);
  int n;
  std::cin >> n;
  int t, x, y;
  segtree_node<int_sum> segtree(1000000001);
  long long sum = 0;
  for (int i = 0; i < n; i++) {
    std::cin >> t >> x >> y;
    if (t == 0) {
      auto current = segtree.get(x);
      segtree.update(x, current + y);
    }
    else {
      sum += segtree.product(x, y + 1);
    }
  }
  std::cout << sum << std::endl;
}
0