結果

問題 No.789 範囲の合計
コンテスト
ユーザー Unbakedbread
提出日時 2026-07-17 13:18:03
言語 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  
実行時間 87 ms / 1,000 ms
+ 843µs
コード長 1,354 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 2,604 ms
コンパイル使用メモリ 334,764 KB
実行使用メモリ 24,188 KB
最終ジャッジ日時 2026-07-17 13:18:17
合計ジャッジ時間 3,936 ms
ジャッジサーバーID
(参考情報)
judge2_1 / judge1_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 15
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

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

const int N = 1e9 + 100;

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

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

int make_node(const int a, const int b) {
	t.push_back(Node(a, b));
	return ssize(t) - 1;
}

void add(int x, const int p, const int v) {
    const int a = t[x].a, b = t[x].b;
	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(a, c);
	    add(t[x].l, p, v);
	} else {
	    if(t[x].r == -1) t[x].r = make_node(c, b);
	    add(t[x].r, 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 l, const int r) {
    const int a = t[x].a, b = t[x].b;
	if(x == -1 or r <= a or b <= l) return 0;
	if(l <= a and b <= r) return t[x].val;
	return prod(t[x].l, l, r) + prod(t[x].r, l, r);
} 

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