結果

問題 No.1099 Range Square Sum
ユーザー 👑 kmjpkmjp
提出日時 2020-06-26 22:32:45
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 352 ms / 2,000 ms
コード長 2,068 bytes
コンパイル時間 2,213 ms
コンパイル使用メモリ 203,836 KB
実行使用メモリ 52,356 KB
最終ジャッジ日時 2023-09-18 05:58:57
合計ジャッジ時間 7,697 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 16 ms
52,164 KB
testcase_01 AC 16 ms
52,100 KB
testcase_02 AC 18 ms
52,188 KB
testcase_03 AC 15 ms
52,168 KB
testcase_04 AC 14 ms
52,208 KB
testcase_05 AC 15 ms
52,208 KB
testcase_06 AC 17 ms
52,140 KB
testcase_07 AC 16 ms
52,112 KB
testcase_08 AC 16 ms
52,272 KB
testcase_09 AC 15 ms
52,212 KB
testcase_10 AC 16 ms
52,112 KB
testcase_11 AC 18 ms
52,140 KB
testcase_12 AC 17 ms
52,164 KB
testcase_13 AC 18 ms
52,196 KB
testcase_14 AC 18 ms
52,356 KB
testcase_15 AC 18 ms
52,104 KB
testcase_16 AC 18 ms
52,264 KB
testcase_17 AC 18 ms
52,176 KB
testcase_18 AC 18 ms
52,120 KB
testcase_19 AC 18 ms
52,108 KB
testcase_20 AC 18 ms
52,112 KB
testcase_21 AC 350 ms
52,188 KB
testcase_22 AC 349 ms
52,272 KB
testcase_23 AC 352 ms
52,176 KB
testcase_24 AC 352 ms
52,108 KB
testcase_25 AC 352 ms
52,096 KB
testcase_26 AC 302 ms
52,104 KB
testcase_27 AC 303 ms
52,120 KB
testcase_28 AC 302 ms
52,144 KB
testcase_29 AC 303 ms
52,144 KB
testcase_30 AC 306 ms
52,208 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef signed long long ll;

#undef _P
#define _P(...) (void)printf(__VA_ARGS__)
#define FOR(x,to) for(x=0;x<(to);x++)
#define FORR(x,arr) for(auto& x:arr)
#define ITR(x,c) for(__typeof(c.begin()) x=c.begin();x!=c.end();x++)
#define ALL(a) (a.begin()),(a.end())
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
//-------------------------------------------------------

template<class V,int NV> class SegTree_1 {
public:
	vector<V> add;
	vector<V> sum1;
	vector<V> sum2;
	
	SegTree_1(){
		add.resize(NV*2);
		sum1.resize(NV*2);
		sum2.resize(NV*2);
	};
	
	V getval(int x,int y,int l,int r,int k) {
		if(r<=x || y<=l) return 0;
		if(x<=l && r<=y) return sum2[k];
		if(add[k]) {
			update(l,(l+r)/2,l,(l+r)/2,k*2,add[k]);
			update((l+r)/2,r,(l+r)/2,r,k*2+1,add[k]);
			add[k]=0;
			sum1[k]=sum1[2*k]+sum1[2*k+1];
			sum2[k]=sum2[2*k]+sum2[2*k+1];
		}
		
		return getval(x,y,l,(l+r)/2,k*2)+getval(x,y,(l+r)/2,r,k*2+1);
	}
	V getval(int x,int y) { return getval(x,y,0,NV,1);}
	
	void update(int x,int y,int l,int r,int k,int v) {
		if(r<=x || y<=l) return;
		if(v==0) return;
		if(x<=l && r<=y) {
			add[k]+=v;
			sum2[k]+=2LL*v*sum1[k]+1LL*(r-l)*v*v;
			sum1[k]+=1LL*v*(r-l);
			return;
		}
		
		update(l,(l+r),l,(l+r)/2,k*2,add[k]);
		update((l+r)/2,r,(l+r)/2,r,k*2+1,add[k]);
		update(x,y,l,(l+r)/2,k*2,v);
		update(x,y,(l+r)/2,r,k*2+1,v);
		add[k]=0;
		sum1[k]=sum1[2*k]+sum1[2*k+1];
		sum2[k]=sum2[2*k]+sum2[2*k+1];
	}
	void update(int x,int y,int v) { update(x,y,0,NV,1,v);}
};
SegTree_1<ll,1<<20> st;

int N;
int Q;
void solve() {
	int i,j,k,l,r,x,y; string s;
	
	cin>>N;
	FOR(i,N) {
		cin>>x;
		st.update(i+1,i+2,x);
	}
	cin>>Q;
	while(Q--) {
		cin>>i>>l>>r;
		if(i==1) {
			cin>>x;
			st.update(l,r+1,x);
		}
		else {
			cout<<st.getval(l,r+1)<<endl;
		}
	}
}


int main(int argc,char** argv){
	string s;int i;
	if(argc==1) ios::sync_with_stdio(false), cin.tie(0);
	FOR(i,argc-1) s+=argv[i+1],s+='\n'; FOR(i,s.size()) ungetc(s[s.size()-1-i],stdin);
	cout.tie(0); solve(); return 0;
}
0