@Yezi.press
构造一维前缀和数组,在 O(1) 时间复杂度内查询区间和。
const int N = 1e5+5; using ll = long long; ll a[N] = {0}; for (int i = 1; i <= n; i++) { cin >> a[i]; a[i] += a[i-1]; // 原地构造前缀和 } // 查询区间 [L, R] 的和 cout << a[R] - a[L-1];