CodeVS 1080 线段树练习 分块 块状数组
来源:互联网 发布:4g网络哪家有 编辑:程序博客网 时间:2024/06/07 02:03
题意
给定序列a,支持两种操作:
1. 单点修改
2. 区间求和
分析
考虑分块。
我们把连续的
对于单点修改,找到对应块,修改对应点的值和块的和。
对于区间求和,注意分类讨论:
①当
②当
需要统计三种:
还有注意要开long long。
代码
#include <cstdio>#include <cctype>#include <cmath>typedef long long Lint;const int N=100010;const int U=400;int n;int unit;struct Block{ int a[U]; Lint sum;}b[U];int btot;inline int read(void){ int x=0,f=1; char c=getchar(); for (;!isdigit(c);c=getchar()) if (c=='-') f=-1; for (;isdigit(c);c=getchar()) x=x*10+c-'0'; return x*f;}int m;inline void update(int loc,int add){ int lb=(loc-1)/unit+1,lc=loc-(lb-1)*unit; b[lb].a[lc]+=add; b[lb].sum+=add;}Lint query(int l,int r){ int lb=(l-1)/unit+1,lc=l-(lb-1)*unit; int rb=(r-1)/unit+1,rc=r-(rb-1)*unit; Lint sum=0; if (lb==rb) for (int i=lc;i<=rc;i++) sum+=b[lb].a[i]; else { for (int i=lc;i<=unit;i++) sum+=b[lb].a[i]; for (int i=lb+1;i<=rb-1;i++) sum+=b[i].sum; for (int i=1;i<=rc;i++) sum+=b[rb].a[i]; } return sum;}int main(void){ n=read(); unit=(int)sqrt(n); int nowcur=unit; int x; for (int i=1;i<=n;i++) { if (nowcur==unit) btot++,nowcur=0; x=read(); b[btot].sum+=x; b[btot].a[++nowcur]=x; } int k,y,z; m=read(); for (int i=1;i<=m;i++) { k=read(),y=read(),z=read(); if (k==1) update(y,z); else printf("%d\n",query(y,z)); } return 0;}
0 0
- CodeVS 1080 线段树练习 分块 块状数组
- 【CodeVS】1080 线段树练习 分块 线段树 树状数组 开放性
- Codevs 1080 线段树练习(线段树&&树状数组&&分块&&CDQ分治)
- Codevs 4927 线段树练习5(分块)
- Codevs 1081 线段树练习 2(线段树&&树状数组&&分块)
- 形形色色的线段树练习——codevs线段树练习1-5:线段树,树状数组及分块模板
- Codevs 1082 线段树练习 3(线段树&&分块)
- codevs 1080_线段树练习_树状数组
- CODEVS 1080线段树练习
- 1080 线段树练习 codevs
- CODEVS 1080 线段树练习
- [Codevs] 1080 线段树练习
- 【codevs 1080】线段树练习
- 【codevs 1080~1082】线段树练习重做
- [题解] codevs 1080 线段树练习
- 【CodeVS】1191 数轴染色 分块 线段树 树状数组 平衡树 开放性
- <线段树系列1> codevs 1080 线段树练习
- codevs线段树练习3
- 软件测试方法汇总
- VB.net学习
- HDOJ 5505-GT and numbers
- Stanford 机器学习笔记 Week2
- text
- CodeVS 1080 线段树练习 分块 块状数组
- 可以作为技术面试的问题收集
- Win10开发篇(序言)
- mysql安装设置
- tomcat重启、关闭、查看日志shell
- Activity生命周期
- 微信公众号开发接入微信
- Android中实现平铺图片
- Collection集合