两道水题,都抄的模板QAQ
HDU 1166 敌兵布阵
#include<iostream>
#include<cstdio>
using namespace std;
#define maxn 50005
int tree[maxn<<2];
void build(int left,int right,int root)
{
if(left==right)
{
scanf("%d",&tree[root]);
return ;
}
int mid=(left+right)>>1;
build(left,mid,root<<1);
build(mid+1,right,root<<1|1);
tree[root]=tree[root<<1]+tree[root<<1|1];
}
int query(int x,int y,int left,int right,int root)
{
if(x<=left&&right<=y)
{
return tree[root];
}
int sum=0,mid=(left+right)>>1;
if(x<=mid)sum=sum+query(x,y,left,mid,root<<1);
if(y>mid)sum=sum+query(x,y,mid+1,right,root<<1|1);
return sum;
}
void update(int p,int add,int left,int right,int root)
{
if(left==right)
{
tree[root]+=add;
return ;
}
int mid=(left+right)>>1;
if(p<=mid)update(p,add,left,mid,root<<1);
else update(p,add,mid+1,right,root<<1|1);
tree[root]=tree[root<<1]+tree[root<<1|1];
}
int main()
{
// printf("%d %d\n",2<<4,100>>2);
//freopen("in.txt","r",stdin);
int n,t,x,y;
char o[10];
scanf("%d",&t);
for(int i=1;i<=t;i++)
{
printf("Case %d:\n",i);
scanf("%d",&n);
build(1,n,1);
while(scanf("%s",o))
{
if(o[0]=='E'){
break;
}
scanf("%d %d",&x,&y);
if(o[0]=='A')
update(x,y,1,n,1);
else if(o[0]=='S'){
update(x,-y,1,n,1);
}
else
printf("%d\n",query(x,y,1,n,1));
}
}
return 0;
}
HDU 1754 I Hate It
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cctype>
using namespace std;
int tree[800005];
void build(int left,int right,int root)
{
if(left==right)
{
scanf("%d",&tree[root]);
}
else
{
int mid=(left+right)>>1;
build(left,mid,root<<1);
build(mid+1,right,root<<1|1);
tree[root]=max(tree[root<<1],tree[root<<1|1]);
}
}
void update(int p,int add,int left,int right,int root)
{
if(left==right)
{
tree[root]=add;
}
else
{
int mid=(left+right)>>1;
if(p<=mid)update(p,add,left,mid,root<<1);
else update(p,add,mid+1,right,root<<1|1);
tree[root]=max(tree[root<<1],tree[root<<1|1]);
}
}
int query(int x,int y,int left,int right,int root)
{
if(x<=left&&right<=y)
{
return tree[root];
}
else
{
int sum=0,mid=(left+right)>>1;
if(x<=mid)sum=max(sum,query(x,y,left,mid,root<<1));
if(y>mid)sum=max(sum,query(x,y,mid+1,right,root<<1|1));
return sum;
}
}
int main()
{
int n,m,x,y;
char o[3];
while(scanf("%d%d",&n,&m)!=EOF)
{
build(1,n,1);
while(m--)
{
scanf("%s %d %d",o,&x,&y);
if(o[0]=='U')
update(x,y,1,n,1);
else
printf("%d\n",query(x,y,1,n,1));
}
}
return 0;
}