作用
在求一串数的Sn - Sm 时,降低时间复杂度O(n)为O(1)
代码
#include<iostream>
using namespace std;
const int N = 100010;
int n, m;
int a[N], s[N];
int main()
{
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
for (int i = 1; i <= n; i++) s[i] = s[i - 1] + a[i];
while (m--)
{
int a, b;
scanf("%d%d", &a, &b);
printf("d\n",s[b] - s[a - 1]);
}
return 0;
}
二维前缀和
公式: Sij = S(i-1)j+Si(j-1)-S(i-1)(j-1)+aij
标签:const,前缀,int,复杂度,d%,Sij
From: https://www.cnblogs.com/LQWUI/p/18048040