样例过了,交上去0分。 最有意思的是把第一个测试点下下来测,第一次询问有问题,第二次询问还对了。
测试点#1:
8 10 571373
5929 7152 8443 6028 8580 5449 8473 4237
2 4 8 4376
1 2 8 9637
2 2 6 7918
2 5 8 5681
3 2 8
1 1 5 6482
3 1 5
1 5 8 8701
2 5 8 7992
2 5 8 7806
正确输出:
478836
562114
我的输出:
176479
562114
我的代码:
# include <bits/stdc++.h>
using namespace std;
long long tree[400010];
long long tag1[400010],tag2[400010];
long long arr[100010];
void build(int p,int l,int r,long long mod){
tag1[p]=0;
tag2[p]=1;
if(l==r){
tree[p]=arr[l]%mod;
return;
}
int mid=(l+r)/2;
build(p*2,l,mid,mod);
build(p*2+1,mid+1,r,mod);
tree[p]=(tree[p*2]%mod+tree[p+2+1]%mod)%mod;
}
inline void push_down(int p,int l,int r,long long mod){
int mid=(l+r)/2;
tree[p*2]=(tree[p*2]%mod*tag2[p]%mod+(mid-l+1)*tag1[p]%mod)%mod;
tree[p*2+1]=(tree[p*2+1]%mod*tag2[p]%mod+(r-mid+1)*tag1[p]%mod)%mod;
tag1[p*2]=(tag1[p*2]%mod+tag1[p]%mod)%mod;
tag1[p*2+1]=(tag1[p*2+1]%mod+tag1[p]%mod)%mod;
tag2[p*2]=tag2[p*2]%mod*tag2[p]%mod;
tag2[p*2+1]=tag2[p*2+1]%mod*tag2[p]%mod;
tag1[p]=0;
tag2[p]=1;
}
void updata1(int p,int l,int r,int L,int R,long long k,long long mod){
if(r<L||R<l){
return;
}
if(L<=l&&r<=R){
tag1[p]=(k%mod+tag1[p]%mod)%mod;
tree[p]=(tree[p]%mod+k*(r-l+1)%mod)%mod;
return;
}
push_down(p,l,r,mod);
int mid=(l+r)/2;
if(L<=mid){
updata1(p*2,l,mid,L,R,k,mod);
}
if(mid<R){
updata1(p*2+1,mid+1,r,L,R,k,mod);
}
tree[p]=(tree[p*2]%mod+tree[p*2+1]%mod)%mod;
}
void updata2(int p,int l,int r,int L,int R,long long k,long long mod){
if(r<L||R<l){
return;
}
if(L<=l&&r<=R){
tag1[p]=tag1[p]%mod*k%mod;
tag2[p]=tag2[p]%mod*k%mod;
tree[p]=tree[p]%mod*k%mod;
return;
}
push_down(p,l,r,mod);
int mid=(l+r)/2;
if(L<=mid){
updata2(p*2,l,mid,L,R,k,mod);
}
if(mid<R){
updata2(p*2+1,mid+1,r,L,R,k,mod);
}
tree[p]=(tree[p*2]%mod+tree[p*2+1]%mod)%mod;
}
long long query(int p,int l,int r,int L,int R,long long mod){
if(r<L||R<l){
return 0;
}
if(L<=l&&r<=R){
return tree[p]%mod;
}
push_down(p,l,r,mod);
int mid=(l+r)/2;
long long ans=0;
if(L<=mid){
ans+=query(p*2,l,mid,L,R,mod)%mod;
ans%=mod;
}
if(mid<R){
ans+=query(p*2+1,mid+1,r,L,R,mod)%mod;
ans%=mod;
}
return ans;
}
int main(){
int n,q;
long long mod;
cin>>n>>q>>mod;
for(int i=1;i<=n;i++){
cin>>arr[i];
}
build(1,1,n,mod);
for(int i=0;i<q;i++){
int c;
cin>>c;
int x,y;
long long k;
switch(c){
case 1:{
cin>>x>>y>>k;
updata2(1,1,n,x,y,k,mod);
break;
}
case 2:{
cin>>x>>y>>k;
updata1(1,1,n,x,y,k,mod);
break;
}
case 3:{
cin>>x>>y;
cout<<query(1,1,n,x,y,mod)<<endl;
break;
}
}
}
return 0;
}