結果

問題 No.789 範囲の合計
ユーザー tnakao0123tnakao0123
提出日時 2019-02-10 17:41:29
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 77 ms / 1,000 ms
コード長 2,282 bytes
コンパイル時間 767 ms
コンパイル使用メモリ 87,692 KB
実行使用メモリ 5,964 KB
最終ジャッジ日時 2023-09-20 19:20:42
合計ジャッジ時間 2,714 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 74 ms
5,668 KB
testcase_03 AC 42 ms
4,820 KB
testcase_04 AC 68 ms
5,884 KB
testcase_05 AC 53 ms
5,692 KB
testcase_06 AC 56 ms
5,672 KB
testcase_07 AC 33 ms
5,016 KB
testcase_08 AC 52 ms
5,964 KB
testcase_09 AC 47 ms
5,952 KB
testcase_10 AC 77 ms
5,356 KB
testcase_11 AC 48 ms
5,796 KB
testcase_12 AC 44 ms
5,932 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

/* -*- coding: utf-8 -*-
 *
 * 789.cc:  No.789 範囲の合計 - yukicoder
 */

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<queue>
#include<deque>
#include<algorithm>
#include<numeric>
#include<utility>
#include<complex>
#include<functional>
 
using namespace std;

/* constant */

const int MAX_N = 100000;
const int MAX_E2 = 1 << 21; // 2097152

/* typedef */

typedef long long ll;

struct OP {
  int q, x, y;
  OP() {}
  OP(int _q, int _x, int _y): q(_q), x(_x), y(_y) {}
};

template <typename T, const int MAX_E2>
struct SegTreeSum {
  int n, e2;
  T nodes[MAX_E2];
  SegTreeSum() {}

  void init(int _n) {
    n = _n;
    for (e2 = 1; e2 < n; e2 <<= 1);
  }

  T &get(int i) { return nodes[e2 - 1 + i]; }

  void set(int i, T v) {
    int j = e2 - 1 + i;
    nodes[j] = v;
    while (j > 0) {
      j = (j - 1) / 2;
      nodes[j] = nodes[j * 2 + 1] + nodes[j * 2 + 2];
    }
  }

  T sum_range(int r0, int r1, int k, int i0, int i1) {
    if (r1 <= i0 || i1 <= r0) return 0;
    if (r0 <= i0 && i1 <= r1) return nodes[k];

    int im = (i0 + i1) / 2;
    T v0 = sum_range(r0, r1, k * 2 + 1, i0, im);
    T v1 = sum_range(r0, r1, k * 2 + 2, im, i1);

    return v0 + v1;
  }
  T sum_range(int r0, int r1) { return sum_range(r0, r1, 0, 0, e2); }
};

/* global variables */

OP ops[MAX_N];
SegTreeSum<ll,MAX_E2> st;
int xs[MAX_N];

/* subroutines */

/* main */

int main() {
  int n;
  scanf("%d", &n);

  int m = 0;
  for (int i = 0; i < n; i++) {
    int q, x, y;
    scanf("%d%d%d", &q, &x, &y);
    if (q == 0) {
      ops[i] = OP(0, x, y);
      xs[m++] = x;
    }
    else
      ops[i] = OP(1, x, y + 1);
  }
  sort(xs, xs + m);
  m = unique(xs, xs + m) - xs;
  //for (int i = 0; i < m; i++) printf("%d ", xs[i]); putchar('\n');

  st.init(m);

  ll sum = 0;
  for (int i = 0; i < n; i++) {
    OP &opi = ops[i];
    if (opi.q == 0) {
      int k = lower_bound(xs, xs + m, opi.x) - xs;
      st.set(k, st.get(k) + opi.y);
    }
    else {
      int l = lower_bound(xs, xs + m, opi.x) - xs;
      int r = lower_bound(xs, xs + m, opi.y) - xs;
      sum += st.sum_range(l, r);
    }
  }

  printf("%lld\n", sum);
  return 0;
}
0