前缀和代码模板

#include
using namespace std;
using ll = long long;
const int N = 1e5 + 9;
ll a[N], prefix[N];

void solve()
{
	ll l, r; cin >> l >> r;
	cout << prefix[r] - prefix[l - 1] << '\n';//两区间范围
}

int main()
{
	ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	ll n; cin >> n;
	for (int i = 1; i <= n; ++i) cin >> a[i];
	for (int i = 1; i <= n; ++i) prefix[i] = prefix[i - 1] + a[i];//公式
	ll m; cin >> m;
	while (m--) solve();

	return 0;
}

你可能感兴趣的:(c++,算法)