洛谷P4145 上帝造题的七分钟2 / 花神游历各国[ (线段树) 或 (树状数组+并查集) ]题解

题目:
在这里插入图片描述
线段树思路:
我是通过线段树来写的。
通过线段树维护两个值:区间最大值maxx和区间和sum

那么,该如何实现区间开方这个操作呢?答案是直接单点修改。
听起来有点不可思议,但是这种方法的可行的原因,是因为数据范围中,10e12的数字,最多经过6次的开方,就能到1。
又因为对于数字1,有sqrt(1)==1,所以对于到达1的数字,我们在区间修改中便可以跳过,只去修改其他的数字,根据这个结论,我们最坏的情况也只需要进行6*n次的修改。

那么,我们怎么跳过全部为1的区间?通过维护区间最大值线段树,如果这个区间的maxx为1,说明这个区间的数值都是1,就直接跳过。

void update(int L,int R,int l,int r,int pos){
	if(t[pos].maxx==1){return;}//区间内已经全部开方到1的情况,直接跳过这个区间 
	if(r<L||R<l) return;
	if(l==r){
		t[pos].sum = sqrt(t[pos].sum);
		t[pos].maxx = t[pos].sum;
		return;
	}
	int mid = (l+r)>>1;
	update(L,R,l,mid,pos<<1);
	update(L,R,mid+1,r,pos<<1|1);
	pushup(pos);
}

我们在用update函数更新的时候,就一直往下递归,碰到区间不相交或者maxx==1的情况,就跳过这段。

线段树代码:

#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<map>
#include<queue>
#include<cmath>
using namespace std;

template<class T>inline void read(T &x){x=0;char o,f=1;while(o=getchar(),o<48)if(o==45)f=-f;do x=(x<<3)+(x<<1)+(o^48);while(o=getchar(),o>47);x*=f;}
//int cansel_sync=(ios::sync_with_stdio(0),cin.tie(0),0);
#define ll long long
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define repb(i,a,b) for(int i=(a);i>=b;i--)
#define INF 0x3f3f3f3f
#define cendl printf("\n")
ll gcd(ll a,ll b){ while(b^=a^=b^=a%=b); return a; }
//#define INF 0x7fffffff

const int MAXN = 1e5+5;

ll ans;
int n; 

struct node{
	ll sum;
	ll maxx;
}t[MAXN<<2];

void pushup(int pos){
	t[pos].sum = t[pos<<1].sum + t[pos<<1|1].sum;
	t[pos].maxx = max(t[pos<<1].maxx,t[pos<<1|1].maxx);
} 

void build(int l,int r,int pos){
	if(l==r){
		scanf("%lld",&t[pos].sum);
		t[pos].maxx = t[pos].sum;
		return;
	}
	int mid = (l+r)>>1;
	build(l,mid,pos<<1);
	build(mid+1,r,pos<<1|1);
	pushup(pos);
}

void update(int L,int R,int l,int r,int pos){
	if(t[pos].maxx==1){return;}//区间内已经全部开方到1的情况 
	if(r<L||R<l) return;
	if(l==r){
		t[pos].sum = sqrt(t[pos].sum);
		t[pos].maxx = t[pos].sum;
		return;
	}
	int mid = (l+r)>>1;
	update(L,R,l,mid,pos<<1);
	update(L,R,mid+1,r,pos<<1|1);
	pushup(pos);
}

void query(int L,int R,int l,int r,int pos){
	if(L<=l&&r<=R){
		ans+=t[pos].sum;
		return;
	}
	if(r<L||R<l) return;
	int mid = (l+r)>>1;
	query(L,R,l,mid,pos<<1);
	query(L,R,mid+1,r,pos<<1|1);
	return;
}

inline ll tquery(int l,int r){
	if(l>r) swap(l,r); 
	ans = 0;
	query(l,r,1,n,1);
	return ans;
}

inline void tupdate(int l,int r){
	if(l>r) swap(l,r);
	update(l,r,1,n,1);
}

int main(){
	int m;
	scanf("%d",&n);
	build(1,n,1);
	scanf("%d",&m);
	int typ;
	int l,r;
	while(m--){
		scanf("%d",&typ);
		if(typ==0){
			scanf("%d%d",&l,&r);
			tupdate(l,r);
		}
		else{
			scanf("%d%d",&l,&r);
			printf("%lld\n",tquery(l,r));
		}
	}
}

在这里插入图片描述

树状数组+并查集思路:

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值