P4145 上帝造题的七分钟2 / 花神游历各国 (线段树区间开方)

题目链接:点击这里

题目大意:
给定一个长度为 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 1ai1e12 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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值