POJ 3468 A Simple Problem with Integers 线段树区间更新
来源:互联网 发布:概念式建筑设计知乎 编辑:程序博客网 时间:2024/06/02 18:35
#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;}
0 0
- A Simple Problem with Integers +poj+线段树区间更新
- poj 3468 A Simple Problem with Integers(线段树区间更新 or 树状数组区间更新)
- poj 3468 A Simple Problem with Integers (线段树区间更新 + 树状数组区间更新)
- Poj 3468 A Simple Problem with Integers (线段树 区间更新 区间求和)
- 20140719 「线段树 - 区间更新,区间求和」 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(线段树 区间更新 区间查询)
- 线段树 (更新区间查询区间)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——区间更新线段树经典题目
- 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(线段树区间更新+求和)
- PHP Warning: date(): It is not safe to rely on the system’s timezone settings
- 1401-1500
- TCP、UDP数据包大小的限制
- 51NOD 1632 B君的连通 期望计算 推公式 打表
- DOS命令总结
- POJ 3468 A Simple Problem with Integers 线段树区间更新
- 1501-1600
- Python爬虫实现验证码登录
- CodeForces 527B - Error Correct System 哈希
- windows下的动态库
- 多继承与纯虚函数接口
- CodeForces 527C - Glass Carving set multiset 二分
- ssm框架首次搭建记录
- View的优化