poj3468区间延迟更新模板题
2013-09-03 18:49
429 查看
#include<stdio.h>
#include<string.h>
#define N 100000
struct st{
int x,y;
__int64 yanchi,sum;
}a[N*4];
__int64 b
;
void build(int t,int x,int y) {
a[t].x=x;
a[t].y=y;
a[t].yanchi=0;
if(x==y) {
a[t].sum=b[x];
return ;
}
int temp=t*2;
int mid=(a[t].x+a[t].y)/2;
build(temp,x,mid);
build(temp+1,mid+1,y);
a[t].sum=a[temp].sum+a[temp+1].sum;
return ;
}
void change(int t,int x,int y,int z) {
if(a[t].x==x&&a[t].y==y) {
a[t].yanchi+=z;
return ;
}
a[t].sum+=(y-x+1)*z;
int temp=t*2;
int mid=(a[t].x+a[t].y)/2;
if(y<=mid)
change(temp,x,y,z);
else
if(x>mid)
change(temp+1,x,y,z);
else {
change(temp,x,mid,z);
change(temp+1,mid+1,y,z);
}
return ;
}
__int64 qury(int t,int x,int y) {
if(a[t].x==x&&a[t].y==y)
return a[t].sum+(y-x+1)*a[t].yanchi;
int temp=t<<1;
int mid=(a[t].y+a[t].x)/2;
a[temp+1].yanchi+=a[t].yanchi;
a[temp].yanchi+=a[t].yanchi;
a[t].sum+=a[t].yanchi*(a[t].y-a[t].x+1);
a[t].yanchi=0;
if(y<=mid)
return qury(temp,x,y);
else
if(x>mid)
return qury(temp+1,x,y);
else
return qury(temp,x,mid)+qury(temp+1,mid+1,y);
}
int main() {
int i,j,k,n,m;
char s[100];
while(scanf("%d%d",&n,&m)!=EOF) {
for(i=1;i<=n;i++)
scanf("%I64d",&b[i]);
build(1,1,n);
while(m--) {
scanf("%s",s);
if(s[0]=='Q') {
scanf("%d%d",&i,&j);
printf("%I64d\n",qury(1,i,j));
}
else {
scanf("%d%d%d",&i,&j,&k);
change(1,i,j,k);
}
}
}
return 0;
}
#include<string.h>
#define N 100000
struct st{
int x,y;
__int64 yanchi,sum;
}a[N*4];
__int64 b
;
void build(int t,int x,int y) {
a[t].x=x;
a[t].y=y;
a[t].yanchi=0;
if(x==y) {
a[t].sum=b[x];
return ;
}
int temp=t*2;
int mid=(a[t].x+a[t].y)/2;
build(temp,x,mid);
build(temp+1,mid+1,y);
a[t].sum=a[temp].sum+a[temp+1].sum;
return ;
}
void change(int t,int x,int y,int z) {
if(a[t].x==x&&a[t].y==y) {
a[t].yanchi+=z;
return ;
}
a[t].sum+=(y-x+1)*z;
int temp=t*2;
int mid=(a[t].x+a[t].y)/2;
if(y<=mid)
change(temp,x,y,z);
else
if(x>mid)
change(temp+1,x,y,z);
else {
change(temp,x,mid,z);
change(temp+1,mid+1,y,z);
}
return ;
}
__int64 qury(int t,int x,int y) {
if(a[t].x==x&&a[t].y==y)
return a[t].sum+(y-x+1)*a[t].yanchi;
int temp=t<<1;
int mid=(a[t].y+a[t].x)/2;
a[temp+1].yanchi+=a[t].yanchi;
a[temp].yanchi+=a[t].yanchi;
a[t].sum+=a[t].yanchi*(a[t].y-a[t].x+1);
a[t].yanchi=0;
if(y<=mid)
return qury(temp,x,y);
else
if(x>mid)
return qury(temp+1,x,y);
else
return qury(temp,x,mid)+qury(temp+1,mid+1,y);
}
int main() {
int i,j,k,n,m;
char s[100];
while(scanf("%d%d",&n,&m)!=EOF) {
for(i=1;i<=n;i++)
scanf("%I64d",&b[i]);
build(1,1,n);
while(m--) {
scanf("%s",s);
if(s[0]=='Q') {
scanf("%d%d",&i,&j);
printf("%I64d\n",qury(1,i,j));
}
else {
scanf("%d%d%d",&i,&j,&k);
change(1,i,j,k);
}
}
}
return 0;
}
相关文章推荐
- poj3468区间延迟更新模板题
- POJ 3468 A Simple Problem with Integers(线段树区间更新模板题)
- POJ 3468 A Simple Problem with Integers (伸展树区间更新求和操作 , 模板)
- 线段树 + 区间更新(区间增加v)模板 ---- poj 3468 - Snarl_jsb
- POJ 3468 - A Simple Problem with Integers(线段树区间更新+模板)
- POJ 3468 线段树区间更新求和模板
- 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
- poj 3468 线段树区间更新(基础,模板)
- 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 线段树区间更新,模板题
- poj 3468 线段树(区间更新)模板题
- POJ - 3468 B - A Simple Problem with Integers 线段树区间更新模板
- 线段树模板 (poj 3468)延迟标记