結果

問題 No.789 範囲の合計
ユーザー manjuuu_onsenmanjuuu_onsen
提出日時 2021-09-14 01:11:27
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 881 ms / 1,000 ms
コード長 1,406 bytes
コンパイル時間 1,887 ms
コンパイル使用メモリ 176,116 KB
実行使用メモリ 45,668 KB
最終ジャッジ日時 2023-09-08 09:56:39
合計ジャッジ時間 10,316 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,384 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 832 ms
42,236 KB
testcase_03 AC 174 ms
4,380 KB
testcase_04 AC 881 ms
42,724 KB
testcase_05 AC 704 ms
42,060 KB
testcase_06 AC 730 ms
42,064 KB
testcase_07 AC 331 ms
4,384 KB
testcase_08 AC 759 ms
45,668 KB
testcase_09 AC 681 ms
42,780 KB
testcase_10 AC 599 ms
25,160 KB
testcase_11 AC 643 ms
42,280 KB
testcase_12 AC 637 ms
42,128 KB
testcase_13 AC 2 ms
4,384 KB
testcase_14 AC 2 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>

using namespace std;
using ll = long long;
using pint = pair<int,int>;

template<class M> class SegmentTree { public: using T = typename M::value_type;
private:
	int m, n;
	unordered_map<int, T> data;
	
	T modify(int node) {
		if(data.count(node)) return data[node];
		else return M::id();
	}

	T rec(int l, int r, int node, int node_l, int node_r) {
		if(node_r <= l || node_l >= r)return M::id();
		if(node_l >= l && node_r <= r)return modify(node);
		int mid = (node_l + node_r) / 2;
		return M::op( rec(l, r, node * 2, node_l, mid), rec(l, r, node * 2 + 1, mid, node_r) );
	}

public:
	SegmentTree() = default;
	SegmentTree(int N) {
		m = N;
		for(n = 1; n < N; n <<= 1);
	}

	T prod(int l, int r) {return rec(l, r, 1, 0, n);}
	T get (int i){ return modify(i + n); }
	T all_prod() {return modify(1);}

	void set(int index, T val) {
		index += n;
		data[index] = val;
		while(index != 1){
			index >>= 1;
			data[index] = M::op( modify(index<<1), modify((index<<1) + 1));
		}
	}
 
};

template<class T> class RSQ { public: using value_type = T;
	static T op(T l, T r){ return l + r; }
	static T id(){return 0;}
};

int main() {
	int Q; cin >> Q;
	SegmentTree<RSQ<ll>> seg(1'000'000'001);
	ll ans = 0;
	for(int i = 0; i < Q; i++) {
		int t, a, b; cin >> t >> a >> b;
		if(t == 0) seg.set(a, seg.get(a) + b);
		else {
			ans += seg.prod(a, b + 1);
		}
	}

	cout << ans << endl;

}
0