首页 > 其他分享 >前缀和和差分

前缀和和差分

时间:2023-03-30 19:44:23浏览次数:38  
标签:前缀 int cout long 差分 include define

前缀和和差分

前缀和

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <vector>
#include <cstring>
#include <unordered_set>
#include <set>
#include <stack>
#include <map>
#include <cmath>
#include <sstream>
#include <queue>
#define int long long
#define yes cout<<"YES"<<'\n'
#define no 	cout<<"NO"<<'\n'

using namespace std;
const int N = 100008;
int s[N];
int a[N];

signed main () {
	int n,m;
	scanf("%lld %lld",&n,&m);
	for(int i=1;i<=n;i++){
		scanf("%lld",&a[i]);
		s[i]=s[i-1]+a[i];
	}
	while(m--){
		int l,r;
		scanf("%lld %lld",&l,&r);
		printf("%lld\n",s[r]-s[l-1]);
	}
	


	return 0;
}

子矩阵的和

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <vector>
#include <cstring>
#include <unordered_set>
#include <set>
#include <stack>
#include <map>
#include <cmath>
#include <sstream>
#include <queue>
#define int long long
#define yes cout<<"YES"<<'\n'
#define no 	cout<<"NO"<<'\n'

using namespace std;
const int N = 1004;
int s[N][N];
int a[N][N];

signed main () {
	int n, m, q;
	scanf("%lld %lld %lld", &n, &m, &q);
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= m; j++) {
			scanf("%lld", &a[i][j]);
			s[i][j] = s[i - 1][j] + s[i][j - 1] - s[i - 1][j - 1] + a[i][j];
		}
	}
	while (q--) {
		int x1, y1, x2, y2;
		scanf("%lld %lld %lld %lld", &x1, &y1, &x2, &y2);
		printf("%lld\n", s[x2][y2] - s[x2][y1 - 1] - s[x1 - 1][y2] + s[x1 - 1][y1 - 1]);
	}

	return 0;
}

差分

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <vector>
#include <cstring>
#include <unordered_set>
#include <set>
#include <stack>
#include <map>
#include <cmath>
#include <sstream>
#include <queue>
#define int long long
#define yes cout<<"YES"<<'\n'
#define no 	cout<<"NO"<<'\n'

using namespace std;
const int N = 1004;
int b[N];
int a[N];
void insert(int l, int r, int x) {
	b[l] = b[l] + x;
	b[r + 1] = b[r + 1] - x;
}

signed main () {
	int n, m;
	scanf("%lld %lld", &n, &m);
	for (int i = 1; i <= n; i++) {
		scanf("%lld", &a[i]);
		insert(i, i, a[i]);
	}
	int l, r, x;
	while (m--) {
		scanf("%lld %lld %lld", &l, &r, &x);
		insert(l, r, x);
	}
	for (int i = 1; i <= n; i++) {
		b[i] = b[i - 1] + b[i];
	}
	for (int i = 1; i <= n; i++) {
		printf("%lld ", b[i]);
	}



	return 0;
}

差分矩阵

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <vector>
#include <cstring>
#include <unordered_set>
#include <set>
#include <stack>
#include <map>
#include <cmath>
#include <sstream>
#include <queue>
#define int long long
#define yes cout<<"YES"<<'\n'
#define no 	cout<<"NO"<<'\n'

using namespace std;
const int N = 1004;
int b[N][N];
int a[N][N];
void insert(int x1, int y1, int x2, int y2, int x) {
	b[x1][y1] += x;
	b[x2 + 1][y1] -= x;
	b[x1][y2 + 1] -= x;
	b[x2 + 1][y2 + 1] += x;


}

signed main () {
	int n, m, q;
	scanf("%lld %lld %lld", &n, &m, &q);
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= m; j++) {
			scanf("%lld", &a[i][j]);
			insert(i, j, i, j, a[i][j]);
		}
	}
	int x1, y1, x2, y2, x;
	while (q--) {
		scanf("%lld %lld %lld %lld %lld", &x1, &y1, &x2, &y2, &x);
		insert(x1, y1, x2, y2, x);
	}
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= m; j++) {
			b[i][j] = b[i - 1][j] + b[i][j - 1] - b[i - 1][j - 1] + b[i][j];
			//	s[i][j]=s[i-1][j]+s[i][j-1]-s[i-1][j-1]+a[i][j];
		}
	}
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= m; j++) {
			printf("%lld ", b[i][j]);
//				b[i][j]=b[i-1][j]+b[i][j-1]-b[i-1][j-1]+b[i][j];
			//	s[i][j]=s[i-1][j]+s[i][j-1]-s[i-1][j-1]+a[i][j];
		}
		printf("\n");
	}





	return 0;
}

标签:前缀,int,cout,long,差分,include,define
From: https://www.cnblogs.com/harper886/p/17274104.html

相关文章

  • 双因素方差分析流程
    双因素方差分析流程一、案例分析当前收集了39名志愿者减重效果的相关数据,他们的生活方式可分为3种,现在研究人员想要研究生活方式和性别对于减重的影响,想要知道不同的生......
  • LeetCode 周赛 338,贪心 / 埃氏筛 / 欧氏线性筛 / 前缀和 / 二分查找 / 拓扑排序
    本文已收录到AndroidFamily,技术和职场问题,请关注公众号[彭旭锐]提问。大家好,我是小彭。上周末是LeetCode第338场周赛,你参加了吗?这场周赛覆盖的知识点很多,第四题......
  • 差分矩阵 | 差分
         已知有原数组a,现欲建立差分数组b建立差分数组的两种方式:(i)根据原数组建立。b[i]=a[i]-a[i-1](ii)在空数组上白手起家:  1#include<iostream>......
  • 如何自定义 elementui 的前缀
    1、安装插件:postcss-change-css-prefix2、在根目录下创建postcss.config.js文件,并写入如下内容:constaddCssPrefix=require('postcss-change-css-prefix')module.e......
  • MySQL过程式编程,case when嵌套,差分(自联结完成),PERIOD_DIFF求月份差
    题目地址https://www.nowcoder.com/practice/aef5adcef574468c82659e8911bb297f代码#还是过程式编程吧,否则万一签到奖励规则变了,SQL代码你根本不知道怎么改#Keepin......
  • 解决WP表前缀更换后出现的You do not have sufficient permission
    将安装的wordpress表前缀由默认的wp_修改为其它了,再次登陆后台后出现Youdonothavesufficientpermissionstoaccessthispage.网上搜索了一下,说是修改检查wp_userme......
  • 前缀和算法
    前缀和算法什么是前缀和?前缀和是指某序列的前n项和,可以把它理解为数学上的数列的前n项和,而拆分可以看成前缀和的逆运算。合理的使用前缀和与拆分,可以将某些复杂的问题简......
  • 算法学习1 前缀和与差分
    一前缀和是什么? 顾名思义,就是数组里面,以原数组的和作为另一个数组元素的数组。二有何益裨?求数组某个元素内,某一块区域内数据的和,并将他们的时间复杂度由O(n)降低到O(......
  • k倍区间 | 前缀和
    k倍区间-蓝桥云课(lanqiao.cn)  1#include<iostream>2usingnamespacestd;3#defineios_base\4ios::sync_with_stdio(false);\5cin.tie(......
  • css针对各个浏览器的前缀是什么
    css针对各个浏览器的前缀是什么:现在写css3代码的时候,为了实现兼容性,需要在前面加前缀以便兼容对应的浏览器。下面就列举一下前缀的写法:-webkit//Webkit内核,例如谷歌......