結果

問題 No.789 範囲の合計
ユーザー AC2KAC2K
提出日時 2023-03-27 20:41:28
言語 C++23(draft)
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 148 ms / 1,000 ms
コード長 3,849 bytes
コンパイル時間 5,343 ms
コンパイル使用メモリ 248,812 KB
実行使用メモリ 35,108 KB
最終ジャッジ日時 2023-10-19 19:34:07
合計ジャッジ時間 5,085 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 134 ms
29,024 KB
testcase_03 AC 42 ms
4,348 KB
testcase_04 AC 148 ms
32,224 KB
testcase_05 AC 109 ms
27,864 KB
testcase_06 AC 114 ms
29,060 KB
testcase_07 AC 38 ms
4,348 KB
testcase_08 AC 119 ms
35,108 KB
testcase_09 AC 107 ms
32,224 KB
testcase_10 AC 111 ms
20,420 KB
testcase_11 AC 88 ms
28,476 KB
testcase_12 AC 90 ms
28,476 KB
testcase_13 AC 2 ms
4,348 KB
testcase_14 AC 2 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#line 2 "template.hpp"
#include<bits/stdc++.h>
using namespace std;
#define rep(i, N)  for(int i=0;i<(N);i++)
#define all(x) (x).begin(),(x).end()
#define popcount(x) __builtin_popcount(x)
using i128=__int128_t;
using ll = long long;
using ld = long double;
using graph = vector<vector<int>>;
using P = pair<int, int>;
constexpr int inf = 1e9;
constexpr ll infl = 1e18;
constexpr ld eps = 1e-6;
const long double pi = acos(-1);
constexpr uint64_t MOD = 1e9 + 7;
constexpr uint64_t MOD2 = 998244353;
constexpr int dx[] = { 1,0,-1,0 };
constexpr int dy[] = { 0,1,0,-1 };
template<class T>inline void chmax(T&x,T y){if(x<y)x=y;}
template<class T>inline void chmin(T&x,T y){if(x>y)x=y;}
#line 1 "data-structure/dynamic_segtree.hpp"
/// @brief Dynamic Segment Tree(動的セグメント木)
/// @tparam S 要素の型
/// @tparam op 二項演算
/// @tparam e 単位元
/// @docs docs/data-structure/dynamic_segtree.md
template<class S,S(*op)(S,S),S(*e)()>
class dynamic_segtree {
public:
	dynamic_segtree(const size_t& n) :n(n), root(nullptr) {}
private:
	struct node {
		S val;
		node* left;
		node* right;

		node(const S& v) :val(v), left(nullptr), right(nullptr) {}
	};
	node* root;
	size_t n;
public:
	void update(const size_t& p, const S& x) { 
		assert(0 <= p && p < n);
		internal_update(root, 0, n, p, x); 
	}
	void add(const size_t& p, const S& x) { 
		assert(0 <= p && p < n);
		internal_add(root, 0, n, p, x); 
	}
	S operator[](const size_t& p) { 
		assert(0 <= p && p < n);
		return internal_access(root, 0, n, p); 
	}
	S prod(const size_t& l, const size_t& r) {
		assert(0 <= l && l <= r && r <= n);
		if (l == r) {
			return e();
		}

		return internal_prod(root, 0, n, l, r);
	}

private:
	void internal_update(node*& p, const size_t& l, const size_t& r, const  size_t& idx, const S& new_val) {
		if (p == nullptr) {
			p = new node(e());
		}

		if (r - l == 1) {
			p->val = new_val;
			return;
		}

		size_t mid = (l + r) >> 1;
		if (idx < mid) internal_update(p->left, l, mid, idx, new_val);
		else internal_update(p->right, mid, r, idx, new_val);
		p->val = e();
		if (p->left!=nullptr) p->val = op(p->left->val, p->val);
		if (p->right!=nullptr) p->val = op(p->val, p->right->val);
	}
	void internal_add(node*& p, const size_t& l, const size_t& r, const  size_t& idx, const S& new_val) {
		if (p == nullptr) {
			p = new node(e());
		}

		if (r - l == 1) {
			p->val = op(p->val, new_val);;
			return;
		}

		size_t mid = (l + r) >> 1;
		if (idx < mid) internal_add(p->left, l, mid, idx, new_val);
		else internal_add(p->right, mid, r, idx, new_val);
		p->val = e();
		if (p->left!=nullptr) p->val = op(p->left->val, p->val);
		if (p->right!=nullptr) p->val = op(p->val, p->right->val);
	}


	S internal_access(node*& p, const size_t& l, const size_t& r, const  size_t& idx) {
		if (p == nullptr) {
			return e();
		}
		if (r - l == 1) {
			return p->val;
		}

		size_t mid = (l + r) >> 1;
		if (idx < mid) return internal_access(p->left, l, mid, idx);
		else return internal_access(p->right, mid, r, idx);
	}

	S internal_prod(node*& p, const size_t& l, const size_t& r, const size_t& L, const size_t& R) {
		if (p == nullptr || r <= L || R <= l) {
			return e();
		}
		if (L <= l && r <= R) {
			return p->val;
		}

		size_t mid = (l + r) >> 1;
		return op(internal_prod(p->left, l, mid, L, R), internal_prod(p->right, mid, r, L, R));
	}
};
#line 3 "main.cpp"
int op(int x, int y) { return x + y; }
int e() { return 0; }
int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
	const size_t n = 1000000001;
	dynamic_segtree<int, op, e> seg(n);

	int q;
	cin >> q;
	long long ans = 0;
	while (q--) {
		int type;
		cin >> type;
		if (type == 0) {
			size_t x;
			long long y;
			cin >> x >> y;
			seg.add(x, y);
		}
		else {
			size_t l, r;
			cin >> l >> r;
			ans += seg.prod(l, r + 1);
		}
	}
	cout << ans << '\n';
}
0