这道题一开始用分块写,结果不知是什么,可能写挂了,TLE。(呵呵)
之后便仔细想一想,可以用线段树做,思路跟分块差不多,因为这道题的开根号后是向下取整,所以经过几次开根后这个数就变成了1或0,这样就给它打上一个标记,以后便不用动它,可以省很多时间。这题便就解决了。
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<cstdlib>
using namespace std;
struct node
{
int l,r,lc,rc;long long c;
bool lazy;
node(){lazy=false;}
}tr[210000];int trlen;
int n,m;long long a[110000];
void bt(int l,int r)
{
trlen++;
int now=trlen;
tr[now].l=l;tr[now].r=r;
tr[now].lc=tr[now].rc=-1;
if(l==r)
{
tr[now].c=a[l];
if(a[l]==1 || a[l]==0)tr[now].lazy=true;
}
else
{
int mid=(l+r)/2;
tr[now].lc=trlen+1;bt(l,mid);
tr[now].rc=trlen+1;bt(mid+1,r);
tr[now].c=tr[tr[now].lc].c+tr[tr[now].rc].c;
if(tr[tr[now].lc].lazy==true && tr[tr[now].rc].lazy==true)tr[now].lazy=true;
}
}
long long getsum(int now,int l,int r)
{
if(tr[now].l==l && tr[now].r==r)return tr[now].c;
int lc=tr[now].lc,rc=tr[now].rc,mid=(tr[now].l+tr[now].r)/2;
if(r<=mid)return getsum(lc,l,r);
else if(mid+1<=l)return getsum(rc,l,r);
else return (getsum(lc,l,mid)+getsum(rc,mid+1,r));
}
void change(int now,int l,int r)
{
if(tr[now].lazy==true)return ;
if(tr[now].l==tr[now].r)
{
tr[now].c=(long long)sqrt(tr[now].c);
if(tr[now].c==1 || tr[now].c==0)tr[now].lazy=true;
return ;
}
int lc=tr[now].lc,rc=tr[now].rc,mid=(tr[now].l+tr[now].r)/2;
if(r<=mid)change(lc,l,r);
else if(mid+1<=l)change(rc,l,r);
else change(lc,l,mid),change(rc,mid+1,r);
tr[now].c=tr[lc].c+tr[rc].c;
if(tr[lc].lazy==true && tr[rc].lazy==true)tr[now].lazy=true;
}
int main()
{
scanf("%d",&n);
for(int i=1;i<=n;i++)scanf("%lld",&a[i]);
trlen=0;bt(1,n);
scanf("%d",&m);
for(int i=1;i<=m;i++)
{
int q,x,y;
scanf("%d%d%d",&q,&x,&y);
if(x>y)swap(x,y);
if(q==1)printf("%lld\n",getsum(1,x,y));
else change(1,x,y);
}
return 0;
}