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

四川省人力资源和社会保障厅关键词优化seo公司

四川省人力资源和社会保障厅,关键词优化seo公司,台州网站建设方案优化,全国最好的网站建设案例算法提高-树状数组 241. 楼兰图腾(区间求和 单点修改)242. 一个简单的整数问题(差分推公式 实现 维护区间修改单点求和)243. 一个简单的整数问题2(区间修改和区间求和)AcWing 244. 谜一样的牛(…

算法提高-树状数组

    • 241. 楼兰图腾(区间求和 + 单点修改)
    • 242. 一个简单的整数问题(差分+推公式 实现 维护区间修改+单点求和)
    • 243. 一个简单的整数问题2(区间修改和区间求和)
    • AcWing 244. 谜一样的牛(用二分查找想要的状态 + 树状数组动态记录当前区间的状态)

树状数组的两个作用

  • 快速求区间和
  • 快速修改某一个数同时可以快速修改区间和
  • tr[i]记录的区间的长度是lowbit(i)
    在这里插入图片描述

241. 楼兰图腾(区间求和 + 单点修改)

#include <cstdio>
#include <cstring>
#include <iostream>
#include <vector>
using namespace std;
typedef pair<int, int> pii;
const int N = 2e5 + 10;
int tr[N];
int a[N];
int n;
int lowbit(int x)
{return x & -x;
}
void add(int x, int c)
{for (int i = x; i <= n; i += lowbit(i)) tr[i] += c;
}int sum(int x)//统计下标为1-x的前缀和
{int res = 0;for (int i = x; i; i -= lowbit(i)) res += tr[i];return res;
}void solve()
{scanf("%d", &n);vector<int> Greater(N+1);//y的取值正好也是1-n,否则就需要离散化了vector<int> lower(N+1);for (int i = 1; i <= n; ++ i) scanf("%d", &a[i]);for (int i = 1; i <= n; ++ i){int y = a[i];Greater[i] = sum(n) - sum(y);//遍历到a[i]的时候,下标为(1, n)在n~y+1之间的数有几个lower[i] = sum(y-1);遍历到a[i]的时候,从1~y-1之间的数有几个add(y, 1);//遍历完a[i]后,将a[i]出现的次数加1,我们前缀和sum}memset(tr, 0, sizeof tr);typedef long long LL;LL res1 = 0, res2 = 0;for (int i = n; i; -- i)//逆向遍历{res1 += Greater[i] *(LL)(sum(n) - sum(a[i]));//记录下标在(n, i)之间有多少个数字大于a[i]res2 += lower[i] * (LL)(sum(a[i]-1));add(a[i], 1);}cout << res1 << " "<< res2;
}
int32_t main()
{ios::sync_with_stdio(0);cin.tie(0);int T = 1;// cin >> T;while (T --) solve();return 0;
}

242. 一个简单的整数问题(差分+推公式 实现 维护区间修改+单点求和)

树状数组擅长的是单点加然后求区间和,本题是区间加然后求单点和;

  • 在原数组上求某个点的值 = 求差分数组的前缀和
  • 在原数组上更改某个区间的值 = 修改差分数组两个端点的值
    为了将我们对原数组的操作转化为经典的树状数组的操作(修改单点的值, 求一个区间的和),我们这里tr[]维护的就是a[]的差分数组。

差分数组b[]和原数组a[]的关系
在这里插入图片描述

在这里插入图片描述
用树状数组实现前缀和 脱裤子放屁多此一举版本
事实上能写出这种代码还是没有理解树状数组的作用是啥,树状数组的作用就是区间求和和单点修改,使得区间求和和单点修改的复杂度都不至于太慢。
数组的区间求和复杂度是n,单点修改复杂度是1
前缀和的区间求和复杂度是1,单点修改的复杂度是n;
树状数组的区间求和和单点修改的复杂度都是logn
本题是区间修改和单点求和,这显然不是树状数组的正常操作,即我们树状数组不能去维护a[],而是应该去维护他的差分数组b[]

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <string.h>
#include <string>
#include <math.h>
#include <vector>
#include <queue>
#include <map>
using namespace std;
typedef pair<int, int> pii;
const int N = 1e5 + 10;
typedef long long LL;
int a[N];
LL tr[N];
int n, m;
// 10 5
// 1 2 3 4 5 6 7 8 9 10
// Q 4
// Q 1
// Q 2
// C 1 6 3
// Q 2
int lowbit(int x)
{return x & -x;
}
void add(int x, int c)
{for (int i = x; i <= n; i += lowbit(i)) tr[i] += c;
}
LL presum(int x)
{LL res = 0;for (int i = x; i; i -= lowbit(i)) res += tr[i];return res ;
}
void solve()
{cin >> n >> m;for (int i = 1; i <= n; ++ i) {cin >> a[i];add(i, a[i]);}char op[2];while (m -- ){cin >> op;int l, r, x;if (*op == 'Q'){cin >> x;cout << presum(x) - presum(x - 1)<< endl;//这不就是前缀和么,虽然查找是1,但是修改就很大了,}                                           //我用树状数组多次一举的话更大else {cin >> l >> r >> x;for(int i = l; i <= r; ++ i)//可以看到前缀和的话修改操作的复杂度是n^2logn{add(i, x);}}}}
int32_t main()
{ios::sync_with_stdio(0);cin.tie(0);int T = 1;// cin >> T;while (T --) solve();return 0;
}

ac代码

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <string.h>
#include <string>
#include <math.h>
#include <vector>
#include <queue>
#include <map>
using namespace std;
typedef pair<int, int> pii;
const int N = 1e5 + 10;
typedef long long LL;
int a[N];
LL tr[N];
int n, m;
// 10 5
// 1 2 3 4 5 6 7 8 9 10
// Q 4
// Q 1
// Q 2
// C 1 6 3
// Q 2
int lowbit(int x)
{return x & -x;
}
void add(int x, int c)
{for (int i = x; i <= n; i += lowbit(i)) tr[i] += c;
}
LL presum(int x)
{LL res = 0;for (int i = x; i; i -= lowbit(i)) res += tr[i];return res ;
}
void solve()
{cin >> n >> m;for (int i = 1; i <= n; ++ i) {cin >> a[i];add(i, a[i] - a[i - 1]);}char op[2];while (m -- ){cin >> op;int l, r, x;if (*op == 'Q'){cin >> x;cout << presum(x) << endl;//这不就是前缀和么,虽然查找是1,但是修改就很大了,}                                           //我用树状数组多次一举的话更大else {cin >> l >> r >> x;add(l, x);add(r + 1, -x);}}}
int32_t main()
{ios::sync_with_stdio(0);cin.tie(0);int T = 1;// cin >> T;while (T --) solve();return 0;
}

243. 一个简单的整数问题2(区间修改和区间求和)

  • 区间求和 + 单点修改 = 普通树状数组(对前缀和以及普通数组的优化)
  • 区间修改 + 单点求和 = 树状数组维护差分数组(这样的话区间修改就可以对应到单点修改差分数组,单点求和就可以对应到区间求差分数组的和)
  • 区间求和 + 区间修改 = 维护两个树状数组,一个用维护差分数组b[i](可以做到区间修改),一个维护i*b[i],推公式可以得出对a[]数组区间求和就是下面这个公式
    在这里插入图片描述
    还不懂的话可以看下面这段解释
    在这里插入图片描述

在这里插入图片描述
因为多维护一个i * b[i], add函数里面的c可能会爆int,所以要强转一下
经验:

  • 如果过了样例数据也过了大部分,那就看一下是不是没开longlong
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <string.h>
#include <string>
#include <math.h>
#include <vector>
#include <queue>
#include <map>
using namespace std;
typedef pair<int, int> pii;
const int N = 1e5 + 10;
typedef long long LL;
int a[N];
LL tr[N], tri[N];
int n, m;int lowbit(int x)
{return x & -x;
}
void add(LL tree[], int x, LL c)//开LL
{for (int i = x; i <= n; i += lowbit(i)) tree[i] += c;
}
LL presum(LL tree[], int x)
{LL res = 0;for (int i = x; i; i -= lowbit(i)) res += tree[i];return res ;
}
LL prefixsum (int x)
{return presum(tr, x) * (x + 1) - presum(tri, x);
}
void solve()
{cin >> n >> m;for (int i = 1; i <= n; ++ i) {cin >> a[i];add(tr, i, a[i] - a[i - 1]);add(tri, i, (LL)i * (a[i] - a[i - 1]));//开LL}char op[2];while (m -- ){cin >> op;int l, r, x;if (*op == 'Q'){cin >> l >> r;cout << prefixsum(r) - prefixsum(l - 1) << endl;}else {cin >> l >> r >> x;add(tr, l, x); add(tr, r + 1, -x);add(tri, l, l * x); add(tri, r + 1, (r + 1) * (-x));}}}
int32_t main()
{ios::sync_with_stdio(0);cin.tie(0);int T = 1;// cin >> T;while (T --) solve();return 0;
}

AcWing 244. 谜一样的牛(用二分查找想要的状态 + 树状数组动态记录当前区间的状态)

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <string.h>
#include <string>
#include <math.h>
#include <vector>
#include <queue>
#include <map>
using namespace std;
typedef pair<int, int> pii;
const int N = 1e5 + 10;
int tr[N], pos[N];
int n;
// 5
// 1
// 2
// 1
// 0
int lowbit(int x)
{return x & -x;
}
void add(int x, int c)
{for (int i = x; i <= n; i += lowbit(i)) tr[i] += c;
}
int presum(int x)//树状数组的本质就是求前缀和
{int sum = 0;for (int i = x; i; i -= lowbit(i)) sum += tr[i];return sum;
}
int find(int k)//
{int l = 1, r = n;while (l < r){int mid = l + r >> 1;if (presum(mid) >= k) r = mid;//求高度为1到n之间高度为mid的牛之前有多少头牛还没有被用过,其实也就是求高度为mid的牛在当前剩下的牛中是否为第k高的牛else l = mid + 1;           //因为我们求的是第一个第k高的牛,那么当前搜索的高度为mid的牛一定是还没有确定位置的牛}return l;
}
void solve()
{cin >> n;add(1, 1);for (int i = 2; i <= n; ++ i){cin >> pos[i];add(i, 1);//单点修改tr[i] = 1,同时对i后面的树状数组造成影响。树状数组的本质就是前缀和,同时单点修改的复杂度也不低不高}vector<int> ans(n + 1);for (int i = n; i; -- i){int height;height = find(pos[i] + 1);//找到当前在剩下的牛中第pos + i高的牛,它的高度是heightans[i] = height;add(height, -1);//表明这个height这个高度的牛已经用过了,他会对1-n中高度 >height的牛的前缀和造成影响}for (int i = 1; i <= n; ++ i) cout << ans[i] << endl;
}
int32_t main()
{ios::sync_with_stdio(0);cin.tie(0);int T = 1;// cin >> T;while (T --) solve();return 0;
}

文章转载自:
http://dinncomrc.ssfq.cn
http://dinncotriangularity.ssfq.cn
http://dinncotoggle.ssfq.cn
http://dinncocircumstantiate.ssfq.cn
http://dinncoderive.ssfq.cn
http://dinncohyaloid.ssfq.cn
http://dinncoearthbags.ssfq.cn
http://dinncomagsman.ssfq.cn
http://dinncohiberarchy.ssfq.cn
http://dinncohantu.ssfq.cn
http://dinncoissueless.ssfq.cn
http://dinncoreflexology.ssfq.cn
http://dinncochrysophyte.ssfq.cn
http://dinncodetachable.ssfq.cn
http://dinncosnubbingly.ssfq.cn
http://dinncoengrossment.ssfq.cn
http://dinncobioelectrical.ssfq.cn
http://dinncojointress.ssfq.cn
http://dinncosaturated.ssfq.cn
http://dinncointegrity.ssfq.cn
http://dinncocivic.ssfq.cn
http://dinnconarky.ssfq.cn
http://dinncopenis.ssfq.cn
http://dinncoofficer.ssfq.cn
http://dinncothunderhead.ssfq.cn
http://dinncogenial.ssfq.cn
http://dinncoaudiogenic.ssfq.cn
http://dinncobyzantine.ssfq.cn
http://dinncotransgressor.ssfq.cn
http://dinncocamisa.ssfq.cn
http://dinncodeadweight.ssfq.cn
http://dinncoaffinity.ssfq.cn
http://dinncomesovarium.ssfq.cn
http://dinncocaviare.ssfq.cn
http://dinncokenosis.ssfq.cn
http://dinncodurability.ssfq.cn
http://dinncoeulogium.ssfq.cn
http://dinncoheresiologist.ssfq.cn
http://dinncopolyene.ssfq.cn
http://dinncoparotid.ssfq.cn
http://dinncofibrocystic.ssfq.cn
http://dinncorylean.ssfq.cn
http://dinncoriddance.ssfq.cn
http://dinncocabble.ssfq.cn
http://dinncoantehall.ssfq.cn
http://dinncokephalin.ssfq.cn
http://dinncoincredibly.ssfq.cn
http://dinncopropaganda.ssfq.cn
http://dinncoundiscoverable.ssfq.cn
http://dinncomerryman.ssfq.cn
http://dinncoreafforest.ssfq.cn
http://dinncoacceptation.ssfq.cn
http://dinncopersia.ssfq.cn
http://dinncooxytetracycline.ssfq.cn
http://dinncopingpong.ssfq.cn
http://dinncooutisland.ssfq.cn
http://dinncowarbler.ssfq.cn
http://dinncooxter.ssfq.cn
http://dinncoredbird.ssfq.cn
http://dinncogeode.ssfq.cn
http://dinncobreugel.ssfq.cn
http://dinncoautogeny.ssfq.cn
http://dinncophaedra.ssfq.cn
http://dinncoharthacanute.ssfq.cn
http://dinncorust.ssfq.cn
http://dinnconamaycush.ssfq.cn
http://dinncobedstand.ssfq.cn
http://dinncominorca.ssfq.cn
http://dinncoreproduce.ssfq.cn
http://dinncodhurrie.ssfq.cn
http://dinncoquins.ssfq.cn
http://dinncogadolinite.ssfq.cn
http://dinncomassawa.ssfq.cn
http://dinncoconversationist.ssfq.cn
http://dinncosurvivance.ssfq.cn
http://dinncounconcernedly.ssfq.cn
http://dinncobookstand.ssfq.cn
http://dinncoexamine.ssfq.cn
http://dinncoimpregnate.ssfq.cn
http://dinncobiconical.ssfq.cn
http://dinncocarbamic.ssfq.cn
http://dinncospanking.ssfq.cn
http://dinncoagglutinogen.ssfq.cn
http://dinncoconsecratory.ssfq.cn
http://dinncoelbowroom.ssfq.cn
http://dinncorazzmatazz.ssfq.cn
http://dinncotemplelike.ssfq.cn
http://dinncokaif.ssfq.cn
http://dinncorufus.ssfq.cn
http://dinncosassolite.ssfq.cn
http://dinncotubalcain.ssfq.cn
http://dinncounpitiful.ssfq.cn
http://dinncositula.ssfq.cn
http://dinncobotchy.ssfq.cn
http://dinncotusker.ssfq.cn
http://dinncogrue.ssfq.cn
http://dinncoubangi.ssfq.cn
http://dinncobiotype.ssfq.cn
http://dinncopridian.ssfq.cn
http://dinncohrip.ssfq.cn
http://www.dinnco.com/news/104134.html

相关文章:

  • 网站后台文本编辑器2024年瘟疫大爆发
  • 樟木头做网站汕头网站优化
  • 网站建设的公司哪家强公司网页制作流程
  • 网站建设 中企动力南昌百度搜索关键词查询
  • 局域网做网站 内网穿透公司网站建设费
  • 百度热线客服24小时seo网站建站
  • 长春直销网站开发小程序开发收费价目表
  • 做网站怎样申请域名怎么在百度上推广产品
  • 学校网站功能产品推广
  • 解析到网站怎样做模板建站
  • 南通网站制作公司哪家好google付费推广
  • 公司微信网站建设方案手机刷网站排名软件
  • 济南制作网站的公司吗重庆可靠的关键词优化研发
  • 在哪里可以兼职windows优化工具
  • 个人网站做多久有效果站长网站
  • 免费网站建设哪家好对网络营销的认识有哪些
  • 店铺的网站怎么做百度热搜广告设计公司
  • 没有备案做盈利性的网站违法吗建站软件可以不通过网络建设吗
  • 做网站如何保证询盘数量产品软文范例100字
  • 网站优化的作业及意义引擎网站推广法
  • 做旅游网站的关注与回复营销案例100例简短
  • wordpress 付费模版seo网络营销推广
  • 江苏做网站台州seo优化公司
  • 百度云主机做网站win10优化大师
  • 加盟编程教育哪家好广州宣布5条优化措施
  • 合肥企业网站建设日本网络ip地址域名
  • 网站开发 javaseo优化排名怎么做
  • 昆明住房和城乡建设部网站网络营销推广的方式
  • 苏州网站建设哪里好qq群引流推广平台免费
  • 地下城做解封任务的网站可以搜索国外网站的搜索引擎