結果

問題 No.789 範囲の合計
ユーザー goodbatongoodbaton
提出日時 2019-03-08 13:17:42
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 389 ms / 1,000 ms
コード長 4,816 bytes
コンパイル時間 1,548 ms
コンパイル使用メモリ 113,976 KB
実行使用メモリ 91,136 KB
最終ジャッジ日時 2024-06-23 15:00:24
合計ジャッジ時間 5,223 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 2 ms
6,944 KB
testcase_02 AC 332 ms
74,496 KB
testcase_03 AC 79 ms
6,940 KB
testcase_04 AC 389 ms
83,200 KB
testcase_05 AC 280 ms
71,168 KB
testcase_06 AC 299 ms
74,624 KB
testcase_07 AC 78 ms
6,944 KB
testcase_08 AC 334 ms
91,136 KB
testcase_09 AC 303 ms
83,328 KB
testcase_10 AC 259 ms
50,560 KB
testcase_11 AC 200 ms
72,576 KB
testcase_12 AC 212 ms
72,576 KB
testcase_13 AC 2 ms
6,940 KB
testcase_14 AC 2 ms
6,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>

#include <iostream>
#include <complex>
#include <string>
#include <algorithm>
#include <numeric>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>

#include <functional>
#include <cassert>

typedef long long ll;
using namespace std;

#ifndef LOCAL
#define debug(x) ;
#else
#define debug(x) cerr << __LINE__ << " : " << #x << " = " << (x) << endl;

template <typename T1, typename T2>
ostream &operator<<(ostream &out, const pair<T1, T2> &p) {
  out << "{" << p.first << ", " << p.second << "}";
  return out;
}

template <typename T>
ostream &operator<<(ostream &out, const vector<T> &v) {
  out << '{';
  for (const T &item : v) out << item << ", ";
  out << "\b\b}";
  return out;
}
#endif

#define mod 1000000007 //1e9+7(prime number)
#define INF 1000000000 //1e9
#define LLINF 2000000000000000000LL //2e18
#define SIZE 200010

/* Starry Sky Tree */
//0-index

struct StarrySkyTree{
  typedef int Type;
  int segn2;
  vector<Type> data, s_data;
  function<Type(Type, Type)> merge;

  StarrySkyTree(function<Type(Type, Type)> merge, int n): merge(merge)
  {
    for(segn2=1; segn2<n; segn2*=2);
    data.assign(segn2*2, 0);
    s_data.assign(segn2*2, 0);
  }

  StarrySkyTree(int n): //Original Ver.
    StarrySkyTree([](Type a, Type b){ return max(a, b); }, n) {}

  //get value of [a,b)
  Type query(int a, int b, int l = 0, int r = -1, int k = 0){
    if(r == -1) r = segn2;
    if(r <= a || b <= l) return -INF; //大きさに注意
    if(a <= l && r <= b) return data[k] + s_data[k];
    return merge(query(a, b, l, (l+r)/2, k*2+1), query(a, b, (l+r)/2 , r, k*2+2)) + s_data[k];
  }

  //add x to [a,b)
  Type add(int a, int b, Type x, int l = 0, int r = -1, int k = 0){
    if(r == -1) r = segn2;
    if(a <= l && r <= b)
      s_data[k] += x;
    else if(a < r && l < b)
      data[k] = merge(add(a, b, x, l, (l+r)/2, k*2+1), add(a, b, x, (l+r)/2, r, k*2+2));

    return data[k] + s_data[k];
  }

};



template <typename T>
struct DynamicRangeAddSegtree{
  struct Node{
    T val, add;
    Node *l, *r;

    Node():val(0), add(0), l(NULL), r(NULL){}
    ~Node() {
      if (l) delete l;
      if (r) delete r;
    }
  };

  Node *root;
  ll segn2;

  DynamicRangeAddSegtree(ll n){
    for(segn2=1; segn2<n; segn2*=2);
    root = new Node();
  }

  virtual ~DynamicRangeAddSegtree() {
    delete root;
  }

  virtual T merge(T a, T b) = 0;
  virtual T rangeAdd(ll a, ll b, ll l, ll r, T add) = 0;

  void add(ll a, ll b, T c) {
    add(a, b, c, 0, segn2, root);
  }

  T query(ll a, ll b) {
    return query(a, b, 0, segn2, root);
  }

private:
  T add(ll a, ll b, T c, ll l, ll r, Node *node){
    if(!(r <= a || b <= l)) {
      if(a <= l && r <= b){
        node->add += c;
        debug(c);
      } else {
        if(node->l == NULL) node->l = new Node();
        if(node->r == NULL) node->r = new Node();

        T lVal = add(a, b, c, l, (l+r)/2, node->l);
        T rVal = add(a, b, c, (l+r)/2, r, node->r);

        if (l == 4 && r == 6) {
          debug(lVal); debug(rVal);
        }

        node->val = merge(lVal, rVal);
      }
    }

    return node->val + rangeAdd(l, r, l, r, node->add);
  }

  T query(ll a, ll b, ll l, ll r, Node *node){
    if(r <= a || b <= l) return 0;

    if(a <= l && r <= b){
      debug(node->val + node->add);
      debug(l);
      debug(r);
      return node->val + rangeAdd(a, b, l, r, node->add);
    }

    T res = 0;

    if (node->l) res = merge(res, query(a, b, l, (l+r)/2, node->l));
    if (node->r) res = merge(res, query(a, b, (l+r)/2, r, node->r));

    return res + rangeAdd(a, b, l, r, node->add);
  }
};

template <typename T>
struct DynamicRangeAddRangeSumSegtree : public DynamicRangeAddSegtree<T> {
  DynamicRangeAddRangeSumSegtree(ll n) : DynamicRangeAddSegtree<T>(n) {}

  T merge(T a, T b) {
    return a + b;
  }

  T rangeAdd(ll a, ll b, ll l, ll r, T add) {
    if (add) {
      auto v = vector<ll>{a,b,l,r, add}; debug(v);
      debug(min(r, b) - max(a, l));
    }
    return max(0LL, min(r, b) - max(a, l)) * add;
  }
};

template <typename T>
struct DynamicRangeAddRangeMinSegtree : public DynamicRangeAddSegtree<T> {
  DynamicRangeAddRangeMinSegtree(ll n) : DynamicRangeAddSegtree<T>(n) {}

  T merge(T a, T b) {
    return min(a, b);
  }

  T rangeAdd(ll a, ll b, ll l, ll r, T add) {
    return add;
  }
};

int main(){
  int N = 0;

  scanf("%d", &N);

  DynamicRangeAddRangeSumSegtree<ll> seg((int)1e9 + 1);

  ll ans = 0;

  while(N--) {
    int c, a, b;

    scanf("%d%d%d", &c, &a, &b);

    if (c == 0)
      seg.add(a, a+1, b);
    else
      ans += seg.query(a, b+1);

    debug(ans);
  }

  debug(seg.query(0, 100000000));

  printf("%lld\n", ans);


  return 0;
}
0