hdu 4027 线段树 Can you answer these queries?

坑哥题

写完就跑出了样例,挺顺的

结果

没注意到开方最多开8次就不用开了 TLE

没用__int64 WA

没想到x>y继续WA

最后还有一个PE

人生啊,总是不能一帆风顺,偶尔会在得意时给你当头一棒

View Code
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define mid (l+r)>>1
const int maxn = 100001;
__int64 sum[maxn<<2];
int cnt[maxn<<2];
bool limit[maxn<<2];//区间内的开方次数是否都超过了8
void pushup(int rt){
sum[rt]=sum[rt<<1]+sum[rt<<1|1];
limit[rt]=limit[rt<<1]&limit[rt<<1|1];
}
void build(int l,int r,int rt){
if(l==r){
scanf("%I64d",&sum[rt]);
limit[rt]=false;
cnt[rt]=0;
return ;
}
int m=mid;
build(lson);build(rson);pushup(rt);
}
void update(int L,int R,int l,int r,int rt){
if(limit[rt]) return ;
if(L<=l&&r<=R){
if(l==r){
sum[rt]=(__int64)sqrt(double(sum[rt]));
cnt[rt]++;
if(cnt[rt]==8) limit[rt]=true;
return ;
}
}
int m=mid;
if(L<=m) update(L,R,lson);
if(R>m) update(L,R,rson);
pushup(rt);
}
__int64 query(int L,int R,int l,int r,int rt){
if(L<=l&&r<=R){
return sum[rt];
}
int m=mid;
__int64 ret=0;
if(L<=m) ret+=query(L,R,lson);
if(R>m) ret+=query(L,R,rson);
return ret;
}
int main(){
int n,m,flag,a,b,c,cases=1;
while(scanf("%d",&n)!=EOF){
build(1,n,1);
scanf("%d",&m);
printf("Case #%d:\n",cases++);
while(m--){
scanf("%d%d%d",&flag,&a,&b);
if(a>b) swap(a,b);
if(flag==0)
update(a,b,1,n,1);
else printf("%I64d\n",query(a,b,1,n,1));
} puts("");
}
return 0;
}



posted @ 2012-01-07 21:53  Because Of You  Views(298)  Comments(0Edit  收藏  举报