結果

問題 No.631 Noelちゃんと電車旅行
ユーザー kotatsugamekotatsugame
提出日時 2020-02-22 18:54:25
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 665 ms / 2,000 ms
コード長 2,491 bytes
コンパイル時間 1,174 ms
コンパイル使用メモリ 88,576 KB
実行使用メモリ 13,212 KB
最終ジャッジ日時 2024-04-10 08:01:24
合計ジャッジ時間 10,413 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 425 ms
6,816 KB
testcase_01 AC 662 ms
13,076 KB
testcase_02 AC 660 ms
13,072 KB
testcase_03 AC 656 ms
13,212 KB
testcase_04 AC 659 ms
12,996 KB
testcase_05 AC 665 ms
12,980 KB
testcase_06 AC 252 ms
7,936 KB
testcase_07 AC 127 ms
8,192 KB
testcase_08 AC 479 ms
12,720 KB
testcase_09 AC 570 ms
8,192 KB
testcase_10 AC 386 ms
12,592 KB
testcase_11 AC 344 ms
8,576 KB
testcase_12 AC 435 ms
12,816 KB
testcase_13 AC 415 ms
6,944 KB
testcase_14 AC 165 ms
6,940 KB
testcase_15 AC 345 ms
6,940 KB
testcase_16 AC 2 ms
6,944 KB
testcase_17 AC 2 ms
6,944 KB
testcase_18 AC 2 ms
6,940 KB
testcase_19 AC 2 ms
6,944 KB
testcase_20 AC 2 ms
6,940 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp:80:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   80 | main()
      | ^~~~

ソースコード

diff #

#include<iostream>
using namespace std;
#include<vector>
#include<functional>
template<typename T>
struct lazysegtree{
	using F=function<T(T,T)>;
	using G=function<T(T,T,int,int)>;
	const F calcfn,lazycalcfn;
	const G updatefn;
	int n;
	T defvalue,lazydefvalue;
	vector<T>dat,lazy;
	vector<bool>lazyflag;
	lazysegtree(int n_=0,T defvalue_=0,
		const F calcfn_=[](T a,T b){return a+b;},
		const F lazycalcfn_=[](T a,T b){return a+b;},
		const G updatefn_=[](T a,T b,int l,int r){return a+b*(r-l);},
		T lazydefvalue_=0
	):defvalue(defvalue_),lazydefvalue(lazydefvalue_),
		calcfn(calcfn_),lazycalcfn(lazycalcfn_),updatefn(updatefn_)
	{
		n=1;
		while(n<n_)n<<=1;
		dat.assign(2*n-1,defvalue);
		lazy.assign(2*n-1,lazydefvalue);
		lazyflag.assign(2*n-1,false);
	}
	void copy(const vector<T>&v)
	{
		for(int i=0;i<v.size();i++)dat[i+n-1]=v[i];
		for(int i=n-2;i>=0;i--)dat[i]=calcfn(dat[2*i+1],dat[2*i+2]);
	}
	void eval(int i,int l,int r)
	{
		if(lazyflag[i])
		{
			dat[i]=updatefn(dat[i],lazy[i],l,r);
			if(r-l>1)
			{
				lazy[2*i+1]=lazycalcfn(lazy[2*i+1],lazy[i]);
				lazy[2*i+2]=lazycalcfn(lazy[2*i+2],lazy[i]);
				lazyflag[2*i+1]=lazyflag[2*i+2]=true;
			}
			lazy[i]=lazydefvalue;
			lazyflag[i]=false;
		}
	}
	void update(int a,int b,T x,int k=0,int l=0,int r=-1)//[a,b)
	{
		if(r<0)r=n;
		eval(k,l,r);
		if(b<=l||r<=a)return;
		else if(a<=l&&r<=b)
		{
			lazy[k]=lazycalcfn(lazy[k],x);
			lazyflag[k]=true;
			eval(k,l,r);
		}
		else
		{
			update(a,b,x,2*k+1,l,(l+r)/2);
			update(a,b,x,2*k+2,(l+r)/2,r);
			dat[k]=calcfn(dat[2*k+1],dat[2*k+2]);
		}
	}
	T query(int a,int b,int k=0,int l=0,int r=-1)//[a,b)
	{
		if(r<0)r=n;
		eval(k,l,r);
		if(b<=l||r<=a)return defvalue;
		else if(a<=l&&r<=b)return dat[k];
		else return calcfn(
			query(a,b,2*k+1,l,(l+r)/2),
			query(a,b,2*k+2,(l+r)/2,r)
		);
	}
};
int N;
main()
{
	cin>>N;
	lazysegtree<pair<long,long> >P(N-1,make_pair(0,0),
		[](pair<long,long>a,pair<long,long>b){
			return make_pair(max(a.first,b.first-a.second),a.second+b.second);
		},
		[](pair<long,long>a,pair<long,long>b){return make_pair(a.first+b.first,0L);},
		[](pair<long,long>a,pair<long,long>b,int l,int r){return make_pair(a.first+b.first,a.second);},
		make_pair(0,0));
	vector<pair<long,long> >init(N-1);
	for(int i=0;i<N-1;i++)
	{
		cin>>init[i].first;
		init[i].second=3;
	}
	P.copy(init);
	int M;cin>>M;
	for(;M--;)
	{
		long l,r,d;cin>>l>>r>>d;
		P.update(l-1,r,make_pair(d,0L));
		pair<long,long>p=P.query(0,N-1);
		cout<<p.first+p.second<<endl;
	}
}
0