【loj6281】数列分块练习 5

「分块」数列分块入门1 – 9 by hzwer

#include
#include
#include
#include
#include
#define inf 0x3f3f3f3f
#define N 1000009
#define ll long long
using namespace std;

ll v[N],sum[N];
int blo[N],L[N],R[N],f[N];
int n,len;

ll read()
{
	char ch=getchar();ll fu=1,ret=0;
	while(ch<'0'||ch>'9') {if (ch=='-') fu=-1;ch=getchar();}
	while(ch>='0'&&ch<='9'){ret=ret*10+ch-'0';ch=getchar();}
	return fu*ret;
}

void solve_sqrt(int x)
{
	if (f[x]) return;
	f[x]=1;
	sum[x]=0;
	for (int i=(x-1)*len+1;i<=min(x*len,n);i++)
	{
		v[i]=sqrt(v[i]);sum[x]+=v[i];
		if (v[i]>1) f[x]=0;
	}
}

void change(int l,int r)
{
	int p=blo[l],q=blo[r];
	for (int i=l;i<=min(R[l],r);i++) {sum[p]-=v[i];v[i]=sqrt(v[i]);sum[p]+=v[i];}
	if (p!=q)
		for (int i=L[r];i<=r;i++) {sum[q]-=v[i];v[i]=sqrt(v[i]);sum[q]+=v[i];}
	for (int i=p+1;i<=q-1;i++) solve_sqrt(i);
}

ll query(int l,int r)
{
	int p=blo[l],q=blo[r];ll ret=0;
	for (int i=l;i<=min(R[l],r);i++) ret+=v[i];
	if (p!=q)
		for (int i=L[r];i<=r;i++) ret+=v[i];
	for (int i=p+1;i<=q-1;i++) ret+=sum[i];
	return ret; 
}


int main()
{
	n=read();len=sqrt(n);
	for (int i=1;i<=n;i++) v[i]=read();
	for (int i=1;i<=n;i++) {blo[i]=(i-1)/len+1;L[i]=(blo[i]-1)*len+1;R[i]=min(blo[i]*len,n);sum[blo[i]]+=v[i];}
	for (int i=1;i<=n;i++)
	{
		int opt=read(),l=read(),r=read();ll d=read();
		if (!opt) change(l,r);
		else printf("%lld\n",query(l,r));
	}

	return 0;
}

你可能感兴趣的:(LibreOJ,中级数据结构-分块)