跳到正文

题解

P5309 [Ynoi2011] 初始化

jr_linys约 2 分钟
原文
文章信息与版本

文章信息

发布
2024年8月6日
收录
2026年8月14日
更新
2026年11月15日
篇幅
约 2 分钟
获赞
8
题目
P5309

版本

  1. v1当前

这是一个不用分块的题解。

根号分治。
对原序列做前缀和。
x≤nx\le\sqrt n 的限制,对于每个 xx 暴力维护贡献的前缀和。
x≥nx\ge\sqrt n 的限制,先扔 vector 里暴力判断,限制数量大于等于 n\sqrt n 时合并到数组里。

时间复杂度 O(nn)O(n\sqrt n),块长直接取 n\sqrt n 都能过。

C++
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=2e5,M=ceil(sqrt(N));
int n,m,B;
ll a[N+5],b[M+5][M+5],c[N+5];
struct stu{int x,y,z;};
vector<stu> v;
ll get(int p){
	ll ans=a[p];
	for(auto [x,y,z]:v) ans+=1ll*(p/x+(p%x>=y))*z;
	for(int x=1;x<=B/4;++x) ans+=p/x*b[x][x]+b[x][p%x];
	return ans;
}

int main(){
	ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
	cin>>n>>m,B=ceil(sqrt(n));
	for(int i=1;i<=n;++i) cin>>a[i],a[i]+=a[i-1];
	while(m--){
		int opt,x,y,z;
		cin>>opt>>x>>y;
		if(opt==1){
			cin>>z;
			if(x<=B/4){
				for(int i=y;i<=x;++i) b[x][i]+=z;
			}else{
				v.push_back({x,y,z});
				if(v.size()>=B/2){
					memset(c,0,sizeof c);
					for(auto [x,y,z]:v) for(int i=y;i<=n;i+=x) c[i]+=z;
					v.clear();
					for(int i=1;i<=n;++i) c[i]+=c[i-1],a[i]+=c[i];
				}
			}
		}else{
			cout<<(get(y)-get(x-1))%1000000007ll<<'\n';
		}
	}
}
原文
存档评论5 条抓取于