POJ 3468 A Simple Problem with Integers (区间增减)
2014-08-12 08:18
357 查看
线段树的区间增减,水题,帮助练手
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#define max(a,b) (a>b)?a:b
#define min(a,b) (a>b)?b:a
#define lson l , m , rt << 1
#define rson m + 1 , r , rt << 1 | 1
#define LL __int64
const int maxn = 500100;
using namespace std;
#define MAX INT_MAX
#define MIN INT_MIN
struct node
{
int l,r;
LL add,sum; //add作为一个数的累加和,同时起标记的作用;
}T[300010];
int a[100005]; //add必须是__int64;
void putup(int id)
{
T[id].sum=T[2*id].sum+T[2*id+1].sum;
}
void putdown(int id)
{
if(T[id].add)
{
T[2*id].add+=T[id].add;
T[2*id].sum += (T[2*id].r-T[2*id].l+1)*T[id].add;
T[2*id+1].add+=T[id].add;
T[2*id+1].sum += (T[2*id+1].r-T[2*id+1].l+1)*T[id].add;
T[id].add=0;
}
}
void creat(int l,int r,int id)
{
T[id].l=l;
T[id].r=r;
T[id].add=0;
if(l==r)
{
T[id].sum=a[r];
return;
}
int mid=(l+r)>>1;
creat(l,mid,2*id);
creat(mid+1,r,2*id+1);
putup(id);
}
void update(int from,int to,LL add,int id)
{
if(from<=T[id].l&&to>=T[id].r)
{
T[id].add +=add;
T[id].sum += (T[id].r-T[id].l+1)*add;
return;
}
putdown(id);
if(from<=T[2*id].r)
update(from,to,add,2*id);
if(to>=T[2*id+1].l)
update(from,to,add,2*id+1);
putup(id);
}
LL query(int from,int to,int id)
{
if(from==T[id].l&&to==T[id].r)
return T[id].sum;
putdown(id);
if(from>=T[2*id+1].l)
return query(from,to,2*id+1);
else if(to<=T[2*id].r)
return query(from,to,2*id);
else
return query(from,T[2*id].r,2*id) + query(T[2*id+1].l,to,2*id+1);
}
int main()
{
int n,m,A,B;
LL add;
char str[5];
while(scanf("%d%d",&n,&m)!=EOF)
{
for(int i=1; i<=n; i++)
scanf("%d",&a[i]);
creat(1,n,1);
while(m--)
{
LL ans = 0;
scanf("%s",str);
if(str[0]=='C')
{
scanf("%d%d%I64d",&A,&B,&add);
update(A,B,add,1);
}
else
{
scanf("%d%d",&A,&B);
ans=query(A,B,1);
printf("%I64d\n",ans);
}
}
}
}
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#define max(a,b) (a>b)?a:b
#define min(a,b) (a>b)?b:a
#define lson l , m , rt << 1
#define rson m + 1 , r , rt << 1 | 1
#define LL __int64
const int maxn = 500100;
using namespace std;
#define MAX INT_MAX
#define MIN INT_MIN
struct node
{
int l,r;
LL add,sum; //add作为一个数的累加和,同时起标记的作用;
}T[300010];
int a[100005]; //add必须是__int64;
void putup(int id)
{
T[id].sum=T[2*id].sum+T[2*id+1].sum;
}
void putdown(int id)
{
if(T[id].add)
{
T[2*id].add+=T[id].add;
T[2*id].sum += (T[2*id].r-T[2*id].l+1)*T[id].add;
T[2*id+1].add+=T[id].add;
T[2*id+1].sum += (T[2*id+1].r-T[2*id+1].l+1)*T[id].add;
T[id].add=0;
}
}
void creat(int l,int r,int id)
{
T[id].l=l;
T[id].r=r;
T[id].add=0;
if(l==r)
{
T[id].sum=a[r];
return;
}
int mid=(l+r)>>1;
creat(l,mid,2*id);
creat(mid+1,r,2*id+1);
putup(id);
}
void update(int from,int to,LL add,int id)
{
if(from<=T[id].l&&to>=T[id].r)
{
T[id].add +=add;
T[id].sum += (T[id].r-T[id].l+1)*add;
return;
}
putdown(id);
if(from<=T[2*id].r)
update(from,to,add,2*id);
if(to>=T[2*id+1].l)
update(from,to,add,2*id+1);
putup(id);
}
LL query(int from,int to,int id)
{
if(from==T[id].l&&to==T[id].r)
return T[id].sum;
putdown(id);
if(from>=T[2*id+1].l)
return query(from,to,2*id+1);
else if(to<=T[2*id].r)
return query(from,to,2*id);
else
return query(from,T[2*id].r,2*id) + query(T[2*id+1].l,to,2*id+1);
}
int main()
{
int n,m,A,B;
LL add;
char str[5];
while(scanf("%d%d",&n,&m)!=EOF)
{
for(int i=1; i<=n; i++)
scanf("%d",&a[i]);
creat(1,n,1);
while(m--)
{
LL ans = 0;
scanf("%s",str);
if(str[0]=='C')
{
scanf("%d%d%I64d",&A,&B,&add);
update(A,B,add,1);
}
else
{
scanf("%d%d",&A,&B);
ans=query(A,B,1);
printf("%I64d\n",ans);
}
}
}
}
相关文章推荐
- POJ 3468 A Simple Problem with Integers (线段树 区间增减 区间求和)
- POJ 3468 (线段树 区间增减) A Simple Problem with Integers
- POJ 3468——A Simple Problem with Integers(线段树 成端增减,区间求和)
- 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(线段树区间更新+求和)
- 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 区间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 (线段树 区间更新 区间求和)
- poj 3468 A Simple Problem with Integers 区间更新与询问 Splay简单应用
- POJ-3468 A Simple Problem with Integers Splay Tree区间练习