前缀和思维导图:
一维前缀和算法模版:
1 #include <iostream> 2 3 using namespace std; 4 5 const int N = 100010; 6 7 int n, m; 8 int s[N]; 9 10 int main() 11 { 12 scanf("%d%d", &n, &m); 13 for (int i = 1; i <= n; i++) 14 { 15 int x; 16 scanf("%d", &x); 17 s[i] = s[i - 1] + x; // 前缀和的初始化 18 } 19 20 while (m--) 21 { 22 int l, r; 23 scanf("%d%d", &l, &r); 24 printf("%d\n", s[r] - s[l - 1]); // 区间和的计算 25 } 26 27 return 0; 28 }View Code
二维前缀和算法模版:
1 #include <iostream> 2 3 using namespace std; 4 5 const int N = 1010; 6 7 int n, m, q, x; 8 int s[N][N]; 9 10 int main() 11 { 12 scanf("%d%d%d", &n, &m, &q); 13 for (int i = 1; i <= n; i++) 14 for (int j = 1; j <= m; j++) 15 { 16 scanf("%d", &x); 17 s[i][j] = s[i - 1][j] + s[i][j - 1] - s[i - 1][j - 1] + x; 18 } 19 20 while (q--) 21 { 22 int x1, y1, x2, y2; 23 scanf("%d%d%d%d", &x1, &y1, &x2, &y2); 24 printf("%d\n", s[x2][y2] - s[x1 - 1][y2] - s[x2][y1 - 1] + s[x1 - 1][y1 - 1]); 25 } 26 27 return 0; 28 }View Code
标签:总结,const,前缀,int,d%,算法,include From: https://www.cnblogs.com/ykycode/p/17860249.html