P4145已A,TLE求调
查看原帖
P4145已A,TLE求调
676569
WoodReal12楼主2025/2/5 19:48
#include <iostream>
#include <cmath>
#define int long long
#define lson p<<1
#define rson p<<1|1
using namespace std;
char buf[1<<23],*p1=buf,*p2=buf,obuf[1<<23],*O=obuf;
#define gc() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)
template <typename T>
inline void read(T &f){
	f=1;
	T x=0;
	char ch=gc();
	while(ch<'0'||ch>'9'){
		if(ch=='-')
			f=-1;
		ch=gc();
	}
	while(ch>='0'&&ch<='9'){
		x=x*10+ch-'0';
		ch=gc();
	}
	f*=x;
}
template <typename T>
inline void print(T x){
	if(x<0){
		putchar('-');
		x=-x;
	}
	if(x>9)
		print(x/10);
	putchar(x%10+'0');
}
const int N=1e5+5;
struct node{
	int sum,mx;
}seg[N*4]; 
void upd(int p){
	seg[p].sum=seg[lson].sum+seg[rson].sum;
	seg[p].mx=max(seg[lson].mx,seg[rson].mx);
}
int a[N],n;
void build(int p,int L,int R){
	if(L==R){
		seg[p].sum=a[L],seg[p].mx=a[L];
		return ;
	}
	int mid=L+R>>1;
	build(lson,L,mid);
	build(rson,mid+1,R);
	upd(p);
}
void modify(int p,int L,int R,int l,int r){
	if(L==R){
		seg[p].sum=sqrt(seg[p].sum);
		seg[p].mx=sqrt(seg[p].mx);
		return ;
	}
	int mid=L+R>>1;
	if(l<=mid&&seg[lson].mx>1)
		modify(lson,L,mid,l,r);
	if(r>mid&&seg[rson].mx>1)
		modify(rson,mid+1,R,l,r);
	upd(p);
}
int query(int p,int L,int R,int l,int r){
	if(l<=L&&R<=r)
		return seg[p].sum;
	int mid=L+R>>1;
	int ans=0;
	if(l<=mid)
		ans+=query(lson,L,mid,l,r);
	if(r>mid)
		ans+=query(rson,mid+1,R,l,r);
	return ans;
}
signed main(){
	int cnt=0;
//	ios::sync_with_stdio(0);
//	cin.tie(0),cout.tie(0);
	while(~scanf("%d",&n)){
	//	cin>>n;
		cnt++;
		for(int i=1;i<=n;i++)
			read(a[i]);
		build(1,1,n);
		int q;
		read(q);
		cout<<"Case #"<<cnt<<":"<<endl;
		while(q--){
			char op;
			cin>>op;
			if(op=='0'){
				int l,r;
				read(l),read(r);
				if(l>r)
					swap(l,r);
				modify(1,1,n,l,r);
			}
			else{
				int l,r;
				read(l),read(r);
				if(l>r)
					swap(l,r);
				print(query(1,1,n,l,r)),cout<<endl;
			}
		}
		cout<<endl;
	}
    return 0;
}
2025/2/5 19:48
加载中...