結果

問題 No.789 範囲の合計
ユーザー manjuuu_onsenmanjuuu_onsen
提出日時 2021-09-14 01:08:52
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 1,411 bytes
コンパイル時間 2,328 ms
コンパイル使用メモリ 208,320 KB
実行使用メモリ 92,884 KB
最終ジャッジ日時 2023-09-08 09:53:48
合計ジャッジ時間 4,707 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
8,756 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 TLE -
testcase_03 -- -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
権限があれば一括ダウンロードができます

ソースコード

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;
	map<int, T> data;
	
	T modify(int node) {
		if(data.count(node)) return data[node];
		else return (data[node] = 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