1. 程式人生 > 其它 >Basic Level 1070 結繩 (25分)

Basic Level 1070 結繩 (25分)

C.等排隊

#include<iostream>
#include<cstdio>
using namespace std;
typedef long long LL;
const LL N=1e5+10, M=1e9+7;
LL pre_sum(int l, int r, LL x[N]);

int n, q;
LL t[N], pre[N];
LL res;

int main()
{
	cin>>n;
	
	for(int i=1;i<=n;i++)
	{
		scanf("%lld", &t[i]);
		t[i]+=t[i-1];//在原佇列情況下,t[i]是第i個人的打水時間. 
	}
	
	for(int i=1;i<=n;i++) pre[i]=pre[i-1]+t[i];
	
	cin>>q;
	while(q--)//之前的人不排了,還要減去他們的時間。 
	{
		int l, r;
		scanf("%d%d", &l, &r);
		
		LL sum=(pre_sum(l, r, pre)-(r-l+1)*t[l-1])%M;
		
		res=(res+sum)%M;
	}
	
	cout<<res<<endl;
	
	return 0;
}

LL pre_sum(int l, int r, LL x[N])
{
	return x[r]-x[l-1];
}