3212: Pku3468 A Simple Problem with Integers
来源:互联网 发布:ios11流畅优化技巧 编辑:程序博客网 时间:2024/06/05 20:17
题目链接
题目大意:线段树区间求和,区间查询
题解:我为什么要看这样的题……
我的收获:手速++
#include <iostream>#include <cstdio>using namespace std;#define M 100005#define ls x<<1#define rs x<<1|1#define lson l,m,ls#define rson m+1,r,rs#define root 1,n,1int n,m,x,y,z,a[M];long long sum[M<<2],add[M<<2];inline void pushup(int x){sum[x]=sum[ls]+sum[rs];}void pushdown(int x,int m){ if(!add[x]) return ; add[ls]+=add[x]; add[rs]+=add[x]; sum[ls]+=add[x]*(m-(m>>1)); sum[rs]+=add[x]*(m>>1); add[x]=0;}void build(int l,int r,int x){ add[x]=0; if(l==r) {sum[x]=a[l];return ;} int m=(l+r)>>1; build(lson),build(rson); pushup(x);}void updata(int L,int R,int p,int l,int r,int x){ if(L<=l&&r<=R) {add[x]+=p,sum[x]+=p*(r-l+1);return ;} pushdown(x,r-l+1); int m=(l+r)>>1; if(L<=m) updata(L,R,p,lson); if(R>m) updata(L,R,p,rson); pushup(x);}long long query(int L,int R,int l,int r,int x){ if(L<=l&&r<=R) return sum[x]; pushdown(x,r-l+1); int m=(l+r)>>1;long long ans=0; if(L<=m) ans+=query(L,R,lson); if(R>m) ans+=query(L,R,rson); return ans;}void work(){ char opt[10]; while(m--) { scanf("%s%d%d",opt,&x,&y); if(opt[0]=='Q') printf("%lld\n",query(x,y,root)); else{scanf("%d",&z),updata(x,y,z,root);} }}void init(){ cin>>n>>m; for(int i=1;i<=n;i++) scanf("%d",&a[i]); build(root);}int main(){ init(); work(); return 0;}
0 0
- 3212: Pku3468 A Simple Problem with Integers
- pku3468: A Simple Problem with Integers
- [BZOJ3212]Pku3468 A Simple Problem with Integers
- [BZOJ3212] Pku3468 A Simple Problem with Integers
- bzoj3212 Pku3468 A Simple Problem with Integers
- BZOJ 3212 Pku3468 A Simple Problem with Integers
- bzoj 3212: Pku3468 A Simple Problem with Integers 线段树
- BZOJ 3212 Pku3468 A Simple Problem with Integers 线段树
- bzoj 3212: Pku3468 A Simple Problem with Integers
- BZOJ P3212 Pku3468 A Simple Problem with Integers
- BZOJ-3212 Pku3468 A Simple Problem with Integers 裸线段树区间维护查询
- bzoj 3212: Pku3468 A Simple Problem with Integers (线段树)
- PKU3468(A Simple Problem with Integers)线段树的成段更新
- A Simple Problem with Integers
- A Simple Problem with Integers
- A Simple Problem with Integers
- A Simple Problem with Integers
- A Simple Problem with Integers
- nodejs中npm深入理解
- smail语法
- UML类图几种关系的总结
- Opencv 中重映射的remap函数中map_x,map_y的困惑说明
- mybatis系列笔记(3)---SqlMapConfig.xml解析
- 3212: Pku3468 A Simple Problem with Integers
- Java 迭代 apriori算法
- input type
- XListView简单使用下拉刷新上拉加载
- Tool-基于gulp的前端自动化构建方案的总结
- 浅谈Java的反射(四) 内省的使用
- Groovy 正则表达式
- 算法设计与应用基础作业第二周
- JavaBug库____tomcat配置解决中文乱码问题