练下线段树,pushdown不把加和乘分开做就A了。。。就这被卡了好一会。虽然不造为什么,但是算了0.0

 1 #include<bits/stdc++.h>
 2 #define inc(i,l,r) for(i=l;i<=r;i++)
 3 #define dec(i,l,r) for(i=l;i>=r;i--)
 4 #define mem(a) memset(a,0.sizeof(a))
 5 #define NM 400000+5
 6 #define cheng(a,b) (long long)(a*b)%inf
 7 #define jia(a,b) (long long)(a+b)%inf
 8 using namespace std;
 9 int n,i,x,y,m;
10 long long inf,a[NM],b[NM],c[NM],ad[NM],z[NM],k;
11 void pushdown(int i){
12     if(z[i]!=1||ad[i]!=0){
13         a[i*2]=cheng(a[i*2],z[i]);
14         a[i*2+1]=cheng(a[i*2+1],z[i]);
15         z[i*2]=cheng(z[i*2],z[i]);
16         z[i*2+1]=cheng(z[i*2+1],z[i]);
17         ad[i*2]=cheng(ad[i*2],z[i]);ad[i*2+1]=cheng(z[i],ad[i*2+1]);
18         a[i*2]=jia(a[i*2],ad[i]*(c[i*2]-b[i*2]+1));
19         a[i*2+1]=jia(a[i*2+1],ad[i]*(c[i*2+1]-b[i*2+1]+1));
20         ad[i*2]=jia(ad[i*2],ad[i]);ad[i*2+1]=jia(ad[i*2+1],ad[i]);
21         ad[i]=0;
22         z[i]=1;ad[i]=0;
23     }
24 }
25 void build(int x,int y,int i){
26     int t=x+y>>1;
27     b[i]=x;c[i]=y;z[i]=1;
28     if(x==y){
29         scanf("%lld",&a[i]);
30         return;
31     }
32     build(x,t,i*2);build(t+1,y,i*2+1);
33     a[i]=jia(a[i*2],a[i*2+1]);
34 }
35 void ch(int x,int y,int i){
36     int t=b[i]+c[i]>>1;
37     if(x==b[i]&&y==c[i]){
38         a[i]=cheng(a[i],k);
39         ad[i]=cheng(ad[i],k);
40         z[i]=cheng(z[i],k);
41         return;
42     }
43     pushdown(i);
44     if(y<=t)ch(x,y,i*2);
45     else if(t<x)ch(x,y,i*2+1);
46     else{
47         ch(x,t,i*2);ch(t+1,y,i*2+1);
48     }
49     a[i]=jia(a[i*2],a[i*2+1]);
50 }
51 void add(int x,int y,int i){
52     int t=b[i]+c[i]>>1;
53     a[i]=jia((y-x+1)*k,a[i]);
54     if(x==b[i]&&y==c[i]){
55         ad[i]=jia(ad[i],k);
56         return ;
57     }
58     pushdown(i);
59     if(y<=t)add(x,y,i*2);
60     else if(t<x)add(x,y,i*2+1);
61     else{
62         add(x,t,i*2);add(t+1,y,i*2+1);
63     }
64 }
65 long long sum(int x,int y,int i){
66     int t=b[i]+c[i]>>1;
67     if(x==b[i]&&y==c[i])return a[i];
68     pushdown(i);
69     if(y<=t)return sum(x,y,i*2);
70     else if(t<x) return sum(x,y,i*2+1);
71     else return jia(sum(x,t,i*2),sum(t+1,y,i*2+1));
72 }
73 int main(){
74     scanf("%d%lld",&n,&inf);
75     build(1,n,1);
76     scanf("%d",&m);
77     inc(i,1,m){
78         scanf("%d",&x);
79         if(x==1){
80             scanf("%d%d%lld",&x,&y,&k);
81             ch(x,y,1);
82         }else if(x==2){
83             scanf("%d%d%lld",&x,&y,&k);
84             add(x,y,1);
85         }else{
86             scanf("%d%d",&x,&y);
87             printf("%lld\n",sum(x,y,1));
88         }
89     }
90     return 0;
91 }
View Code

 

posted on 2015-08-10 18:04  onlyRP  阅读(124)  评论(0编辑  收藏  举报