結果

問題 No.728 ギブ and テイク
ユーザー kotatsugame
提出日時 2020-04-15 13:38:20
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 443 ms / 3,000 ms
コード長 936 bytes
コンパイル時間 746 ms
コンパイル使用メモリ 80,152 KB
実行使用メモリ 9,996 KB
最終ジャッジ日時 2024-10-01 18:43:35
合計ジャッジ時間 7,586 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 30
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp:36:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   36 | main()
      | ^~~~

ソースコード

diff #

#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
//1-indexed
#include<vector>
template<typename T>
struct BIT{
	int n;
	vector<T>bit;
	BIT(int n_=0):n(n_),bit(n_+1){}
	T sum(int i)
	{
		T ans=0;
		for(;i>0;i-=i&-i)ans+=bit[i];
		return ans;
	}
	void add(int i,T a)
	{
		if(i==0)return;
		for(;i<=n;i+=i&-i)bit[i]+=a;
	}
	int lower_bound(T k)//k<=sum(ret)
	{
		if(k<=0)return 0;
		int ret=0,i=1;
		while((i<<1)<=n)i<<=1;
		for(;i;i>>=1)
			if(ret+i<=n&&bit[ret+i]<k)k-=bit[ret+=i];
		return ret+1;
	}
};
int N;
int A[3<<17];
long ans;
main()
{
	cin>>N;
	for(int i=0;i<N;i++)cin>>A[i];
	BIT<int>P(N);
	priority_queue<pair<int,int> >Q;
	for(int i=0;i<N;i++)
	{
		while(!Q.empty()&&-Q.top().first<A[i])
		{
			P.add(Q.top().second,-1);
			Q.pop();
		}
		int L,R;cin>>L>>R;
		int il=lower_bound(A,A+N,A[i]-L)-A;
		ans+=P.sum(i+1)-P.sum(il);
		P.add(i+1,1);
		Q.push(make_pair(-A[i]-R,i+1));
	}
	cout<<ans<<endl;
}
0