题目链接:点击这里
题目大意:
给定一个长度为
n
n
n 的序列
a
1
,
a
2
,
.
.
.
,
a
n
a_1,a_2,...,a_n
a1,a2,...,an ,对序列进行区间开方和区间查询
题目分析:
因为
1
≤
a
i
≤
1
e
12
1 \le a_i \le 1e12
1≤ai≤1e12 而
1
e
12
1e12
1e12 连续开方
6
6
6 次就会变成
1
1
1 ,所以修改次数实际上是很少的然后我们用线段树维护区间和和区间最值,如果区间最值为
1
1
1 就不开方了否则就对每一个权值不为
1
1
1 的点进行开方
具体细节见代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
#include<set>
#include<map>
#include<queue>
#define ll long long
#define inf 0x3f3f3f3f
#define Inf 0x3f3f3f3f3f3f3f3f
#define int ll
using namespace std;
ll read()
{
ll res = 0,flag = 1;
char ch = getchar();
while(ch<'0' || ch>'9')
{
if(ch == '-') flag = -1;
ch = getchar();
}
while(ch>='0' && ch<='9')
{
res = (res<<3)+(res<<1)+(ch^48);//res*10+ch-'0';
ch = getchar();
}
return res*flag;
}
const int maxn = 5e5+5;
const int mod = 1e9+7;
const double pi = acos(-1);
const double eps = 1e-8;
struct sgt{
int val,maxx;
}a[maxn<<2];
int n,m,sco[maxn];
char s[3];
void pushup(int root)
{
a[root].val = a[root<<1].val+a[root<<1|1].val;
a[root].maxx = max(a[root<<1].maxx,a[root<<1|1].maxx);
}
void build(int root,int l,int r)
{
if(l == r)
{
a[root].val = a[root].maxx = read();
return ;
}
int mid = l+r>>1;
build(root<<1,l,mid);
build(root<<1|1,mid+1,r);
pushup(root);
}
void updat(int root,int l,int r,int ql,int qr)
{
if(l>qr || r<ql) return ;
if(l == r)
{
a[root].val = sqrt(a[root].val);
a[root].maxx = sqrt(a[root].maxx);
return ;
}
int mid = l+r>>1;
if(a[root<<1].maxx != 1) updat(root<<1,l,mid,ql,qr);
if(a[root<<1|1].maxx != 1) updat(root<<1|1,mid+1,r,ql,qr);
pushup(root);
}
int query(int root,int l,int r,int ql,int qr)
{
if(l>qr || r<ql) return 0;
if(l>=ql && r<=qr) return a[root].val;
int mid = l+r>>1;
return query(root<<1,l,mid,ql,qr)+query(root<<1|1,mid+1,r,ql,qr);
}
signed main()
{
n = read();
build(1,1,n);
m = read();
while(m--)
{
int opt = read(),x = read(),y = read();
if(x > y) swap(x,y);
if(opt == 0) updat(1,1,n,x,y);
else printf("%lld\n",query(1,1,n,x,y));
}
return 0;
}