洛谷—P4145 上帝造题的七分钟2(线段树)

在这里插入图片描述

#include<bits/stdc++.h>
using namespace std;
const int maxn=100010;
#define ll long long
long long sum[maxn<<2],arr[maxn],m[maxn<<2];
void pushup(ll p,ll l,ll r)
{
	sum[p]=sum[p<<1]+sum[p<<1|1];
	m[p]=max(m[p<<1],m[p<<1|1]);
}
void build(ll p,ll l,ll r)
{
	if(l==r)
	{
		sum[p]=m[p]=arr[l];
		return;
	}
	ll mid=(l+r)>>1;
	build(p<<1,l,mid);
	build(p<<1|1,mid+1,r);
	pushup(p,l,r);
}
void change(ll p,ll l,ll r,ll ql,ll qr)
{
	if(l==r)
	{
		sum[p]=m[p]=sqrt(sum[p]);
		return;
	}
	ll mid=(l+r)>>1;
	if(ql<=mid&&m[p<<1]>1) change(p<<1,l,mid,ql,qr);
	if(qr>mid) change(p<<1|1,mid+1,r,ql,qr);
	pushup(p,l,r);
}
long long ask(ll p,ll l,ll r,ll ql,ll qr)
{
	if(ql<=l&&qr>=r) return sum[p];
	ll mid=(l+r)>>1;
	long long ans=0;
	if(ql<=mid) ans+=ask(p<<1,l,mid,ql,qr);
	if(qr>mid) ans+=ask(p<<1|1,mid+1,r,ql,qr);
	return ans;
}
void exchange(ll& x,ll& y)
{
	ll temp=x;
	x=y;
	y=temp;
}
int main()
{
	ll n,m;
	cin>>n;
	for(ll i=1;i<=n;++i)
	cin>>arr[i];
	cin>>m;
	build(1,1,n);
	while(m--)
	{
		ll temp,x,y;
		cin>>temp>>x>>y;
		if(x>y) exchange(x,y);
		if(temp==0) change(1,1,n,x,y);
		else cout<<ask(1,1,n,x,y)<<endl;
	}
	return 0;
}
发布了165 篇原创文章 · 获赞 11 · 访问量 4885

猜你喜欢

转载自blog.csdn.net/weixin_43784305/article/details/104949013