結果

問題 No.789 範囲の合計
ユーザー YDK1727YDK1727
提出日時 2019-03-03 12:02:43
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 78 ms / 1,000 ms
コード長 1,904 bytes
コンパイル時間 1,656 ms
コンパイル使用メモリ 178,188 KB
実行使用メモリ 7,312 KB
最終ジャッジ日時 2023-09-05 17:41:10
合計ジャッジ時間 3,397 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 77 ms
7,240 KB
testcase_03 AC 50 ms
6,392 KB
testcase_04 AC 75 ms
7,312 KB
testcase_05 AC 66 ms
7,080 KB
testcase_06 AC 68 ms
7,240 KB
testcase_07 AC 46 ms
6,188 KB
testcase_08 AC 62 ms
6,584 KB
testcase_09 AC 57 ms
6,544 KB
testcase_10 AC 78 ms
7,124 KB
testcase_11 AC 70 ms
7,080 KB
testcase_12 AC 69 ms
7,068 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include "bits/stdc++.h"
#define LF '\n'
#define ALL(x) x.begin(), x.end()
#define LEN(x) (int)x.size()
#define iostreamBooster() do{ cin.tie(nullptr); ios_base::sync_with_stdio(false); }while(0)
using namespace std;
typedef int64_t i64;
typedef pair<int,int> pii;
template<class A, class B>inline bool chmax(A &a, const B &b){return b>a ? a=b,1 : 0;}
template<class A, class B>inline bool chmin(A &a, const B &b){return b<a ? a=b,1 : 0;}
constexpr int INF = 0x3f3f3f3f;
template<class Itr>void dump(Itr begin, Itr end) {
  for(;begin != end; ++begin) clog << ' ' << *begin;
  clog << LF;
}

struct FenwickTree {
  const int N;
  vector<i64> dat;
  explicit FenwickTree(int n): N(n), dat(N+1, 0) {}
  i64  sum(int l, int r) { return sum(r) - sum(l-1); }
  i64  sum(int i)        { return (i>0)? (sum(i - (i&-i)) + dat[i]) : 0; }
  void add(int i, i64 v) { for(; i <= N; i += i&-i) dat[i] += v; }
};

using P = tuple<int, int, int>;

int N;
P query[100100];
vector<int> points;

inline int index(int orgIndex) { return lower_bound(ALL(points), orgIndex) - points.begin(); }


signed main()
{
  iostreamBooster();
  cin >> N;
  for (int i = 0; i < N; ++i) {
    int a, b, c; cin >> a >> b >> c;
    query[i] = make_tuple(a, b, c);
    if (a) {
      points.push_back(b);
      points.push_back(b-1);
    } else {
      points.push_back(b);
      points.push_back(c);
      points.push_back(b-1);
      points.push_back(c-1);
    }
  }

  points.push_back(0);
  points.push_back(-1);
  points.push_back(1'000'000'001);
  sort(ALL(points));
  points.erase(unique(ALL(points)), points.end());

  FenwickTree ft(points.size() + 5);

  i64 ans = 0;

  for (int i = 0; i < N; ++i) {
    int com, a, b;
    tie(com, a, b) = query[i];

    if (com == 0) {
      ft.add(index(a), b);
    } else {
      const i64 tmp = ft.sum(index(a), index(b));
      ans += tmp;
    }
  }

  cout << ans << endl;

  return 0;
}

0