10pts猩红一片,求助
查看原帖
10pts猩红一片,求助
521283
wangif424楼主2024/5/16 09:30
#include <bits/stdc++.h>
#define R(x) x = read()
#define ENDL push('\n');
#define SPACE push(' ');
#define int unsigned long long
using namespace std;
char pbuf[1<<20], *pp=pbuf;
inline void push(const char &c) {
	if(pp - pbuf == 1<<20)fwrite(pbuf, 1, 1<<20, stdout),pp = pbuf;
	*pp++ = c;
}
#define BASE 10
class io {public:~io() {fwrite(pbuf, 1, pp - pbuf, stdout);}} _;
inline void write(int x) {
	if (x<0)x=-x,push('-');
	int sta[35],top=0;
	do {
		sta[top++]=x%BASE,x/=BASE;
	} while (x);
	while(top)push(sta[--top]^'0');
}
#ifndef LOCAL
	char buf[1<<23],*p1=buf,*p2=buf;
	#define getchar() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)
#endif
inline int read() {
    int 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 x*f;
}
constexpr int mod=1e9+7;
int n,m;
int a[200100];
int b;
int l[5000],r[5000];
int bl[200100];
int s[5000];
void build(){
	b=max((int)(sqrt(n)+3)/4,1ull);
	for(int i=1,id=1;i<=n;i++){
		bl[i]=id;
		if(i%b==0)id++;
	}
	for(int i=1;i<=n;i++){
		if(bl[i]!=bl[i-1])l[bl[i]]=i;
		if(bl[i]!=bl[i+1])r[bl[i]]=i;
		s[bl[i]]=(s[bl[i]]+a[i])%mod;
	}
}
void add(int i,int x){
	a[i]=(a[i]+x)%mod;
	s[bl[i]]=(s[bl[i]]+x)%mod;
}
int query(int ll,int rr){
	int res=0;
	if(bl[ll]==bl[rr])for(int i=ll;i<=rr;i++)res=(res+a[i])%mod;
	else{
		for(int i=ll;i<=r[bl[ll]];i++)res=(res+a[i])%mod;
		for(int i=l[bl[rr]];i<=rr;i++)res=(res+a[i])%mod;
		for(int i=bl[ll]+1;i<=bl[rr]-1;i++)res=(res+s[i])%mod;
	}
	return res;
}
int pr[5000][5000],sf[5000][5000];
void add(int x,int y,int z){
	for(int i=x;i>=y;i--)pr[x][i]=(pr[x][i]+z)%mod;
	for(int i=1;i<=y;i++)sf[x][i]=(sf[x][i]+z)%mod;
}
signed main(){
	R(n);R(m);
	for(int i=1;i<=n;i++)R(a[i]);
	build();
	while(m--){
		int R(opt),R(x),R(y);
		switch(opt){
			case 1:{
				int R(z);
				if(x<b)add(x,y,z);
				else for(int i=y;i<=n;i+=x)add(i,z);
				break;
			}
			case 2:{
				int ans=query(x,y);
				for(int i=1;i<b;i++) {
					int X=(x-1)/i+1,Y=(y-1)/i+1;
					int L=Y-X-1;
					if(X==Y)ans=(ans-pr[i][(x-1)%i+1]+pr[i][(y-1)%i+1])%mod;
					else ans=(ans+L*pr[i][i]+pr[i][(y-1)%i+1]+sf[i][(x-1)%i+1])%mod;
				}
				write(ans);push('\n');
				break;
			}
		}
	}
    return 0;
}
2024/5/16 09:30
加载中...