結果

問題 No.789 範囲の合計
ユーザー tomatoma
提出日時 2019-09-09 01:44:12
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 1,517 bytes
コンパイル時間 1,654 ms
コンパイル使用メモリ 174,292 KB
実行使用メモリ 85,840 KB
最終ジャッジ日時 2023-09-09 22:54:19
合計ジャッジ時間 11,668 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,384 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 TLE -
testcase_03 AC 163 ms
4,584 KB
testcase_04 TLE -
testcase_05 AC 809 ms
60,672 KB
testcase_06 AC 987 ms
83,756 KB
testcase_07 AC 177 ms
4,636 KB
testcase_08 TLE -
testcase_09 AC 991 ms
83,736 KB
testcase_10 AC 735 ms
46,924 KB
testcase_11 AC 778 ms
83,724 KB
testcase_12 AC 787 ms
83,732 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 1 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;

constexpr ll n = 1ll << 30; // 横幅

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

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

	void add_val(int idx, ll val) {
		idx += n - 1;
		//if (data.find(idx) == data.end())data[idx] = 0;
		data[idx] += val;
		while (idx > 0) {
			idx = (idx - 1) / 2;
			int num = 2 * idx + 1;
			data[idx] = data[num] + data[num + 1];
			//(data.find(num) == data.end() ? 0 : data[num]) +
			//(data.find(num + 1) == data.end() ? 0 : data[num + 1]);
		}
	}

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

int main()
{
	int n;
	cin >> n;
	int com[100000], x[100000], y[100000];
	rep(i, n)scanf("%d %d %d", com + i, x + i, y + i);

	DynamicSegmentTree dst;
	ll res = 0;
	rep(i, n) {
		if (com[i]) {
			res += dst.query(x[i], y[i] + 1);
		}
		else {
			dst.add_val(x[i], y[i]);
		}
	}
	cout << res << endl;
	return 0;
}
0