結果

問題 No.789 範囲の合計
ユーザー le_panda_noirle_panda_noir
提出日時 2020-04-12 15:56:30
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 127 ms / 1,000 ms
コード長 3,369 bytes
コンパイル時間 1,264 ms
コンパイル使用メモリ 125,636 KB
実行使用メモリ 6,208 KB
最終ジャッジ日時 2023-10-22 01:19:21
合計ジャッジ時間 4,420 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,356 KB
testcase_01 AC 2 ms
4,356 KB
testcase_02 AC 117 ms
5,948 KB
testcase_03 AC 73 ms
5,948 KB
testcase_04 AC 113 ms
5,948 KB
testcase_05 AC 106 ms
5,948 KB
testcase_06 AC 108 ms
5,948 KB
testcase_07 AC 68 ms
6,208 KB
testcase_08 AC 96 ms
6,208 KB
testcase_09 AC 93 ms
5,948 KB
testcase_10 AC 127 ms
5,948 KB
testcase_11 AC 97 ms
5,948 KB
testcase_12 AC 92 ms
5,948 KB
testcase_13 AC 2 ms
4,356 KB
testcase_14 AC 2 ms
4,356 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <cmath>
#include <iomanip>

using namespace std;
using ll = long long;
using pii = pair<int, int>;
using vi = vector<int>;
template<class T>using vv = vector<vector<T>>;

#define in(v) v; cin >> v;
// #define rep(i,n) for(int i=0;i<(n);++i)
#define all(f,c,...) (([&](decltype((c)) cccc) { return (f)(begin(cccc), end(cccc), ## __VA_ARGS__); })(c))
#define _overload3(_1,_2,_3,name,...) name
#define _rep(i,n) for(int i=0;i<(n);++i)
#define repi(i,a,b) for(int i=(a);i<(b);++i)
#define rep(...) _overload3(__VA_ARGS__,repi,_rep,)(__VA_ARGS__)

#define rrep(i,n) for(int i=(n);i>=0;--i)
const int dx[4]={1,0,-1,0}, dy[4]={0,1,0,-1};
template<class T>bool chmax(T &a,const T &b){if(a<b){a=b;return 1;}return 0;}
template<class T>bool chmin(T &a,const T &b){if(b<a){a=b;return 1;}return 0;}

// debug
template<class T>ostream& operator<<(ostream& os,const vector<T>& vec){os<<"{";for(size_t i=0;i<vec.size();++i)os<<(i?", ":"")<<vec[i];os<<"}";return os;}
ostream& operator<<(ostream& os,const vector<char>&v){for(size_t i=0;i<v.size();++i)os<<v[i];return os;}
template<class T1,class T2>ostream& operator<<(ostream& os,const pair<T1,T2>& rhs){os<<"("<<rhs.first<<", "<<rhs.second<<")";return os;}


#ifdef LOCAL
void debug() {cerr << "\n";}
template<class First> void debug(const First& first) {cerr<<first<<"\n";}
template<class First, class... Rest> void debug(const First& first, const Rest&... rest) {cerr<<first<<",";debug(rest...);}
#else
#define debug(...) 42
#endif




#include <functional>
template<class T> struct SegTree {
  private:
    int size;
    vector<T> v;
    using F = function<T(T,T)>;
    F f; T default_value;
  public:
    SegTree(int n, T default_value, F f) : default_value(default_value), f(f) {
      size = 1;
      while (size < n) size *= 2;
      v.resize(2 * size - 1, default_value);
    }
    void update(int index, T val) {
      index = index + size - 1;
      v[index] = val;
      while (index > 0) {
        index = (index - 1) / 2;
        v[index] = f(v[2 * index + 1], v[2 * index + 2]);
      }
    }
    void add(int index, T val) {
      update(index, v[index + size - 1] + val);
    }
    T query(int l, int r) { return query(l, r, 0, size, 0); }
    T query(int l, int r, int cur_l, int cur_r, int index) {
      // [l, r)に対するクエリ
      if (cur_r <= l || r <= cur_l) return default_value;
      if (l <= cur_l && cur_r <= r) return v[index];

      int mid = (cur_l + cur_r) / 2;
      return f(query(l, r, cur_l, mid, 2 * index + 1), query(l, r, mid, cur_r, 2 * index + 2));
    }
};




int main() {
  int in(N);

  SegTree<int> T(1e5, 0, [](int a, int b){return a + b;});

  pair<int, pii> query[N];
  vi A;
  rep(i, N) {
    int t;
    cin >> t >> query[i].second.first >> query[i].second.second;
    query[i].first = t;
    if (t == 0)
      A.push_back(query[i].second.first);
  }
  all(sort, A);
  A.erase(all(unique, A), A.end());

  ll ans = 0;
  rep(i, N) {
    int t = query[i].first;
    if (t == 0) {
      auto [x, y] = query[i].second;
      T.add(all(lower_bound, A, x) - A.begin(), y);
    } else {
      auto [l, r] = query[i].second;
      l = all(lower_bound, A, l) - A.begin();
      r = all(lower_bound, A, r+1) - A.begin();
      ans += T.query(l, r);
    }
  }
  cout << ans << endl;

  return 0;
}
0