結果

問題 No.789 範囲の合計
コンテスト
ユーザー Unbakedbread
提出日時 2026-07-17 13:25:53
言語 C++23
(gcc 15.2.0 + boost 1.90.0)
コンパイル:
g++-15 -O2 -lm -std=c++23 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 72 ms / 1,000 ms
+ 544µs
コード長 1,346 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 2,483 ms
コンパイル使用メモリ 335,056 KB
実行使用メモリ 15,104 KB
最終ジャッジ日時 2026-07-17 13:26:01
合計ジャッジ時間 3,743 ms
ジャッジサーバーID
(参考情報)
judge3_0 / judge1_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 15
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <bits/stdc++.h>
using namespace std;

const int N = (1 << 30);

struct Node {
	int l, r, val;
	Node() : l(-1), r(-1), val(0) {}
};

vector<Node> t = {Node()};
const int root = 0;

int make_node() {
	t.push_back(Node());
	return ssize(t) - 1;
}

void add(int x, const int a, const int b, const int p, const int v) {
	if(b - a == 1) {
		t[x].val += v;
		return;
	}
	
	const int c = (a + b) / 2;
	if(p < c) {
	    if(t[x].l == -1) t[x].l = make_node();
	    add(t[x].l, a, c, p, v);
	} else {
	    if(t[x].r == -1) t[x].r = make_node();
	    add(t[x].r, c, b, p, v);
	}
	
	t[x].val = 0;
	if(t[x].l != -1) t[x].val += t[t[x].l].val;
	if(t[x].r != -1) t[x].val += t[t[x].r].val;
}

int prod(const int x, const int a, const int b, const int l, const int r) {
	if(x == -1 or r <= a or b <= l) return 0;
	if(l <= a and b <= r) return t[x].val;
	const int c = (a + b) / 2;
	return prod(t[x].l, a, c, l, r) + prod(t[x].r, c, b, l, r);
} 

int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	cout.tie(nullptr);
	
	int Q;
	cin >> Q;
	
	t.reserve(Q * 32);
	
	int64_t ans = 0;
	while(Q--) {
		int cmd;
		cin >> cmd;
		
		if(cmd == 0) {
			int x, y;
			cin >> x >> y;
			
			add(root, 0, N, x, y);
		}
		
		if(cmd == 1) {
			int l, r;
			cin >> l >> r, ++r;
			
			ans += prod(root, 0, N, l, r);
		}
	}
	
	cout << ans << "\n";
	
	return 0;
}
0