結果

問題 No.789 範囲の合計
ユーザー hamrayhamray
提出日時 2020-08-10 23:40:18
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 125 ms / 1,000 ms
コード長 2,783 bytes
コンパイル時間 1,500 ms
コンパイル使用メモリ 160,524 KB
実行使用メモリ 34,432 KB
最終ジャッジ日時 2024-04-17 07:32:42
合計ジャッジ時間 3,974 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 AC 119 ms
28,288 KB
testcase_03 AC 41 ms
5,376 KB
testcase_04 AC 125 ms
31,616 KB
testcase_05 AC 101 ms
27,392 KB
testcase_06 AC 105 ms
28,672 KB
testcase_07 AC 37 ms
5,376 KB
testcase_08 AC 109 ms
34,432 KB
testcase_09 AC 96 ms
31,744 KB
testcase_10 AC 104 ms
19,968 KB
testcase_11 AC 82 ms
28,160 KB
testcase_12 AC 79 ms
28,032 KB
testcase_13 AC 1 ms
5,376 KB
testcase_14 AC 2 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
 
const double pi = 3.141592653589793238462643383279;
using namespace std;
//typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef pair<long long, long long> PLL;
typedef pair<int, PII> TIII;
typedef long long LL;
typedef unsigned long long ULL;
typedef vector<LL> VLL;
typedef vector<VLL> VVLL;
 
 
//container util
//------------------------------------------
#define ALL(a)  (a).begin(),(a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define SQ(a) ((a)*(a))
#define EACH(i,c) for(typeof((c).begin()) i=(c).begin(); i!=(c).end(); ++i)
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c) sort((c).begin(),(c).end())
 
 
//repetition
//------------------------------------------
#define FOR(i,s,n) for(int i=s;i<(int)n;++i)
#define REP(i,n) FOR(i,0,n)
#define MOD 1000000007
 
 
#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define trav(a, x) for(auto& a : x)
#define all(x) x.begin(), x.end()
#define sz(x) (int)(x).size()
 
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
 
#define chmin(x,y) x=min(x,y)
#define chmax(x,y) x=max(x,y)

//ここから編集

struct SegmentTree {
  struct Node {
    Node *left, *right;
    ll v;
    Node() : left(nullptr), right(nullptr), v(0) {}  
  };

  Node * root;
  int n, n0;

  SegmentTree(int n) : n(n) {
    n0 = 1;
    while(n0 < n) n0 <<= 1;
    root = new Node();
  }
  
  ~SegmentTree() {
    delete root; root = nullptr;
  }

  void update(int k, ll x){
    Node *n = root;  
    int l = 0, r = n0;
    n->v = (n->v + x) % MOD;
    while(r-l>1){
      int m = (l+r)>>1;
      if(k < m){
        if(!n->left) n->left = new Node();
        n = n->left;

        r = m;
      } else {
        if(!n->right) n->right = new Node();
        n = n->right;

        l = m;
      }

      n->v = (n->v + x) % MOD;
    }
  }

  ll query(int a, int b, Node *n, int l, int r) {
    if(a <= l && r <= b) {
      return n-> v;
    }
    if(r <= a || b <= l){
      return 0;
    }

    ll lv = n->left ? query(a, b, n->left, l, (l+r)>>1) : 0;
    ll rv = n->right ? query(a, b, n->right, (l+r)>>1, r) : 0;
    return (lv+rv) % MOD;
  }

  ll query(int a, int b){
    return query(a, b, root, 0, n0);
  }
};
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(5);
  

  int N; cin >> N;
  SegmentTree seg((1<<30));
  ll ans = 0;
  REP(i,N){
    int q; cin >> q;
    if(q == 0){
      int x, y; cin >> x >> y;
      seg.update(x, y);
    }else{
      int l, r; cin >> l >> r;
      ans += seg.query(l, r+1);
      
    }
  }
  cout << ans << endl;
  return 0;
}
0