其实也没有想象中那么难,只要区间里面的数都为1,就不再需要开方了,然后对于>1的数,直接对每一个点暴力开方,然后就可以了。
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<iostream>
#include<vector>
#include<cmath>
#include<map>
#include<string>
#include<queue>
#include<stack>
#include<bitset>
#include<list>
#include<set>
#include<utility>
#include<iomanip>
#define IO ios::sync_with_stdio(false)
#define eps 1e-7
#define int long long
using namespace std;
struct node
{
int l,r,sum;
}t[100005*4];
int n,m,a[100005],cnt;
void build(int rt,int l,int r)
{
t[rt].l=l,t[rt].r=r;
if(l==r)
{
t[rt].sum=a[l];
return;
}
int mid=l+r>>1;
build(rt*2,l,mid);
build(rt*2+1,mid+1,r);
t[rt].sum=t[rt*2].sum+t[rt*2+1].sum;
}
void upd(int rt,int l,int r)
{
if(t[rt].l==t[rt].r)
{
t[rt].sum=sqrt(t[rt].sum);
return;
}
if(l<=t[rt].l&&r>=t[rt].r&&t[rt].sum==t[rt].r-t[rt].l+1)
{
//cout<<"upddddddd: "<<t[rt].l<<" "<<t[rt].r<<" "<<t[rt].sum<<endl;
return;
}
int mid=t[rt].l+t[rt].r>>1;
if(l<=mid)
{
upd(rt*2,l,r);
}
if(r>mid)
{
upd(rt*2+1,l,r);
}
t[rt].sum=t[rt*2].sum+t[rt*2+1].sum;
}
void debug(int rt)
{
if(t[rt].l==t[rt].r)
{
cout<<"1233333: "<<t[rt].l<<" "<<t[rt].sum<<endl;
return;
}
int mid=t[rt].l+t[rt].r>>1;
debug(rt*2);
debug(rt*2+1);
}
int ask(int rt,int l,int r)
{
if(l<=t[rt].l&&r>=t[rt].r)
{
//cout<<"12345"<<" "<<t[rt].l<<" "<<t[rt].r<<" "<<t[rt].sum<<endl;
return t[rt].sum;
}
int mid=t[rt].l+t[rt].r>>1;
if(r<=mid)
{
return ask(rt*2,l,r);
}
else if(l>mid)
{
return ask(rt*2+1,l,r);
}
else
{
return ask(rt*2,l,r)+ask(rt*2+1,l,r);
}
}
signed main()
{
IO;
while(cin>>n)
{
cout<<"Case #"<<++cnt<<":"<<endl;
memset(t,0,sizeof(t));
for(int i=1;i<=n;i++)
{
cin>>a[i];
}
build(1,1,n);
cin>>m;
for(int i=1;i<=m;i++)
{
int t,x,y;
cin>>t>>x>>y;
if(t==0)
{
//cout<<"upd :"<<min(x,y)<<" "<<max(x,y)<<endl;
upd(1,min(x,y),max(x,y));
//debug(1);
}
else
{
cout<<ask(1,min(x,y),max(x,y))<<endl;
}
}
cout<<endl;
}
}