POJ 3468 A Simple Problem with Integers 线段树区间更新
2016-11-13 14:57
447 查看
#include<stdio.h> #include<string> #include<cstring> #include<queue> #include<algorithm> #include<functional> #include<vector> #include<iomanip> #include<math.h> #include<iostream> #include<sstream> #include<set> #include<climits> #include<map> #include<bitset> using namespace std; const int MAX=100505; long long F[6*MAX],Mark[6*MAX]= {0},A[MAX]; void build(int x,int left,int right) { if (left==right) { F[x]=A[left]; return; } int mid=(left+right)/2; build(x*2,left,mid); build(x*2+1,mid+1,right); F[x]=F[x*2]+F[x*2+1]; } void change(int x,int left,int right,int L,int R,int num) { if (left>R||right<L) return; if (left>=L&&right<=R) { F[x]+=(right-left+1)*num; Mark[x]+=num; return; } int mid=(left+right)/2; if (Mark[x]) { Mark[x*2]+=Mark[x]; Mark[x*2+1]+=Mark[x]; F[x*2]+=(mid-left+1)*Mark[x]; F[x*2+1]+=(right-mid)*Mark[x]; Mark[x]=0; } if (L<=mid) change(x*2,left,mid,L,R,num); if (R>mid) change(x*2+1,mid+1,right,L,R,num); F[x]=F[x*2]+F[x*2+1]; } long long query(int x,int left,int right,int L,int R) { if (left>R||right<L) return 0; if (left>=L&&right<=R) return F[x]; int mid=(left+right)/2; if (Mark[x]) { Mark[x*2]+=Mark[x]; Mark[x*2+1]+=Mark[x]; F[x*2]+=(mid-left+1)*Mark[x]; F[x*2+1]+=(right-mid)*Mark[x]; Mark[x]=0; } long long Ans=0; if (L<=mid) Ans+=query(x*2,left,mid,L,R); if (R>mid) Ans+=query(x*2+1,mid+1,right,L,R); return Ans; } int main() { int N,Q,a,b,z; char ch[20]; scanf("%d%d",&N,&Q); for (int i=1; i<=N; i++) scanf("%I64d",&A[i]); memset(Mark,0,sizeof(Mark)); build(1,1,N); for (int j=1; j<=Q; j++) { scanf("%s",ch); if (ch[0]=='C') { scanf("%d%d%d",&a,&b,&z); change(1,1,N,a,b,z); } if (ch[0]=='Q') { scanf("%d%d",&a,&b); printf("%I64d\n",query(1,1,N,a,b)); } } return 0; }
相关文章推荐
- POJ 3468 A Simple Problem with Integers(线段树区间更新)
- poj 3468 A Simple Problem with Integers 线段树区间更新
- POJ 3468 A Simple Problem with Integers 线段树区间更新
- [POJ] 3468 A Simple Problem with Integers [线段树区间更新求和]
- poj 3468 A Simple Problem with Integers 【线段树 + 区间更新lazy】
- POJ 3468 A Simple Problem with Integers(线段树区间更新+求和)
- poj 3468 A Simple Problem with Integers(线段树区间更新)
- POJ 3468 A Simple Problem with Integers(线段树,区间更新,区间求和)
- 线段树 poj 3468 A Simple Problem with Integers 区间add更新
- POJ-3468-A Simple Problem with Integers(区间更新,求和)-splay或线段树
- POJ 3468 :A Simple Problem with Integers——区间更新线段树经典题目
- poj 3468 A Simple Problem with Integers(线段树的区间更新与求和)
- poj 3468 A Simple Problem with Integers(线段树区间更新 or 树状数组区间更新)
- (简单) POJ 3468 A Simple Problem with Integers , 线段树+区间更新。
- POJ 3468 A Simple Problem with Integers 【线段树,区间更新】
- POJ 3468 A Simple Problem with Integers(线段树区间更新,模板题,求区间和)
- POJ 3468-A Simple Problem with Integers(线段树_区间更新+lazy标记)
- POJ 3468 A Simple Problem with Integers(线段树 区间更新)
- Poj 3468 A Simple Problem with Integers(线段树 区间更新 延迟标记)
- POJ-3468-A Simple Problem with Integers(线段树区间更新)