Acwing_795前缀和 【一维前缀和】+Acwing_796子矩阵的和 【二维前缀和】

Acwing_795前缀和 【一维前缀和】

题目:

代码:

cpp 复制代码
#include <bits/stdc++.h>
#define int long long
#define INF 0X3f3f3f3f
#define endl '\n'
using namespace std;
const int N = 100010;
int arr[N];

int n,m;
int l,r;
signed main(){
	
	std::ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	cin>>n>>m;
	int sum[N];
	for(int i = 1; i <= n; i++){
		cin>>arr[i];
		sum[i]= sum[i-1] + arr[i];
	}
	while(m--){
		cin>>l>>r;
		cout<<sum[r]- sum[l-1]<<endl;
	}
	return 0;
}

Acwing_795前缀和 【一维前缀和】

cpp 复制代码
#include <bits/stdc++.h>
#define int long long
#define INF 0X3f3f3f3f
#define endl '\n'
using namespace std;
const int N = 1010;
int arr[N][N];

int n, m, q;
signed main(){
	
	std::ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int sum[N][N];
	cin>>n>>m>>q;
	for (int i = 1; i <= n; i++){
		for (int j = 1; j <= m; j++){
			cin>>arr[i][j];
			sum[i][j] = sum[i-1][j] +sum[i][j-1]-sum[i-1][j-1]+arr[i][j];			
		}
	}
	while(q--){
		int x1, y1, x2, y2;
		cin>>x1>>y1>>x2>>y2;
		cout<<sum[x2][y2] - sum[x1-1][y2]-sum[x2][y1-1]+sum[x1-1][y1-1]<<endl;
	}
	return 0;
}
相关推荐
民乐团扒谱机6 小时前
【超详细】基频音高提取算法之SWIPE 算法深度解析:频谱匹配的精度极限与工程边界
人工智能·算法
饮水桶_T7 小时前
C++ stack & queue复习笔记
开发语言·笔记·算法
wy_hhxx8 小时前
[Leetcode] 滑动窗口
算法·leetcode·滑动窗口
码途行者8 小时前
每天10分钟学Go:第20课 泛型
c++·算法·golang
AIgorithmGEEK10 小时前
算法·哈希表|从“逐个比”到“按 key 算位置”
数据结构·c++·算法·leetcode·hash
风落青芜12 小时前
排序算法(二):选择排序、堆排序与冒泡排序
c语言·开发语言·数据结构·算法·排序算法
Navigator_Z12 小时前
LeetCode //C - 1319. Number of Operations to Make Network Connected
c语言·算法·leetcode
h_a_o777oah13 小时前
【数据结构】FHQ Treap:无旋转的平衡 BST
数据结构·c++·算法·acm·平衡树·treap·fhq treap
草履虫AI课13 小时前
强化学习从零到一 07|重要性采样:借别人的数据,算自己的期望
算法·强化学习