社区讨论

子任务2TLE求助

P13976数列分块入门 1参与者 3已保存回复 4

讨论操作

快速查看讨论及其快照的属性,并进行相关操作。

当前回复
4 条
当前快照
1 份
快照标识符
@mid0l687
此快照首次捕获于
2025/11/24 18:39
3 个月前
此快照最后确认于
2025/11/24 19:33
3 个月前
查看原帖
CPP
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N=3e5+5;
const int SQ=605;
int n,a[N],nn,tot,bel[N];
int st[SQ],ed[SQ],sum[SQ],tag[SQ];
inline int read()
{
	int x=0,f=1;char ch=getchar();
	while(ch<'0'||ch>'9') {if(ch=='-') f=-1;ch=getchar();}
	while(ch>='0'&&ch<='9'){x=x*10+ch-48;ch=getchar();}
	return x*f;
}
void init()
{
	nn=sqrt(n);
	for(int i=1;i<=n;i+=nn) st[++tot]=i,ed[tot]=i+nn-1;ed[tot]=n;
	for(int i=1;i<=tot;++i)
		for(int j=st[i];j<=ed[i];++j) bel[j]=i;
}
void update(int l,int r,int c)
{
	int x=bel[l],y=bel[r];
	if(x==y) {for(int i=l;i<=r;++i) a[i]+=c;return;}
	if(l!=st[x]) {for(int i=l;i<=ed[x];++i) a[i]+=c;x++;}
	if(r!=ed[y]) {for(int i=r;i>=st[y];--i) a[i]+=c;y--;}
	for(int i=x;i<=y;++i) tag[i]+=c;
}
signed main()
{
	n=read();
	for(int i=1;i<=n;++i) a[i]=read();
	int m=n;
	while(m--)
	{
		int op=read(),l=read(),r=read(),c=read();
		if(op==0) update(l,r,c);
		if(op==1) printf("%lld\n",a[r]+tag[bel[r]]);
	}
	return 0;
}

回复

4 条回复,欢迎继续交流。

正在加载回复...