結果

問題 No.789 範囲の合計
ユーザー furafura
提出日時 2020-06-12 15:37:51
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 61 ms / 1,000 ms
コード長 1,613 bytes
コンパイル時間 2,567 ms
コンパイル使用メモリ 207,456 KB
実行使用メモリ 5,620 KB
最終ジャッジ日時 2023-09-06 09:15:28
合計ジャッジ時間 4,069 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 58 ms
5,468 KB
testcase_03 AC 37 ms
4,608 KB
testcase_04 AC 55 ms
5,432 KB
testcase_05 AC 47 ms
5,436 KB
testcase_06 AC 50 ms
5,460 KB
testcase_07 AC 31 ms
4,692 KB
testcase_08 AC 43 ms
5,192 KB
testcase_09 AC 41 ms
5,100 KB
testcase_10 AC 61 ms
5,620 KB
testcase_11 AC 50 ms
5,372 KB
testcase_12 AC 52 ms
5,372 KB
testcase_13 AC 1 ms
4,380 KB
testcase_14 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

#define rep(i,n) for(int i=0;i<(n);i++)

using namespace std;
using lint=long long;

template<class G>
class Fenwick_tree{
	vector<G> a;
public:
	Fenwick_tree(){}
	Fenwick_tree(int n){ build(n); }
	Fenwick_tree(const vector<G>& a){ build(a); }
	void build(int n){
		a.assign(n,G{});
	}
	void build(const vector<G>& a){
		this->a=a;
		for(int i=1;i<a.size();i++) (this->a)[i+(i&-i)-1]+=(this->a)[i-1];
	}
	void add(int i,const G& val){
		for(i++;i<=a.size();i+=i&-i) a[i-1]+=val;
	}
	G sum(int l,int r)const{
		if(l==0){
			G res{};
			for(;r>0;r-=r&-r) res+=a[r-1];
			return res;
		}
		return sum(0,r)-sum(0,l);
	}
	int lower_bound(const G& val)const{
		if(val<=G{}) return 0;
		int x=0,k;
		for(k=1;k<=a.size();k<<=1);
		for(k>>=1;k>0;k>>=1) if(x+k<=a.size() && a[x+k-1]<val) val-=a[x+k-1], x+=k;
		return x;
	}
	int upper_bound(const G& val)const{
		if(val<G{}) return 0;
		int x=0,k;
		for(k=1;k<=a.size();k<<=1);
		for(k>>=1;k>0;k>>=1) if(x+k<=a.size() && a[x+k-1]<=val) val-=a[x+k-1], x+=k;
		return x;
	}
};

int main(){
	int q; scanf("%d",&q);
	vector<int> type(q),x(q),y(q);
	rep(i,q) scanf("%d%d%d",&type[i],&x[i],&y[i]);

	auto X=x;
	sort(X.begin(),X.end());
	X.erase(unique(X.begin(),X.end()),X.end());

	lint ans=0;
	Fenwick_tree<lint> F(X.size());
	rep(i,q){
		if(type[i]==0){
			int pos=lower_bound(X.begin(),X.end(),x[i])-X.begin();
			F.add(pos,y[i]);
		}
		else{
			int l=lower_bound(X.begin(),X.end(),x[i])-X.begin();
 			if(l==X.size()) continue;
			int r=upper_bound(X.begin(),X.end(),y[i])-X.begin();
			ans+=F.sum(l,r);
		}
	}
	printf("%lld\n",ans);

	return 0;
}
0