分块求调,全WA
查看原帖
分块求调,全WA
523541
Onana_in_XMFLS楼主2021/9/21 15:03
#include<bits/stdc++.h>
#define LL long long
#define ULL unsigned long long
using namespace std;
const int size = 100010;
LL a[size],sum[size],add[size],L[size],R[size],pos[size],n,m,t;
inline void update(LL l,LL r,LL d)
{
	LL p = pos[l],q = pos[r];
	if(p == q)
	{
		for(int i = l;i <= r;++i) a[i] += d;
		sum[p] += d*(r-l+1);
	}
	else
	{
		for(int i = p+1;i <= q-1;++i) add[i] += d;
		for(int i = l;i <= R[p];++i) a[i] += d;
		sum[p] += d*(R[p]-l+1);
		for(int i = L[q];i <= r;++i) a[i] += d;
		sum[q] += d*(r-L[q]+1);
	}
}
inline LL query(int l,int r)
{
	LL p = pos[l],q = pos[r];
	LL ans = 0;
	if(p == q)
	{
		for(int i = l;i <= r;++i) ans += a[i];
		ans += add[p]*(r-l+1);
	}
	else
	{
		for(int i = p+1;i <= q-1;++i) ans += sum[i]+add[i]*(R[i]-L[i]+1);
		for(int i = l;i <= R[p];++i) ans += a[i];
		ans += add[p]*(R[p]-l+1);
		for(int i = L[q];i <= r;++i) ans += a[i];
		ans += add[q]*(r-L[q]+1);
	}
	return ans;
}
inline void build()
{
	t = sqrt(n);
	for(int i = 1;i <= t;++i)
	{
		L[i] = (i-1)*sqrt(n)+1;
		R[i] = i*sqrt(n);
	}	
	if(R[t] < n) ++t,L[t] = R[t-1]+1,R[t] = n;
	for(int i = 1;i <= t;++i)
		for(int j = L[i];j <= R[i];++j)
			pos[j] = i,sum[i] += a[i];
}
inline LL read()
{
	LL x=0,f=1;
	char ch = getchar();
	while(!isdigit(ch)) {if(ch == '-') f = -1;ch = getchar();}
	while(isdigit(ch)) {x=x*10+ch-48;ch=getchar();}
	return f*x;
}
int main(int argc,char *argv[])
{ 
	//freopen("data.in","r",stdin);
	//freopen("data.out","w",stdout);
	n = read(),m = read();
	for(int i = 1;i <= n;++i) a[i] = read();
	build();	
	while(m--)
	{
		int op = read(),l = read(),r = read();
		if(op == 1)
		{
			int d = read();
			update(l,r,d);
		}
		else printf("%lld\n",query(l,r));
	}
	#ifndef ONLINE_JUDGE
		printf("Time used = %.0lfms\n",((double)clock()/(double)CLOCKS_PER_SEC) * 1000.0);
	#endif
	return 0;exit(0);
} 
2021/9/21 15:03
加载中...