TLE求条
查看原帖
TLE求条
772875
RAY091016楼主2025/7/30 15:00

如题,code:

#include<bits/stdc++.h>
#define int long long
using namespace std;
int n,a[1000100],q,op,l,r,x,tot=1;
struct segtree{
	int l,r,sum,maxn;
}t[4000400];
inline void build(int p,int l,int r){
	t[p].l=l,t[p].r=r;
	if(l==r){
		t[p].sum=a[l];
		t[p].maxn=a[l];
		return ;
	}
	int mid=(l+r)>>1;
	build(p*2,l,mid);
	build(p*2+1,mid+1,r);
	t[p].maxn=max(t[p*2].maxn,t[p*2+1].maxn);
	t[p].sum=t[p*2].sum+t[p*2+1].sum;
}
inline void upd(int p,int x,int v){
	if(t[p].l==t[p].r){
		t[p].maxn=v;
		t[p].sum=v;
		return ;
	}
	int mid=(t[p].l+t[p].r)>>1;
	if(x<=mid){
		upd(p*2,x,v);
	}
	else{
		upd(p*2+1,x,v);
	}
	t[p].maxn=max(t[p*2].maxn,t[p*2+1].maxn);
	t[p].sum=t[p*2].sum+t[p*2+1].sum;
}
inline void change(int p,int l,int r){
	if(l<=t[p].l&&t[p].r<=r){
		if(t[p].maxn==1){
			return ;
		}
		else{
			for(int i=t[p].l;i<=t[p].r;i++){
				upd(1,i,sqrt(a[i]));
				a[i]=sqrt(a[i]);
			}
			return ;
		}
	}
	int mid=(t[p].l+t[p].r)>>1;
	if(l<=mid){
		change(p*2,l,r);
	}
	if(r>mid){
		change(p*2+1,l,r);
	}
	t[p].maxn=max(t[p*2].maxn,t[p*2+1].maxn);
	t[p].sum=t[p*2].sum+t[p*2+1].sum;
}
inline int ask(int p,int l,int r){
	if(l<=t[p].l&&t[p].r<=r){
		return t[p].sum;
	}
	int mid=(t[p].l+t[p].r)>>1,ans=0;
	if(l<=mid){
		ans+=ask(p*2,l,r);
	}
	if(r>mid){
		ans+=ask(p*2+1,l,r);
	}
	return ans;
}
signed main(){
	ios::sync_with_stdio(false);
	cin.tie(0),cout.tie(0);
	while(cin>>n){
		cout<<"Case #"<<tot<<":"<<'\n';
		tot++;
		for(int i=1;i<=n;i++){
			cin>>a[i];
		}
		build(1,1,n);
		cin>>q;
		for(int i=1;i<=q;i++){
			cin>>op;
			if(op==0){
				cin>>l>>r;
				if(l>r){
					swap(l,r);
				}
				change(1,l,r);
			}
			if(op==1){
				cin>>l>>r;
				if(l>r){
					swap(l,r);
				}
				cout<<ask(1,l,r)<<'\n';
			}
		}
		cout<<'\n';
	}
	return 0;
}
2025/7/30 15:00
加载中...