結果

問題 No.789 範囲の合計
ユーザー tomatoma
提出日時 2019-09-25 00:51:07
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 360 ms / 1,000 ms
コード長 1,869 bytes
コンパイル時間 3,139 ms
コンパイル使用メモリ 186,172 KB
実行使用メモリ 31,232 KB
最終ジャッジ日時 2023-10-19 18:36:56
合計ジャッジ時間 6,012 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 320 ms
29,120 KB
testcase_03 AC 84 ms
8,860 KB
testcase_04 AC 297 ms
28,328 KB
testcase_05 AC 237 ms
29,384 KB
testcase_06 AC 261 ms
29,120 KB
testcase_07 AC 75 ms
8,860 KB
testcase_08 AC 174 ms
18,100 KB
testcase_09 AC 168 ms
16,252 KB
testcase_10 AC 360 ms
31,232 KB
testcase_11 AC 271 ms
28,328 KB
testcase_12 AC 275 ms
28,328 KB
testcase_13 AC 2 ms
4,348 KB
testcase_14 AC 2 ms
4,348 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 SegmentTree {
private:
	using F = function<T(T, T)>; // モノイド型
	int n; // 横幅
	F f;   // モノイド
	T e;   // モノイド単位元
	vector<T> data;

public:
	// init忘れに注意
	SegmentTree() {}
	SegmentTree(F f, T e) :f(f), e(e) {}
	void init(int n_) {
		n = 1;
		while (n < n_)n <<= 1;
		data.assign(n << 1, e);
	}
	void build(const vector<T>& v) {
		int n_ = v.size();
		init(n_);
		rep(i, n_)data[n + i] = v[i];
		for (int i = n - 1; i >= 0; i--) {
			data[i] = f(data[(i << 1) | 0], data[(i << 1) | 1]);
		}
	}
	void set_val(int idx, T val) {
		idx += n;
		data[idx] = val;
		while (idx >>= 1) {
			data[idx] = f(data[(idx << 1) | 0], data[(idx << 1) | 1]);
		}
	}
	T query(int a, int b) {
		// [a,b)
		T vl = e, vr = e;
		for (int l = a + n, r = b + n; l < r; l >>= 1, r >>= 1) {
			if (l & 1)vl = f(vl, data[l++]); // unknown
			if (r & 1)vr = f(data[--r], vr); // unknown
		}
		return f(vl, vr);
	}
};

int main()
{
	// input
	ll n;
	cin >> n;
	vector<vector<ll>> querys(n, vector<ll>(3));
	rep(i, n)rep(j, 3)cin >> querys[i][j];

	// compression
	int cnt = 0;
	set<ll> st;
	map<ll, ll> trans;
	for (const auto& query : querys) {
		st.insert(query[1]);
		if (query[0] == 1)st.insert(query[2]);
	}
	for (const auto& num : st)trans[num] = cnt++;

	// segment init
	auto f = [](ll a, ll b) {return a + b; };
	SegmentTree<ll> seg(f, 0);
	seg.init(cnt);

	// query
	ll ans = 0;
	for (const auto& query : querys) {
		ll x = query[1], y = query[2];
		if (query[0] == 0) {
			x = trans[x];
			ll now = seg.query(x, x + 1);
			seg.set_val(x, now + y);
		}
		else {
			ll l = trans[x], r = trans[y];
			ans += seg.query(l, r + 1);
		}
	}
	cout << ans << endl;
	return 0;
}
0