倍增数组+前缀和
#include <iostream>
using namespace std;
const int N = 2e5 + 10;
int n, m, sums[N], l, r;
int main(void) {
cin >> n;
for (int i = 1; i <= n; ++i) cin >> sums[i], sums[i + n] = sums[i];
for (int i = 1; i <= n << 1; ++i) sums[i] += sums[i - 1];
cin >> m;
while (m--) {
cin >> l >> r;
if (l > r) l ^= r, r ^= l, l ^= r;
printf("%d\n", min(sums[r - 1] - sums[l - 1], sums[l + n - 1] - sums[r - 1]));
}
return 0;
}
请讲解,谢谢!