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

暴力分块

#include<bits/stdc++.h>
using namespace std;
const int N = 1e6+10;
using ll = long long;
ll b[N],w[N];
int l[N],r[N],belong[N];
int n,q,len,tot;



inline void init()
{
	len = sqrt(n),tot = (n+len-1)/len;
	
	for(int i=1;i<=tot;++i)l[i] = r[i-1]+1,r[i] = i*len;
	r[tot] = n;
	for(int i=1;i<=tot;++i){
		for(int j=l[i];j<=r[i];++j)
		 b[i]+=w[j],belong[j] = i;
	}
}

inline void modify(int ql,int qr){
	int p = belong[ql],q = belong[qr];
	if(p==q){
		for(int i=ql;i<=qr;++i)
		 b[p]-=w[i],w[i] = sqrt(w[i]),b[p]+=w[i];
		return;
	}
	
	for(int i=ql;i<=r[p];++i) b[p]-=w[i],w[i] = sqrt(w[i]),b[p]+=w[i];
	for(int i=qr;i>=l[q];--i) b[q]-=w[i],w[i] = sqrt(w[i]),b[q]+=w[i];
	
	for(int i=p+1;i<=q-1;++i){
		if(b[i]==r[i]-l[i]+1)continue;
		for(int j=l[i];j<=r[i];++j)
		  b[i]-=w[j],w[j] = sqrt(w[j]),b[i]+=w[j];
	}
}


inline ll query(int ql,int qr){
	int p = belong[ql],q = belong[qr];
	ll res = 0;
	if(p==q){
		for(int i=ql;i<=qr;++i)
		 res+=w[i];
		return res;
	}
	
	for(int i=ql;i<=r[p];++i)res+=w[i];
	for(int i=qr;i>=l[q];--i)res+=w[i];
	
	for(int i=p+1;i<=q-1;++i)res+=b[i];
	
	return res;
}



int main()
{
	ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
	cin>>n;
	q = n;
	for(int i=1;i<=n;i++)cin>>w[i];
	init();
	
	while(q--){
		int op,ql,qr,c;cin>>op>>ql>>qr>>c;
		//if(ql>qr)swap(ql,qr);
		if(!op)modify(ql,qr);
		else cout<<query(ql,qr)<<"\n";
	}
	
	return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值