题意
N个点,每次攻击或查询一个区间,攻击区间可以使区间内的所有点的生命值变为sqrt(生命值)。查询一个区间,则输出该区间内所有生命值的和。
题解
这道题最重要的是剪枝。如果一个数为1后,开平方就无效,因此对于为1的区间,就直接跳过,不进行开平方处理,这样可以大大节省时间。有个比较坑的事情是数据没有0,这样的话直接利用一个和数组就可以判断该区间内所有的数字是否为1。(更坑的是,如果单独开一个数组来判断区间内所有数字是否为1,会TLE。。)
注意事项
这道题的时间卡的很严,我多开了一个记录1的数组(避免0出现),然后就一直TLE。。。
代码
#include <iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<vector>
#include<cmath>
#include<queue>
#include<string>
#include<set>
#include<map>
#include<bitset>
#define UP(i,l,h) for(int i=l;i<h;i++)
#define DOWN(i,h,l) for(int i=h-1;i>=l;i--)
#define W(a) while(a)
#define MEM(a,b) memset(a,b,sizeof(a))
#define INF 0x3f3f3f3f
#define LL long long
#define MAXN 400010
#define EPS 1e-10
#define MOD 100000000
using namespace std;
int lt,rt;
LL num[MAXN];
int one[MAXN];
void maintain(int o) {
num[o]=num[o*2]+num[o*2+1];
}
void build(int o,int l,int r){
if(l==r){
LL x;
scanf("%I64d",&x);
num[o]=x;
}else{
int m=l+(r-l)/2;
build(o*2,l,m);
build(o*2+1,m+1,r);
maintain(o);
}
}
void attack(int o,int l,int r) {
if(num[o]==(r-l+1)) {
return ;
} else if(l==r) {
num[o]=sqrt(num[o]);
} else {
int m=l+(r-l)/2;
if(lt<=m) {
attack(o*2,l,m);
}
if(rt>m) {
attack(o*2+1,m+1,r);
}
maintain(o);
}
}
LL query(int o,int l,int r) {
if(lt<=l&&rt>=r) {
return num[o];
} else {
int m=l+(r-l)/2;
LL ans=0;
if(lt<=m) {
ans+=query(o*2,l,m);
}
if(rt>m) {
ans+=query(o*2+1,m+1,r);
}
return ans;
}
}
main() {
int n;
int ks=0;
W(~scanf("%d",&n)) {
MEM(num,0);
MEM(one,0);
printf("Case #%d:\n",++ks);
build(1,1,n);
int q;
scanf("%d",&q);
UP(i,0,q) {
int t;
scanf("%d%d%d",&t,<,&rt);
if(lt>rt)
swap(lt,rt);
if(t==0) {
attack(1,1,n);
} else {
printf("%I64d\n",query(1,1,n));
}
}
puts("");
}
}
/*
10
1 2 3 4 5 6 7 8 9 10
5
0 10 1
1 10 1
1 5 1
0 8 5
1 8 4
*/