Luogu P4145 上帝造题的七分钟2 / 花神游历各国

#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int N=1e5+10;
int n;
ll tree[N<<2],maxn[N<<2];
int Q;

void build(int o,int l,int r){
	if(l==r){
		scanf("%lld",&tree[o]);
		maxn[o]=tree[o];
		return;
	}
	int mid=l+r>>1;
	build(o<<1,l,mid);
	build(o<<1|1,mid+1,r);
	tree[o]=tree[o<<1]+tree[o<<1|1];
	maxn[o]=max(maxn[o<<1],maxn[o<<1|1]);
}

ll quire(int o,int l,int r,int L,int R){
	if(L<=l && r<=R)return tree[o];
	int mid=l+r>>1;
	ll ans=0;
	if(mid>=L)ans+=quire(o<<1,l,mid,L,R);
	if(mid<R)ans+=quire(o<<1|1,mid+1,r,L,R);
	return ans;
}

void update(int o,int l,int r,int L,int R){
	if(maxn[o]==1)return;
	if(l==r){
		tree[o]=sqrt(tree[o]);
		maxn[o]=tree[o];
		return;
	}
	int mid=l+r>>1;
	if(mid>=L)update(o<<1,l,mid,L,R);
	if(mid<R)update(o<<1|1,mid+1,r,L,R);
	tree[o]=tree[o<<1]+tree[o<<1|1];
	maxn[o]=max(maxn[o<<1],maxn[o<<1|1]);
}

int main(){
	scanf("%d",&n);
	build(1,1,n);
	scanf("%d",&Q);
	while(Q--){
		int t,l,r;
		scanf("%d%d%d",&t,&l,&r);
		if(l>r)swap(l,r);
		if(t==1)printf("%lld
",quire(1,1,n,l,r));
		else update(1,1,n,l,r);
	}
	return 0;
}

  

原文地址:https://www.cnblogs.com/codetogether/p/13985958.html