当前位置: 首页 > news >正文

建设银行注册网站名咋设置上海建筑设计院排名

建设银行注册网站名咋设置,上海建筑设计院排名,漳州台商投资建设局网站,西城网站建设浩森宇特文章目录 前缀和一维前缀和公式CODE 二维前缀和公式CODE 差分一维差分思路作用CODE 二维差分思路CODE 前缀和 一维前缀和 板子题:https://www.acwing.com/activity/content/problem/content/829/ 公式 S [ i ] a [ i ] S [ i − 1 ] S[i] a[i] S[i - 1] S[i]…

文章目录

  • 前缀和
    • 一维前缀和
      • 公式
      • CODE
    • 二维前缀和
      • 公式
      • CODE
  • 差分
    • 一维差分
      • 思路
      • 作用
      • CODE
    • 二维差分
      • 思路
      • CODE



前缀和

一维前缀和

板子题:https://www.acwing.com/activity/content/problem/content/829/

公式

S [ i ] = a [ i ] + S [ i − 1 ] S[i] = a[i] + S[i - 1] S[i]=a[i]+S[i1]

CODE

#include <iostream>
#include <cstring>
#include <algorithm>using namespace std;const int N = 1e5 + 10;
int n, m, l, r;
int a[N], s[N];int main()
{cin >> n >> m;for(int i = 1; i <= n; ++i){scanf("%d", &a[i]);s[i] = s[i - 1] + a[i];}while(m--){cin >> l >> r;printf("%d\n", s[r] - s[l - 1]);}
}

二维前缀和

板子题:https://www.acwing.com/activity/content/problem/content/830/

公式

S [ i ] [ j ] = S [ i − 1 ] [ j ] + S [ i ] [ j − 1 ] − S [ i − 1 ] [ j − 1 ] + a [ i ] [ j ] S[i][j] = S[i - 1][j] + S[i][j - 1] - S[i - 1][j - 1] + a[i][j] S[i][j]=S[i1][j]+S[i][j1]S[i1][j1]+a[i][j]

CODE

#include <iostream>
#include <cstring>
#include <algorithm>using namespace std;const int N = 1010;
int n, m, q;
int x1, x2, y1, y2;
int a[N][N], s[N][N];int main()
{cin >> n >> m >> q;for(int i = 1; i <= n; ++i)for(int j = 1; j <= m; ++j){scanf("%d", &a[i][j]);s[i][j] = s[i - 1][j] + s[i][j - 1] - s[i - 1][j - 1] + a[i][j];}while (q -- ){cin >> x1 >> y1 >> x2 >> y2;printf("%d\n", s[x2][y2] - s[x1 - 1][y2] - s[x2][y1 - 1] + s[x1 - 1][y1 - 1]);}
}

差分

一维差分

板子题:https://www.acwing.com/activity/content/problem/content/831/

思路

差分其实是前缀和的逆运算,我们假想有一个数组b[],它的前缀和是数组a[],也就是说:
b [ i ] = a [ i ] − a [ i − 1 ] b[i] = a[i] - a[i - 1] b[i]=a[i]a[i1]

作用

这个b[]数组有什么用呢?
在我们对a[]的元素进行加减操作时,如果采用遍历a[]的方法,时间是 o ( N ) o(N) o(N) 的,但是如果我们用b[]对其优化可以使时间复杂度降到 o ( 1 ) o(1) o(1)

a[] [ i , j ] [i, j] [i,j] 段进行+k操作,我们可以在 b[i] + k并在b[j + 1] - k。当我们对b[]求前缀和时,从i开始的每个元素都会+k,但是我们只要加到a[j]就结束了,所以在a[j + 1]进行归位。

CODE

#include <iostream>
#include <cstring>
#include <algorithm>using namespace std;const int N = 1e5 + 10;
int n, m;
int l, r, c;
int a[N], b[N];void insert(int l, int r, int c){b[l] += c;b[r + 1] -= c;
}int main()
{cin >> n >> m;for(int i = 1; i <= n; ++i){ scanf("%d", &a[i]);insert(i, i, a[i]);}while (m -- ){cin >> l >> r >> c;insert(l, r, c);}for(int i = 1; i <= n; ++i) printf("%d ", b[i] += b[i - 1]);
}

整个差分数组的精髓就在于insert()函数,非常巧妙啊,尤其是在读入阶段对b[]数组进行初始化时的操作,这个操作的意义如下:

解释
来源:https://www.acwing.com/activity/content/code/content/39799/


二维差分

板子题:https://www.acwing.com/activity/content/problem/content/832/

思路

答题思路跟一维差分差不多,借鉴二维前缀和的操作我们可以得到以下公式:
a [ i ] [ j ] = b [ i ] [ j ] − b [ i − 1 ] [ j ] − b [ i ] [ j − 1 ] + b [ i − 1 ] [ j − 1 ] a[i][j] = b[i][j] - b[i - 1][j] - b[i][j - 1] + b[i - 1][j - 1] a[i][j]=b[i][j]b[i1][j]b[i][j1]+b[i1][j1]

那我们插入函数该怎么写呢?
一样的原理:
b [ x 1 ] [ y 1 ] + = c b [ x 2 + 1 ] [ y 1 ] − = c b [ x 1 ] [ y 2 + 1 ] − = c b [ x 2 + 1 ] [ y 2 + 1 ] + = c b[x1][y1] += c\\ b[x2 + 1][y1] -= c\\ b[x1][y2 + 1] -=c\\ b[x2 + 1][y2 + 1] += c b[x1][y1]+=cb[x2+1][y1]=cb[x1][y2+1]=cb[x2+1][y2+1]+=c

CODE

#include <iostream>
#include <cstring>
#include <algorithm>using namespace std;int n, m, q;
const int N = 1010;
int a[N][N], b[N][N];
int x1, y1, x2, y2, c;void insert(int x1, int y1, int x2, int y2, int c){b[x1][y1] += c;b[x2 + 1][y1] -= c;b[x1][y2 + 1] -= c;b[x2 + 1][y2 + 1] += c;
}int main()
{cin >> n >> m >> q;for(int i = 1; i <= n; ++i)for(int j = 1; j <= m; ++j){scanf("%d", &a[i][j]);insert(i, j, i, j, a[i][j]);}while(q--){cin >> x1 >> y1 >> x2 >> y2 >> c;insert(x1, y1, x2, y2, c);}for(int i = 1; i <= n; ++i){for(int j = 1; j <= m; ++j){printf("%d ", b[i][j] += b[i - 1][j] + b[i][j - 1] - b[i - 1][j - 1]);}printf("\n");   }
}
http://www.yayakq.cn/news/510666/

相关文章:

  • 网站已付款方式百度手机卫士下载安装
  • 久久建筑网站内搜索宁波江北区网站推广联系方式
  • 网站建设的公司哪家强东道
  • 崇文企业网站建设公司网络营销和推广做什么
  • 广州海珠区网站建设win2008sr怎么用iis做网站
  • 辽宁建设厅网站什么时候换的企业品牌推广口号
  • 个人网站可以做哪些内容iis下建多个网站
  • 唐山做网站那家好调查问卷在哪个网站做
  • 做网站分期付款比例wordpress的后台链接
  • 未备案网站 赚钱做网站主流用什么语言
  • 青岛网站建设哪个好手机wap网站开发教程
  • 网站代理游戏wordpress写文章出现排版乱
  • 软文写作网站北京专业响应式网站建设
  • 优化国内访问wordpress深圳百度快速排名优化
  • 网站建设栏目设计医院网站模板
  • 重庆水舟科技做网站专注徐州网站建设
  • 传统文化网站建设方案做电影网站 需要进那些群
  • 网站html地图怎么做食品企业网站模板
  • 网站推广的方法及特点济源网站建设济源
  • 广州网站设计公司济南兴田德润o简介图片哪个浏览器任何网站都可以访问
  • 小白建设论坛网站培训机构前端开发
  • 为什么要建设应急管理网站网站时间轴
  • 昆山网站建设详细方案网站建设策划书的心得
  • php网站开发路线室内装修设计图片欣赏
  • asp网站整站下载器wordpress 解压
  • 微信网站平台怎么建立响应式网站模板 金融
  • 站长工具2023最新国产业绩统计网站开发
  • 做拆分盘网站做网站看什么书好
  • 宜昌市高新区建设局网站网上视频教程怎么制作
  • 山西网站开发培训php做一个网站