POJ 3468 A Simple Problem with Integers 区间和更新,区间和查找
2016-08-08 14:11
531 查看
点击打开链接
#include<algorithm>
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<queue>
#include<map>
#define LL long long
#define bug puts("***********");
#define lson L,mid,ind<<1
#define rson mid+1,R,ind<<1|1
#define lind ind<<1
#define rind ind<<1|1
using namespace std;
const int N=100010;
LL sum[N*4];
LL lazy[N*4];
int n,m;
void pushdown(int ind,int L,int R){
if(lazy[ind]!=0){ ///向下更新,孩子lazy需要更新,孩子的区间和也需要更新
lazy[rind]+=lazy[ind];
lazy[lind]+=lazy[ind];
sum[lind]+=lazy[ind]*((R+L)/2-L+1);
sum[rind]+=lazy[ind]*(R-(R+L)/2);
lazy[ind]=0;
}
}
void pushup(int ind){
sum[ind]=sum[lind]+sum[rind];
}
void build(int L,int R,int ind){
if(L==R){
scanf("%lld",&sum[ind]);
return ;
}
int mid=L+(R-L)/2;
build(lson);
build(rson);
pushup(ind);
}
void update(int sl,int sr,int val,int L,int R,int ind){
if(sl<=L&&sr>=R){
lazy[ind]+=val;
sum[ind]+=val*(R-L+1);
return ;
}
if(sl>R||sr<L) return ;
pushdown(ind,L,R);
int mid=L+(R-L)/2;
update(sl,sr,val,lson);
update(sl,sr,val,rson);
pushup(ind);
}
LL Query(int sl,int sr,int L,int R,int ind){
LL msum=0;
if(sl<=L&&sr>=R){
return sum[ind];
}
if(sl>R||sr<L) return 0;
pushdown(ind,L,R);
// pushup(ind);
int mid=L+(R-L)/2;
msum+=Query(sl,sr,lson);
msum+=Query(sl,sr,rson);
return msum;
}
int u,v,d;
int main(){
memset(sum,0,sizeof(sum));
memset(lazy,0,sizeof(lazy));
scanf("%d%d",&n,&m);
build(1,n,1);
char s[10];
while(m--){
scanf("%s",s);
if(s[0]=='Q'){
scanf("%d%d",&u,&v);
printf("%lld\n",Query(u,v,1,n,1));
}
else{
scanf("%d%d%d",&u,&v,&d);
update(u,v,d,1,n,1);
}
}
return 0;
}
#include<algorithm>
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<queue>
#include<map>
#define LL long long
#define bug puts("***********");
#define lson L,mid,ind<<1
#define rson mid+1,R,ind<<1|1
#define lind ind<<1
#define rind ind<<1|1
using namespace std;
const int N=100010;
LL sum[N*4];
LL lazy[N*4];
int n,m;
void pushdown(int ind,int L,int R){
if(lazy[ind]!=0){ ///向下更新,孩子lazy需要更新,孩子的区间和也需要更新
lazy[rind]+=lazy[ind];
lazy[lind]+=lazy[ind];
sum[lind]+=lazy[ind]*((R+L)/2-L+1);
sum[rind]+=lazy[ind]*(R-(R+L)/2);
lazy[ind]=0;
}
}
void pushup(int ind){
sum[ind]=sum[lind]+sum[rind];
}
void build(int L,int R,int ind){
if(L==R){
scanf("%lld",&sum[ind]);
return ;
}
int mid=L+(R-L)/2;
build(lson);
build(rson);
pushup(ind);
}
void update(int sl,int sr,int val,int L,int R,int ind){
if(sl<=L&&sr>=R){
lazy[ind]+=val;
sum[ind]+=val*(R-L+1);
return ;
}
if(sl>R||sr<L) return ;
pushdown(ind,L,R);
int mid=L+(R-L)/2;
update(sl,sr,val,lson);
update(sl,sr,val,rson);
pushup(ind);
}
LL Query(int sl,int sr,int L,int R,int ind){
LL msum=0;
if(sl<=L&&sr>=R){
return sum[ind];
}
if(sl>R||sr<L) return 0;
pushdown(ind,L,R);
// pushup(ind);
int mid=L+(R-L)/2;
msum+=Query(sl,sr,lson);
msum+=Query(sl,sr,rson);
return msum;
}
int u,v,d;
int main(){
memset(sum,0,sizeof(sum));
memset(lazy,0,sizeof(lazy));
scanf("%d%d",&n,&m);
build(1,n,1);
char s[10];
while(m--){
scanf("%s",s);
if(s[0]=='Q'){
scanf("%d%d",&u,&v);
printf("%lld\n",Query(u,v,1,n,1));
}
else{
scanf("%d%d%d",&u,&v,&d);
update(u,v,d,1,n,1);
}
}
return 0;
}
相关文章推荐
- 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 A Simple Problem with Integers 区间add更新
- 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(线段树区间更新)
- poj 3468 A Simple Problem with Integers 线段树区间更新
- poj - 3468 - A Simple Problem with Integers(线段树(区间更新))
- 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 (伸展树区间更新求和操作 , 模板)
- (Relax 线段树1.1)POJ 3468 A Simple Problem with Integers(线段树子区间更新的维护:集中更新和动态统计子序列中的数据)