线段树模板
2014-07-21 08:03
316 查看
#include<stdio.h>
#include<iostream>
int max(int x,int y)
{
return x>y?x:y;
}
int a[300000];
struct ele
{
int Left;
int Right;
int Max;
int Sum;
}p[700000];
void Build(int L,int R,int Step)
{
p[Step].Left=L;
p[Step].Right=R;
if(L==R)
p[Step].Sum=p[Step].Max=a[L];
else
{
int Mid=(L+R)/2;
Build(L,Mid,2*Step);
Build(Mid+1,R,2*Step+1);
p[Step].Max=max(p[Step*2].Max,p[Step*2+1].Max);
p[Step].Sum=p[Step*2].Sum+p[Step*2+1].Sum;
}
}
void Update(int x,int y,int Step)
{
if(p[Step].Left==x&&p[Step].Right==x)
p[Step].Max=p[Step].Sum=y;
else
{
int Mid=(p[Step].Left+p[Step].Right)/2;
if(x<=Mid)
Update(x,y,2*Step);
else
Update(x,y,2*Step+1);
p[Step].Max=max(p[Step*2].Max,p[Step*2+1].Max);
p[Step].Sum=p[Step*2].Sum+p[Step*2+1].Sum;
}
}
int Max(int x,int y,int Step)
{
if(p[Step].Left==x&&p[Step].Right==y)
return p[Step].Max;
else
{
int Mid=(p[Step].Left+p[Step].Right)/2;
if(y<=Mid)
return Max(x,y,2*Step);
else
if(x>=Mid+1)
return Max(x,y,2*Step+1);
else
return max(Max(x,Mid,2*Step),Max(Mid+1,y,2*Step+1));
}
}
int Sum(int x,int y,int Step)
{
if(p[Step].Left==x&&p[Step].Right==y)
return p[Step].Sum;
else
{
int Mid=(p[Step].Left+p[Step].Right)/2;
if(y<=Mid)
return Sum(x,y,2*Step);
else
if(x>=Mid+1)
return Sum(x,y,2*Step+1);
else
return Sum(x,Mid,2*Step)+Sum(Mid+1,y,2*Step+1);
}
}
int main()
{
int n,m;
int i,j,k;
while(scanf("%d%d",&n,&m)!=EOF)
{
for(i=1;i<=n;i++)
scanf("%d",&a[i]);
Build(1,n,1);
while(m--)
{
scanf("%d%d%d",&i,&j,&k);
if(i==1)
Update(j,k,1);
if(i==2)
printf("%d\n",Sum(j,k,1));
if(i==3)
printf("%d\n",Max(j,k,1));
}
}
return 0;
}
#include<iostream>
int max(int x,int y)
{
return x>y?x:y;
}
int a[300000];
struct ele
{
int Left;
int Right;
int Max;
int Sum;
}p[700000];
void Build(int L,int R,int Step)
{
p[Step].Left=L;
p[Step].Right=R;
if(L==R)
p[Step].Sum=p[Step].Max=a[L];
else
{
int Mid=(L+R)/2;
Build(L,Mid,2*Step);
Build(Mid+1,R,2*Step+1);
p[Step].Max=max(p[Step*2].Max,p[Step*2+1].Max);
p[Step].Sum=p[Step*2].Sum+p[Step*2+1].Sum;
}
}
void Update(int x,int y,int Step)
{
if(p[Step].Left==x&&p[Step].Right==x)
p[Step].Max=p[Step].Sum=y;
else
{
int Mid=(p[Step].Left+p[Step].Right)/2;
if(x<=Mid)
Update(x,y,2*Step);
else
Update(x,y,2*Step+1);
p[Step].Max=max(p[Step*2].Max,p[Step*2+1].Max);
p[Step].Sum=p[Step*2].Sum+p[Step*2+1].Sum;
}
}
int Max(int x,int y,int Step)
{
if(p[Step].Left==x&&p[Step].Right==y)
return p[Step].Max;
else
{
int Mid=(p[Step].Left+p[Step].Right)/2;
if(y<=Mid)
return Max(x,y,2*Step);
else
if(x>=Mid+1)
return Max(x,y,2*Step+1);
else
return max(Max(x,Mid,2*Step),Max(Mid+1,y,2*Step+1));
}
}
int Sum(int x,int y,int Step)
{
if(p[Step].Left==x&&p[Step].Right==y)
return p[Step].Sum;
else
{
int Mid=(p[Step].Left+p[Step].Right)/2;
if(y<=Mid)
return Sum(x,y,2*Step);
else
if(x>=Mid+1)
return Sum(x,y,2*Step+1);
else
return Sum(x,Mid,2*Step)+Sum(Mid+1,y,2*Step+1);
}
}
int main()
{
int n,m;
int i,j,k;
while(scanf("%d%d",&n,&m)!=EOF)
{
for(i=1;i<=n;i++)
scanf("%d",&a[i]);
Build(1,n,1);
while(m--)
{
scanf("%d%d%d",&i,&j,&k);
if(i==1)
Update(j,k,1);
if(i==2)
printf("%d\n",Sum(j,k,1));
if(i==3)
printf("%d\n",Max(j,k,1));
}
}
return 0;
}
相关文章推荐
- HDU 3308 线段树 区间合并模板题
- 【洛谷1592】【模板】template 线段树 线段树裸题
- 线段树模板
- HDU 1166-敌兵布阵【树状数组&&线段树单点更新】【模板】
- 线段树区间修改模板
- poj 3237 树链剖分模板(用到线段树lazy操作)
- 模板:线段树
- 可持久化线段树 (模板)
- 线段树简单入门模板--单点更新
- 【luogu3372】模板 线段树1
- 线段树模板
- 线段树模板
- hdu1754—I Hate It(线段树模板)
- 比较快也比较漂亮的熟练剖分模板(BZOJ 1036:树剖+线段树 BZOJ 1103:树剖+树状数组)
- 线段树模板
- 洛谷 P3372 【模板】线段树 1
- 【模板】线段树单点修改
- 【常用模板】 线段树区间操作
- 【HDU 4819】Mosaic 二维线段树模板
- 线段树模板