結果

問題 No.789 範囲の合計
ユーザー leaf_1415leaf_1415
提出日時 2019-02-08 22:23:06
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 162 ms / 1,000 ms
コード長 1,597 bytes
コンパイル時間 655 ms
コンパイル使用メモリ 70,240 KB
実行使用メモリ 15,672 KB
最終ジャッジ日時 2023-09-14 03:47:03
合計ジャッジ時間 3,023 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 5 ms
11,272 KB
testcase_01 AC 5 ms
11,076 KB
testcase_02 AC 152 ms
15,600 KB
testcase_03 AC 87 ms
15,632 KB
testcase_04 AC 138 ms
15,672 KB
testcase_05 AC 125 ms
15,592 KB
testcase_06 AC 130 ms
15,664 KB
testcase_07 AC 75 ms
15,524 KB
testcase_08 AC 102 ms
15,592 KB
testcase_09 AC 102 ms
15,568 KB
testcase_10 AC 162 ms
15,532 KB
testcase_11 AC 132 ms
15,528 KB
testcase_12 AC 135 ms
15,672 KB
testcase_13 AC 5 ms
11,300 KB
testcase_14 AC 5 ms
11,184 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <algorithm>
#define llint long long

using namespace std;

struct SegTree{
	int size;
	vector<llint> seg;
	
	SegTree(){}
	SegTree(int size){
		this->size = size;
		seg.resize(1<<(size+1));
	}
	
	void init()
	{
		for(int i = 0; i < (1<<(size+1)); i++) seg[i] = 0;
	}
	
	void update(int i, llint val)
	{
		i += (1 << size);
		seg[i] += val;
		while(i > 1){
			i /= 2;
			seg[i] = seg[i*2] + seg[i*2+1];
		}
	}

	llint query(int a, int b, int k, int l, int r)
	{
		if(b < l || r < a) return 0;
		if(a <= l && r <= b) return seg[k];
		llint lval = query(a, b, k*2, l, (l+r)/2);
		llint rval = query(a, b, k*2+1, (l+r)/2+1, r);
		return lval + rval;
	}
	llint query(int a, int b)
	{
		return query(a, b, 1, 0, (1<<size)-1);
	}
};

llint n;
llint t[100005], l[100005], r[100005];
vector<llint> comp;
SegTree seg(19);

int main(void)
{
	cin >> n;
	for(int i = 0; i < n; i++){
		cin >> t[i] >> l[i] >> r[i];
	}
	
	for(int i = 0; i < n; i++){
		if(t[i] == 0) comp.push_back(l[i]);
		else comp.push_back(l[i]), comp.push_back(r[i]);
	}
	sort(comp.begin(), comp.end());
	comp.erase(unique(comp.begin(), comp.end()), comp.end());
	
	for(int i = 0; i < n; i++){
		if(t[i] == 0) l[i] = lower_bound(comp.begin(), comp.end(), l[i]) - comp.begin();
		else{
			l[i] = lower_bound(comp.begin(), comp.end(), l[i]) - comp.begin();
			r[i] = lower_bound(comp.begin(), comp.end(), r[i]) - comp.begin();
		}
	}
	
	llint ans = 0;
	for(int i = 0; i < n; i++){
		if(t[i] == 0) seg.update(l[i], r[i]);
		else ans += seg.query(l[i], r[i]);
	}
	cout << ans << endl;
	
	return 0;
}
0