您的位置:首页 > 其它

HDU 1166 敌兵布阵

2013-12-23 14:00 253 查看
(更新点查询区间)

好吧,今天算是被这道题坑惨了,TLE了许久,结果发现原来就是用了一个cout的原因。。改成printf后一下就过了,而且时间一下缩短数倍。。俗话说,吃一堑,长一智,,我算是长记性了。。

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <cstdlib>

using namespace std;
#define N 50011

int tree[4*N];

int a
;

void build(int l,int r,int rt)
{
if(l==r)
{
scanf("%d",&tree[rt]);
return;
}
int mid = (l+r)/2;
build(l,mid,2*rt);
build(mid+1,r,2*rt+1);
tree[rt] = tree[2*rt] + tree[2*rt+1];
}

void update(int l,int r,int pos,int val,int rt)
{
if(l==r)
{
tree[rt] += val;
return;
}
int mid = (l+r)/2;
if(pos<=mid)
update(l,mid,pos,val,2*rt);
else
update(mid+1,r,pos,val,2*rt+1);
tree[rt] = tree[2*rt] + tree[2*rt+1];
}

int query(int l,int r,int aa,int bb,int rt)
{
if(aa>r||bb<l)
return 0;
if(aa<=l&&bb>=r)
return tree[rt];
int mid = (l+r)/2;
if(bb<=mid)
return query(l,mid,aa,bb,2*rt);
else if(aa>mid)
return query(mid+1,r,aa,bb,2*rt+1);
return query(l,mid,aa,bb,2*rt)+query(mid+1,r,aa,bb,2*rt+1);
}

int main()
{
int t,n,i;
char ss[10];
int aa,bb;
scanf("%d",&t);
int cs = 1;
while(t--)
{
memset(tree,0,sizeof(tree));
scanf("%d",&n);
build(1,n,1);
printf("Case %d:\n",cs++);
while(scanf("%s",ss)&&ss[0]!='E')
{
scanf("%d%d",&aa,&bb);
if(ss[0] == 'A')
update(1,n,aa,bb,1);
else if(ss[0] == 'S')
update(1,n,aa,-bb,1);
else if(ss[0] == 'Q')
{
int ans = query(1,n,aa,bb,1);
printf("%d\n",ans);   //Don't use cout!
}
}

}
return 0;
}


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: