結果

問題 No.789 範囲の合計
ユーザー AC2KAC2K
提出日時 2023-03-27 20:07:50
言語 C++23(draft)
(gcc 12.3.0 + boost 1.83.0)
結果
RE  
実行時間 -
コード長 3,961 bytes
コンパイル時間 3,673 ms
コンパイル使用メモリ 249,444 KB
実行使用メモリ 32,196 KB
最終ジャッジ日時 2023-10-19 19:01:21
合計ジャッジ時間 6,308 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 148 ms
28,996 KB
testcase_03 AC 43 ms
4,348 KB
testcase_04 AC 169 ms
32,196 KB
testcase_05 AC 114 ms
27,836 KB
testcase_06 RE -
testcase_07 AC 37 ms
4,348 KB
testcase_08 RE -
testcase_09 AC 115 ms
32,196 KB
testcase_10 AC 128 ms
20,392 KB
testcase_11 AC 89 ms
28,448 KB
testcase_12 AC 92 ms
28,448 KB
testcase_13 AC 2 ms
4,348 KB
testcase_14 AC 2 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#line 1 "test/yuki/No-789.test.cpp"
#define PROBLEM "https://yukicoder.me/problems/no/789"

#line 2 "src/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 "src/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();
		}
		if (l == 0 && r == n) {
			return root->val;
		}

		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 5 "test/yuki/No-789.test.cpp"

ll op(ll x, ll y) { return x + y; }
ll e() { return 0; }
int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    dynamic_segtree<ll,op,e> sg(inf+1);
    int n;
    cin >> n;
    ll s = 0;
    while (n--) {
        int t, a, b;
        cin >> t >> a >> b;
        if (t == 0) {
            sg.add(a, b);
        } else {
            s += sg.prod(a, b + 1);
        }
    }
    cout << s << '\n';
}
0