洛谷 P2023 [AHOI2009] 维护序列 【序列分块】

题目链接

题意:

构建一种数据结构,支持

  1. 区间乘
  2. 区间加
  3. 区间求和

题解:

一看就是线段树的模板题,但我就要用分块(顺便加上丑陋的压行

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include 
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
#include
#include
#include
#include
using namespace __gnu_pbds;
#include
using namespace __gnu_cxx;

#define int long long
#define PI acos(-1.0)
#define eps 1e-9
#define lowbit(a) ((a)&-(a))

const int mod = 1e9+7;
int qpow(int a,int b){
	int ans=1;
	while(b){
		if(b&1)ans=(ans*a)%mod;
		a=(a*a)%mod;
		b>>=1;
	}
	return ans;
}
const int INF = 0x3f3f3f3f;
const int N = 1e6+10;
int block[N],blk=313,a[N],l[N],r[N],sum[N],add[N],mul[N];
int n,m; 
void Add(int x,int y,int c){//区间加
	if(block[x]==block[y]){
		for(int i=l[block[x]];i<=r[block[x]];i++)a[i]=(a[i]*mul[block[x]]+add[block[x]])%m;
		add[block[x]]=0,mul[block[x]]=1;
		for(int i=x;i<=y;i++)sum[block[x]]=(sum[block[x]]-a[i]+(a[i]=(a[i]+c)%m))%m;
	}
	else{
		for(int i=block[x]+1;i<=block[y]-1;i++)sum[i]=(sum[i]+c*(r[i]-l[i]+1))%m,add[i]=(add[i]+c)%m;
		for(int i=l[block[x]];i<=r[block[x]];i++)a[i]=(a[i]*mul[block[x]]+add[block[x]])%m;
		add[block[x]]=0,mul[block[x]]=1;
		for(int i=x;i<=r[block[x]];i++)sum[block[x]]=(sum[block[x]]-a[i]+(a[i]=(a[i]+c)%m))%m;
		for(int i=l[block[y]];i<=r[block[y]];i++)a[i]=(a[i]*mul[block[y]]+add[block[y]])%m;
		add[block[y]]=0,mul[block[y]]=1;
		for(int i=l[block[y]];i<=y;i++)sum[block[y]]=(sum[block[y]]-a[i]+(a[i]=(a[i]+c)%m))%m;
	}
}
void Mul(int x,int y,int c){//区间乘
	if(block[x]==block[y]){
		for(int i=l[block[x]];i<=r[block[x]];i++)a[i]=(a[i]*mul[block[x]]+add[block[x]])%m;
		add[block[x]]=0,mul[block[x]]=1;
		for(int i=x;i<=y;i++)sum[block[x]]=(sum[block[x]]-a[i]+(a[i]=(a[i]*c)%m))%m;
	}
	else{
		for(int i=block[x]+1;i<=block[y]-1;i++)add[i]=(add[i]*c)%m,mul[i]=(mul[i]*c)%m,sum[i]=(sum[i]*c)%m;
		for(int i=l[block[x]];i<=r[block[x]];i++)a[i]=(a[i]*mul[block[x]]+add[block[x]])%m;
		add[block[x]]=0,mul[block[x]]=1;
		for(int i=x;i<=r[block[x]];i++)sum[block[x]]=(sum[block[x]]-a[i]+(a[i]=(a[i]*c)%m))%m;
		for(int i=l[block[y]];i<=r[block[y]];i++)a[i]=(a[i]*mul[block[y]]+add[block[y]])%m;
		add[block[y]]=0,mul[block[y]]=1;
		for(int i=l[block[y]];i<=y;i++)sum[block[y]]=(sum[block[y]]-a[i]+(a[i]=(a[i]*c)%m))%m;
	}
}
int Sum(int x,int y){//区间求和
	int ans=0;
	if(block[x]==block[y])
		for(int i=x;i<=y;i++)ans=(ans+a[i]*mul[block[x]]+add[block[x]])%m;
	else{
		for(int i=block[x]+1;i<=block[y]-1;i++)ans=(ans+sum[i])%m;
		for(int i=x;i<=r[block[x]];i++)ans=(ans+a[i]*mul[block[x]]+add[block[x]])%m;
		for(int i=l[block[y]];i<=y;i++)ans=(ans+a[i]*mul[block[y]]+add[block[y]])%m;
	}
	return ans;
}
#define endl '\n'
signed main(){
	std::ios::sync_with_stdio(false);
	cin.tie(0),cout.tie(0);
	cin>>n>>m;
	for(int i=1;i<=n;i++){
		cin>>a[i];
		block[i]=i/blk+(i%blk!=0);	
		if(!l[block[i]])l[block[i]]=i;
		r[block[i]]=i;
		sum[block[i]]+=a[i],mul[block[i]]=1;
	}
	int q; cin>>q;
	while(q--){
		int op,x,y,c; cin>>op>>x>>y;
		if(op==1)cin>>c,Mul(x,y,c);
		if(op==2)cin>>c,Add(x,y,c);
		if(op==3)cout<<Sum(x,y)<<endl;
	}
}

你可能感兴趣的:(数据结构,算法)