萌新分块求卡常!
查看原帖
萌新分块求卡常!
153386
a937082708楼主2020/5/16 07:03

写了个分块,每次提交(没吸氧)都TLE两个,而且只是令人惋惜的超时零点零几秒,求大佬帮忙优化!

#include<bits/stdc++.h>
using namespace std;
inline int read()
{
	char c;int res=0,flag(1);
	for(;!isdigit(c);c=getchar())if(c=='-')flag=-1;
	for(;isdigit(c);c=getchar())res=res*10+c-'0';
	return res*flag;
}
typedef long long ll;
const int N=1e6+5;
int n,q,tot,Block,t,k,x,y;
ll a[N],lazy[N];
int L[N],R[N],belong[N];
inline void build()
{
	Block=sqrt(n);
	tot=(n%Block!=0)+n/Block;
	for(register int i(1);i<=tot;++i)
	{
		L[i]=(i-1)*Block+1;
	}
	for(register int i(1);i<=tot;++i)
	{
		R[i]=i*Block;
	}
	R[tot]=n;
	for(register int i(1);i<=n;++i)
	{
		belong[i]=(i-1)/Block+1;
	}
}
inline void change(int x,int y,ll val)
{
	if(belong[x]==belong[y])
	{
		for(register int i=x;i<=y;++i)
		{
			a[i]+=val;
		}
		return;
	}
	for(register int i=x;i<=R[belong[x]];++i)
	{
		a[i]+=val;
	}
	for(register int i=L[belong[y]];i<=y;++i)
	{
		a[i]+=val;
	}
	for(register int i=belong[x]+1;i<=belong[y]-1;++i)
	{
		lazy[i]+=val;
	}
}
int main()
{
	n=read(),q=read();
	for(register int i(1);i<=n;++i)
	{
		a[i]=read();
	}
	build();
	for(register int i(1);i<=q;++i)
	{
		t=read();
		if(t==1)
		{
			x=read(),y=read(),k=read();
			change(x,y,k);
		}
		else
		{
			x=read();
			printf("%lld\n",a[x]+lazy[belong[x]]);
		}
	}
	return 0;
}
2020/5/16 07:03
加载中...