結果

問題 No.789 範囲の合計
ユーザー tomatoma
提出日時 2019-09-09 01:00:38
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 1,566 bytes
コンパイル時間 1,724 ms
コンパイル使用メモリ 176,072 KB
実行使用メモリ 45,764 KB
最終ジャッジ日時 2023-09-09 22:49:37
合計ジャッジ時間 11,140 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 994 ms
42,048 KB
testcase_03 AC 287 ms
4,380 KB
testcase_04 TLE -
testcase_05 AC 831 ms
42,124 KB
testcase_06 AC 879 ms
42,164 KB
testcase_07 AC 546 ms
4,380 KB
testcase_08 AC 920 ms
45,764 KB
testcase_09 AC 850 ms
42,680 KB
testcase_10 AC 717 ms
25,236 KB
testcase_11 AC 651 ms
41,976 KB
testcase_12 AC 661 ms
42,124 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include"bits/stdc++.h"
using namespace std;
#define REP(k,m,n) for(int (k)=(m);(k)<(n);(k)++)
#define rep(i,n) REP((i),0,(n))
using ll = long long;


template<typename T>
class DynamicSegmentTree {
private:
	//using F = function<T(T, T)>; // モノイド型
	const static ll n = 1ll << 30; // 横幅
	//const F f;   // モノイド
	const T e;   // モノイド単位元
	unordered_map<ll, T> data;

	// 存在しないindexではeを返します
	T at(ll idx) {
		return data.find(idx) == data.end() ? e : data[idx];
	}

public:
	// init忘れに注意
	DynamicSegmentTree(T e) :e(e) {}

	void set_val(ll idx, T val) {
		idx += n - 1;
		data[idx] = val;
		while (idx > 0) {
			idx = (idx - 1) / 2;
			data[idx] = //f(
				at(2 * idx + 1) +
				at(2 * idx + 2);
			//);
		}
	}

	T query(ll a, ll b, ll k = 0, ll l = 0, ll r = n) {
		if (r <= a || b <= l)return e;
		if (a <= l && r <= b) {
			return data.find(k) == data.end() ? e : data[k];
		}
		else {
			T vl = data.find(2 * k + 1) == data.end()
				? e
				: query(a, b, 2 * k + 1, l, (l + r) / 2);
			T vr = data.find(2 * k + 2) == data.end()
				? e
				: query(a, b, 2 * k + 2, (l + r) / 2, r);
			//return f(vl, vr);
			return vl + vr;
		}
	}
};

int main()
{
	int n;
	cin >> n;

	function<ll(ll, ll)> f = [](ll a, ll b) {return a + b; };
	DynamicSegmentTree<ll> dst(0);

	ll res = 0;
	while (n--) {
		int com, x, y;
		cin >> com >> x >> y;
		if (com == 0) {
			ll now = dst.query(x, x + 1);
			dst.set_val(x, now + y);
		}
		else {
			res += dst.query(x, y + 1);
		}
	}
	cout << res << endl;
	return 0;
}
0