模板题,找不出错误
提交记录https://www.luogu.com.cn/record/167626159
附上一百行代码:
#include<bits/stdc++.h>
#define int long long
#define N 100005
using namespace std;
int n,m,q;
int opt,x,y,k;
int a[N],mid;
struct node{
int ans;
int lazy_plus;
int lazy_times;
}p[4*N];
void build_the_tree(int root,int left_side,int right_side){
p[root].lazy_plus=0;
p[root].lazy_times=1;
if(left_side==right_side)p[root].ans=a[left_side];
else{
mid=(left_side+right_side)/2;
build_the_tree(root*2,left_side,mid);
build_the_tree(root*2+1,mid+1,right_side);
p[root].ans=(p[root*2].ans+p[root*2+1].ans)%q;
}
}
void pass_the_tree(int root,int left_side,int right_side){
mid=(left_side+right_side)/2;
p[root*2].ans=(p[root*2].ans*p[root].lazy_times%q+p[root].lazy_plus*(mid-left_side+1)%q)%q;
p[root*2].lazy_plus=(p[root*2].lazy_plus*p[root].lazy_times%q+p[root].lazy_plus)%q;
p[root*2].lazy_times=p[root*2].lazy_times*p[root].lazy_times%q;
p[root*2+1].ans=(p[root*2+1].ans*p[root].lazy_times%q+p[root].lazy_plus*(right_side-mid)%q)%q;
p[root*2+1].lazy_plus=(p[root*2+1].lazy_plus*p[root].lazy_times%q+p[root].lazy_plus)%q;
p[root*2+1].lazy_times=p[root*2+1].lazy_times*p[root].lazy_times%q;
p[root].lazy_plus=0;
p[root].lazy_times=1;
}
void time_the_tree(int root,int left_now,int right_now,int left_should,int right_should,int value){
if(left_now>right_should||right_now<left_should);
else if(left_now>=left_should&&right_now<=right_should){
p[root].ans=p[root].ans*value%q;
p[root].lazy_plus=p[root].lazy_plus*value%q;
p[root].lazy_times=p[root].lazy_times*value%q;
}
else{
pass_the_tree(root,left_now,right_now);
mid=(left_now+right_now)/2;
time_the_tree(root*2,left_now,mid,left_should,right_should,value);
time_the_tree(root*2+1,mid+1,right_now,left_should,right_should,value);
p[root].ans=(p[root*2].ans+p[root*2+1].ans)%q;
}
return;
}
void plus_the_tree(int root,int left_now,int right_now,int left_should,int right_should,int value){
if(left_now>right_should||right_now<left_should);
else if(left_now>=left_should&&right_now<=right_should){
p[root].ans=(p[root].ans+value*(right_now-left_now+1))%q;
p[root].lazy_plus=(p[root].lazy_plus+value)%q;
}
else{
pass_the_tree(root,left_now,right_now);
mid=(left_now+right_now)/2;
plus_the_tree(root*2,left_now,mid,left_should,right_should,value);
plus_the_tree(root*2+1,mid+1,right_now,left_should,right_should,value);
p[root].ans=(p[root*2].ans+p[root*2+1].ans)%q;
}
return;
}
int print_the_tree(int root,int left_now,int right_now,int left_should,int right_should){
if(left_now>right_should||right_now<left_should)return 0;
else if(left_now>=left_should&&right_now<=right_should)return p[root].ans;
else{
pass_the_tree(root,left_now,right_now);
mid=(left_now+right_now)/2;
return(print_the_tree(root*2,left_now,mid,left_should,right_should)+print_the_tree(root*2+1,mid+1,right_now,left_should,right_should))%q;
}
}
signed main(){
scanf("%lld %lld %lld",&n,&m,&q);
for(int i=1;i<=n;i++)scanf("%lld",&a[i]);
build_the_tree(1,1,n);
while(m--){
scanf("%lld",&opt);
switch(opt){
case 1:
scanf("%lld %lld %lld",&x,&y,&k);
time_the_tree(1,1,n,x,y,k);
break;
case 2:
scanf("%lld %lld %lld",&x,&y,&k);
plus_the_tree(1,1,n,x,y,k);
break;
case 3:
scanf("%lld %lld",&x,&y);
printf("%lld\n",print_the_tree(1,1,n,x,y));
break;
}
}
return 0;
}