結果

問題 No.789 範囲の合計
ユーザー WarToksWarToks
提出日時 2019-04-08 02:35:55
言語 C++17(clang)
(17.0.6 + boost 1.83.0)
結果
AC  
実行時間 189 ms / 1,000 ms
コード長 5,474 bytes
コンパイル時間 1,463 ms
コンパイル使用メモリ 108,988 KB
実行使用メモリ 16,900 KB
最終ジャッジ日時 2023-08-20 12:20:02
合計ジャッジ時間 4,506 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 175 ms
15,848 KB
testcase_03 AC 41 ms
5,412 KB
testcase_04 AC 161 ms
15,548 KB
testcase_05 AC 125 ms
16,152 KB
testcase_06 AC 134 ms
15,856 KB
testcase_07 AC 35 ms
5,400 KB
testcase_08 AC 86 ms
10,132 KB
testcase_09 AC 77 ms
8,788 KB
testcase_10 AC 189 ms
16,900 KB
testcase_11 AC 140 ms
15,580 KB
testcase_12 AC 136 ms
15,592 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <set>
#include <queue>
#include <stack>
#include <vector>
#include <algorithm>
#include <map>
#include <chrono>
#include <math.h>
using namespace std;

using lli = long long int;
using Vint = std::vector<int>;
using Vlli = std::vector<lli>;
using Wint = std::vector<Vint>;
using Wlli = std::vector<Vlli>;
using Vbool = std::vector<bool>;
using pii = std::pair<int, int>;
using pll = std::pair<lli, lli>;

constexpr int MOD = 1e9 + 7;
constexpr int INFi = 2e9 + 1;
constexpr lli INFl = (lli)(9e18) + 1;
const vector<pii> DXDY = {std::make_pair(1, 0), std::make_pair(-1, 0), std::make_pair(0, 1), std::make_pair(0, -1)};
constexpr char BR = '\n';

#define FOR(i, a, b) for(int (i) = (a); (i) < (b); (i)++)
#define FOReq(i, a, b) for(int (i) = (a); (i) <= (b); (i)++)
#define rFOR(i, a, b) for(int (i) = (b); (i) >= (a); i--)
#define FORstep(i, a, b, step) for(int (i) = (a); i < (b); i += (step))
#define REP(i, n) FOR(i, 0, n)
#define rREP(i, n) rFOR(i, 0, (n-1))
#define vREP(ele, vec) for(auto &(ele) : (vec))
#define vREPcopy(ele, vec) for(auto (ele) : (vec))
#define SORT(A) std::sort((A).begin(), (A).end())
// 座標圧縮 (for vector) : ソートしてから使うのが一般的 ; SORT(A) => COORDINATE_COMPRESSION(A)
#define COORDINATE_COMPRESSION(A) (A).erase(unique((A).begin(),(A).end()),(A).end())



template <class T> inline int argmin(vector<T> vec){return min_element(vec.begin(), vec.end()) - vec.begin();}
template <class T> inline int argmax(vector<T> vec){return max_element(vec.begin(), vec.end()) - vec.begin();}
template <class T> inline void chmax(T &a, T b){a = max(a, b);}
template <class T> inline void chmin(T &a, T b){a = min(a, b);}
inline int toInt(string &s){int res = 0; for(char a : s) res = 10 * res + (a - '0'); return res;}
inline int toInt(const string s){int res = 0; for(char a : s) res = 10 * res + (a - '0'); return res;}
inline long long int toLong(string &s){lli res = 0; for(char a : s) res = 10 * res + (a - '0'); return res;}
inline long long int toLong(const string s){lli res = 0; for(char a : s) res = 10 * res + (a - '0'); return res;}
template <class T> inline std::string toString(T n){
  if(n == 0) return "0";
  std::string res = "";
  if(n < 0){n = -n;while(n != 0){res += (char)(n % 10 + '0'); n /= 10;}
  std::reverse(res.begin(), res.end()); return '-' + res;}
  while(n != 0){res += (char)(n % 10 + '0'); n /= 10;} std::reverse(res.begin(), res.end()); return res;
}

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~



template <class T>
class SegKi{
  int n;
  std::vector<T> DataTree;
  public:
    // 区間の長さszから(2^k ≥ sz)となるkを求めて初期化
    SegKi(int sz){
      this->n = 1;
      while((1 << this->n) < sz) this->n++;
      this->DataTree.resize((1 << (this->n + 1)) -1);
    }
    // デフォルト値をしてして初期化
    SegKi(int sz, T defaultValue){
      this->n = 1;
      while((1 << this->n) < sz) this->n++;
      this->DataTree.resize((1 << (this->n + 1)) - 1, defaultValue);
      for(int i = (1 << this->n) - 2; i >= 0; i--)
            DataTree.at(i) = DataTree.at(2 * i + 1) + DataTree.at(2 * i + 2);
    }
    // vectorによって初期化
    SegKi(vector<T> A){
      const int sz = A.size();
      this->n = 1;
      while((1 << n) < sz) this->n++;
      DataTree.resize(1 << this->n);
      for(int i = 0; i < sz; i++) DataTree.at(i) = A.at(i);
      for(int i = (1 << this->n) - 2; i >= 0; i--)
            DataTree.at(i) = DataTree.at(2 * i + 1) + DataTree.at(2 * i + 2);
    }
    void update(int k, T value){
      // A[k]の値をvalueで更新
      k += (1 << this->n) - 1;
      DataTree.at(k) = value;
      while(k > 0){
        k = (k - 1) / 2;
        DataTree.at(k) = DataTree.at(2 * k + 1) + DataTree.at(2 * k + 2);
      }
    }
    void add(int k, T value){
      // A[k]の値をvalueで更新
      k += (1 << this->n) - 1;
      DataTree.at(k) += value;
      while(k > 0){
        k = (k - 1) / 2;
        DataTree.at(k) = DataTree.at(2 * k + 1) + DataTree.at(2 * k + 2);
      }
    }
    T getsum(int a, int b, T k=0, int l=0, int r=-1) {
      if(r < 0) r = (1 << this->n);
      if(b <= l or  r <= a) return 0;
      if(a <= l and r <= b) return DataTree.at(k);
      T vl = getsum(a, b, 2 * k + 1, l, (l+r)/2);
      T vr = getsum(a, b, 2 * k + 2, (l+r)/2, r);
      return vl + vr;
    }
    T getValue(int j){
      return DataTree.at(j + (1 << this->n) - 1);
    }
};


int main(void){
  int n, q, x, y; scanf("%d", &n);
  using sub_information_number = std::pair<int, int>;
  using information = std::pair<bool, sub_information_number>;

  std::vector<information> Query(n);

  Vint Coordinate;
  REP(i, n){
    scanf("\n%d %d %d", &q, &x, &y);
    Query.at(i) = make_pair(q == 0, make_pair(x, y));
    Coordinate.push_back(x);
    if(q == 1) Coordinate.push_back(y);
  }
  SORT(Coordinate);
  COORDINATE_COMPRESSION(Coordinate);

  map<int, int> Indexes;
  const int Asz = Coordinate.size();
  REP(i, Asz) Indexes[Coordinate[i]] = i;

  lli ans = 0;
  SegKi<lli> Data(Asz);
  vREP(ele, Query){
    const sub_information_number United = ele.second;
    if(ele.first){
      int position = Indexes[United.first];
      Data.add(position, United.second);
    }
    else{
      int position_l = Indexes[United.first],
      position_r = Indexes[United.second] + 1;
      ans += Data.getsum(position_l, position_r);
    }
  }
  printf("%lld\n", ans);
  return 0;
}
0