結果

問題 No.789 範囲の合計
ユーザー addeight2addeight2
提出日時 2019-02-10 10:06:11
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 152 ms / 1,000 ms
コード長 1,242 bytes
コンパイル時間 1,594 ms
コンパイル使用メモリ 151,864 KB
実行使用メモリ 7,500 KB
最終ジャッジ日時 2023-09-19 06:45:35
合計ジャッジ時間 4,922 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,592 KB
testcase_01 AC 2 ms
5,600 KB
testcase_02 AC 136 ms
7,168 KB
testcase_03 AC 78 ms
6,332 KB
testcase_04 AC 128 ms
7,184 KB
testcase_05 AC 121 ms
7,120 KB
testcase_06 AC 122 ms
7,044 KB
testcase_07 AC 66 ms
6,648 KB
testcase_08 AC 97 ms
6,528 KB
testcase_09 AC 94 ms
6,468 KB
testcase_10 AC 152 ms
7,500 KB
testcase_11 AC 125 ms
6,524 KB
testcase_12 AC 126 ms
6,524 KB
testcase_13 AC 3 ms
5,660 KB
testcase_14 AC 2 ms
5,744 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
typedef long long ll;
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define REP(i,a) FOR(i,0,a)
	
using namespace std;
const int MAX_N=1e5;
int n,n2;
struct query{
	int typ;
	int a,b;
	query(int t=-1,int a=0,int b=0):typ(t),a(a),b(b){}
};
query qry[MAX_N];
int seg[MAX_N<<3];
void segchange(int x,int y){
	seg[n2-1+x]+=y;
	x=n2-1+x;
	while(x>0){
		x=(x-1)/2;
		seg[x]=seg[x*2+1]+seg[x*2+2];
	}
}
int segquery(int l,int r,int k,int a,int b){
	if(r<=a || b<=l){
		return 0;
	}
	if(l<=a && b<=r){
		return seg[k];
	}
	return segquery(l,r,k*2+1,a,(a+b)/2)+segquery(l,r,k*2+2,(a+b)/2,b);
}
int main(){
	cin>>n;
	vector<int> cmp;
	REP(i,n){
		int t,a,b;
		cin>>t>>a>>b;
		qry[i]=query(t,a,b);
		cmp.push_back(a);
		if(t==1){
			cmp.push_back(b+1);
			qry[i].b++;
		}
	}
	sort(cmp.begin(),cmp.end());
	cmp.erase(unique(cmp.begin(),cmp.end()),cmp.end());
	REP(i,n){
		qry[i].a=lower_bound(cmp.begin(),cmp.end(),qry[i].a)-cmp.begin();
		if(qry[i].typ==1){
			qry[i].b=lower_bound(cmp.begin(),cmp.end(),qry[i].b)-cmp.begin();
		}
	}
	n2=1;
	while(n2<(int)cmp.size())n2<<=1;
	ll ans=0;
	REP(i,n){
		if(qry[i].typ==0){
			segchange(qry[i].a,qry[i].b);
		}else{
			ans+=segquery(qry[i].a,qry[i].b,0,0,n2);
		}
	}
	cout<<ans<<endl;
}
0