树状数组模板
来源:互联网 发布:车铣复合手工编程实例 编辑:程序博客网 时间:2024/06/16 13:16
洛谷 P3374 【模板】树状数组 1
洛谷 P3368 【模板】树状数组 2
洛谷 P3374 【模板】树状数组 1
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<cmath>using namespace std;int n,m;int a[1000000],c[1000000];inline int read(){ int x=0,w=1;char ch=0; while(ch!='-'&&(ch<'0'||ch>'9')) ch=getchar(); if(ch=='-') w=-1,ch=getchar(); while(ch>='0'&&ch<='9') x=(x<<3)+(x<<1)+(ch-48),ch=getchar(); return x*w;}inline void add(int x,int y) { while(x<=n){ c[x]+=y; x+=x&-x; } }inline int getsum(int x){ int ans=0; while(x) { ans+=c[x]; x-=x&-x; } return ans;}int main(){ n=read();m=read(); for(register int i=1;i<=n;++i) a[i]=read(),add(i,a[i]); for(register int i=1;i<=m;++i) { int op=read(),x=read(),y=read(); if(op==1) add(x,y); else if(op==2) printf("%d\n",getsum(y)-getsum(x-1)); } return 0;}
洛谷 P3368 【模板】树状数组 2
#include<iostream>#include<cstdio>#define maxn 500100using namespace std;int n,m,c[maxn],a[maxn];inline int read(){ int x=0,w=1;char ch=0; while(ch!='-'&&(ch<'0'||ch>'9')) ch=getchar(); if(ch=='-') w=-1,ch=getchar(); while(ch>='0'&&ch<='9') x=(x<<3)+(x<<1)+(ch-48),ch=getchar(); return x*w;}inline int add(int x,int k){ for(int i=x;i<=n;i+=i&(-i)) c[i]+=k; }inline int query(int x) { int sum=0; for(int i=x;i>0;i-=i&(-i)) sum+=c[i]; return sum ;}int main(){ n=read();m=read(); for(int i=1;i<=n;i++) a[i]=read(); while(m--){ int k=read(); if(k==1){int x=read(),y=read(),z=read();add(x,z); add(y+1,-z);}//维护差分数组 else{int x;x=read();printf("%d\n",a[x]+query(x)); }//query()求的是改变的值,再加上原来的值就可以了 } return 0;}
阅读全文
0 0
- 树状数组【模板】
- 树状数组模板
- 树状数组 模板
- 树状数组模板
- 树状数组模板
- 树状数组-模板
- 树状数组模板
- 树状数组模板
- 树状数组模板
- 树状数组--模板
- 树状数组模板
- 模板 树状数组
- 模板,树状数组
- 树状数组(模板)
- 树状数组模板
- 树状数组的模板;
- 树状数组 (模板)
- 树状数组BIT 模板
- 个人开篇博客--走出第一步
- C语言合法标识符
- SPFA最短路算法
- TestNG(一):在 Eclipse 中安装 TestNG 插件
- 显示锁(一)--- synchronized 的局限性 与 Lock 的优点
- 树状数组模板
- OpenCV距离变换函数:distanceTransform()介绍
- 作业
- 是
- Node 解决跨域问题 JSONP与CORS
- linux 解压war
- python中文乱码总结
- formatNumberStr
- hdu 4372 Count the Buildings(第一类斯特林数)