线段树模板 hdu1166
2014-09-17 12:45
239 查看
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<iostream>
#include<algorithm>
using namespace std;
#define maxn 1500000
struct ele
{
int Left;
int Right;
int Sum;
}p[maxn];
int a[maxn];
void build(int L,int R,int step)
{
p[step].Left=L;
p[step].Right=R;
if(p[step].Left==p[step].Right)
p[step].Sum=a[L];
else
{
int Mid=(p[step].Left+p[step].Right)/2;
build(L,Mid,2*step);
build(Mid+1,R,2*step+1);
p[step].Sum=p[2*step].Sum+p[2*step+1].Sum;
}
}
void update(int x,int y,int step)
{
if(p[step].Left==p[step].Right)
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].Sum=p[step*2].Sum+p[step*2+1].Sum;
}
}
int Sum(int L,int R,int step)
{
if(p[step].Left==L&&p[step].Right==R)
return p[step].Sum;
else
{
int Mid=(p[step].Left+p[step].Right)/2;
if(R<=Mid)
return Sum(L,R,2*step);
else
if(L>=Mid+1)
return Sum(L,R,2*step+1);
else
return Sum(L,Mid,2*step)+Sum(Mid+1,R,2*step+1);
}
}
int T;
int n;
char str[55];
int main()
{
int i;
int cnt=1;
int x,y;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
for(i=1;i<=n;i++)
scanf("%d",&a[i]);
build(1,n,1);
printf("Case %d:\n",cnt++);
while(1)
{
scanf("%s",str);
if(str[0]=='E')
break;
scanf("%d%d",&x,&y);
if(str[0]=='Q')
printf("%d\n",Sum(x,y,1));
if(str[0]=='S')
update(x,-y,1);
if(str[0]=='A')
update(x,y,1);
}
}
return 0;
}
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<iostream>
#include<algorithm>
using namespace std;
#define maxn 1500000
struct ele
{
int Left;
int Right;
int Sum;
}p[maxn];
int a[maxn];
void build(int L,int R,int step)
{
p[step].Left=L;
p[step].Right=R;
if(p[step].Left==p[step].Right)
p[step].Sum=a[L];
else
{
int Mid=(p[step].Left+p[step].Right)/2;
build(L,Mid,2*step);
build(Mid+1,R,2*step+1);
p[step].Sum=p[2*step].Sum+p[2*step+1].Sum;
}
}
void update(int x,int y,int step)
{
if(p[step].Left==p[step].Right)
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].Sum=p[step*2].Sum+p[step*2+1].Sum;
}
}
int Sum(int L,int R,int step)
{
if(p[step].Left==L&&p[step].Right==R)
return p[step].Sum;
else
{
int Mid=(p[step].Left+p[step].Right)/2;
if(R<=Mid)
return Sum(L,R,2*step);
else
if(L>=Mid+1)
return Sum(L,R,2*step+1);
else
return Sum(L,Mid,2*step)+Sum(Mid+1,R,2*step+1);
}
}
int T;
int n;
char str[55];
int main()
{
int i;
int cnt=1;
int x,y;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
for(i=1;i<=n;i++)
scanf("%d",&a[i]);
build(1,n,1);
printf("Case %d:\n",cnt++);
while(1)
{
scanf("%s",str);
if(str[0]=='E')
break;
scanf("%d%d",&x,&y);
if(str[0]=='Q')
printf("%d\n",Sum(x,y,1));
if(str[0]=='S')
update(x,-y,1);
if(str[0]=='A')
update(x,y,1);
}
}
return 0;
}
相关文章推荐
- hdu1166(线段树模板题)
- hdu1166 敌兵布阵+线段树模板题+单点修改
- 线段树基本操作模板 hdu1166 hdu1754
- Hdu1166-- 线段树模板
- hdu1166 敌兵布阵(线段树点更新模板题)
- hdu1166线段树的点跟新模板
- hdu1166(线段树单点更新&区间求和模板)
- 线段树的两道模板题 hdu1698 hdu1166
- hdu1166 敌兵布阵 (线段树模板)
- hdu1166(线段树模板)
- [hdu1166]线段树模板
- 【线段树模板!!!】hdu-1394 顺便学习一下not only succes 的线段树风格
- 线段树模板(学长给的,自己消化了一下,其实基本上差不多(好吧,一模一样QAQ))
- Just a Hook HDU - 1698 (线段树模板题)
- 洛谷P3374模板线段树1
- HDU1823 Luck and Love(二维线段树单点更新+区间查询+模板)
- 【模板】线段树区间修改、区间求和、查询最值
- 树状数组模板 HDU1166
- hdu1166 敌兵布阵,线段树
- 线段树模板