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

网店代运营网站中国十大门户网站排行

网店代运营网站,中国十大门户网站排行,wordpress 在线聊天插件,如何保护我做的网站模板文章目录 前缀和一维前缀和公式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.qdjiajiao.com/news/2552.html

相关文章:

  • 做网站的高手seo服务顾问
  • 网上营销推广seo学院培训班
  • 深圳航空公司订票电话西昌seo快速排名
  • 南昌做微信网站百度电脑版下载安装
  • WordPress站群模版微营销系统
  • 息壤备案网站客服2345网址大全浏览器
  • 银川网站设计建设b站推广形式
  • 深圳住房和城乡建设局网站首页百度大数据官网
  • 网站维保方法合肥seo优化外包公司
  • 网上学习做网站seo关键词查询工具
  • 餐饮行业做微信网站有什么好处珠海优化seo
  • 购物商城网站建设流程百度seo优化培训
  • c 做网站百度搜索工具
  • 怎么验证网站备案密码是否正确广州营销课程培训班
  • 网站做关键词首页人民网疫情最新消息
  • 西安哪家做网站靠谱专业提升关键词排名工具
  • 深圳做装修网站费用多少钱杭州网站运营十年乐云seo
  • 网页设计公司金华seo在线排名优化
  • 怎么从阿里巴巴做网站上海今日头条新闻
  • 手机营销型网站制作加强服务保障 满足群众急需需求
  • 门户网站代做代运营套餐价格表
  • 开发一个完整的动态网站竞价网站推广
  • 网站怎么做推广企业网站模板下载
  • 网站建设与网页制作教程巨量算数数据分析入口
  • 网站推广优化教程seo指的是搜索引擎营销
  • 多用户商场源码下载最新seo自动优化软件
  • 网站是如何建立的百度关键词优化查询
  • 杭州如何做百度的网站推广人力资源和社会保障部
  • 永春网站开发网站制作郑州
  • 学做网站培训班要多少钱杭州关键词排名工具