1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
|
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
const int maxn = 10005;
struct tnode{
ll w,l,r,siz,alazy;
ll sq,mlazy=1;
}tn[maxn<<2];
ll n,m;
const ll mod = 0x3f3f3f3f3f3f3f3f;
ll wt[maxn];
void pushup(ll u){
tn[u].w = (tn[u<<1].w + tn[u<<1|1].w) % mod;
tn[u].sq = (tn[u<<1].sq + tn[u<<1|1].sq) % mod;
}
void build(ll u,ll l,ll r){
tn[u].l = l;
tn[u].r = r;
tn[u].siz = r - l + 1;
if(l == r){
tn[u].w = wt[l];
tn[u].sq = wt[l] * wt[l];
return ;
}
ll mid = (l+r) >> 1;
build(u<<1 , l , mid);
build(u<<1|1 , mid+1 , r);
pushup(u);
}
void pushdown(ll u){
if(tn[u].alazy != 0 or tn[u].mlazy != 1){
tn[u<<1].sq = ((tn[u<<1].sq + 2LL * tn[u<<1].w * tn[u].alazy % mod) % mod + tn[u].alazy * tn[u].alazy % mod * tn[u<<1].siz % mod) % mod;
tn[u<<1|1].sq = ((tn[u<<1|1].sq + 2LL * tn[u<<1|1].w * tn[u].alazy % mod) % mod + tn[u].alazy * tn[u].alazy % mod * tn[u<<1|1].siz % mod) % mod;
tn[u<<1].w = (tn[u<<1].w * tn[u].mlazy % mod + tn[u<<1].siz * tn[u].alazy % mod) % mod;
tn[u<<1|1].w = (tn[u<<1|1].w * tn[u].mlazy % mod + tn[u<<1|1].siz * tn[u].alazy % mod) % mod;
tn[u<<1].alazy = (tn[u<<1].alazy * tn[u].mlazy % mod + tn[u].alazy) % mod;
tn[u<<1|1].alazy = (tn[u<<1|1].alazy * tn[u].mlazy % mod + tn[u].alazy) % mod;
tn[u<<1].mlazy = (tn[u<<1].mlazy * tn[u].mlazy) % mod;
tn[u<<1|1].mlazy = (tn[u<<1|1].mlazy * tn[u].mlazy) % mod;
tn[u].alazy = 0;
tn[u].mlazy = 1;
}
}
ll query(ll u,ll l,ll r,ll t){
if(l<=tn[u].l and r>=tn[u].r) {
if(t == 1) return tn[u].w;
if(t == 2) return tn[u].sq;
}
ll ans = 0;
pushdown(u);
ll mid = (tn[u].l + tn[u].r) >> 1;
if(l<=mid) ans = (ans + query(u<<1 , l , r , t)) % mod;
if(r>mid) ans = (ans + query(u<<1|1 , l , r , t)) % mod;
return ans;
}
void update(ll u,ll l,ll r,ll wa,ll wm){
if(l<=tn[u].l and r>=tn[u].r){
tn[u].sq = ((tn[u].sq + 2LL * tn[u].w * wa % mod) % mod + wa * wa % mod * tn[u].siz % mod) % mod;
tn[u].w = (tn[u].w * wm % mod + tn[u].siz * wa % mod) % mod;
tn[u].alazy = (tn[u].alazy * wm % mod + wa) % mod;
tn[u].mlazy = (tn[u].mlazy * wm) % mod;
return ;
}
pushdown(u);
ll mid = (tn[u].l + tn[u].r) >> 1;
if(l<=mid) update(u<<1, l , r , wa , wm);
if(r>mid) update(u<<1|1, l , r , wa , wm);
pushup(u);
}
int main(){
//freopen("in.txt","r",stdin);
cin>>n>>m;
for(ll i=1;i<=n;i++){
cin>>wt[i];
}
build(1,1,n);
while(m--){
ll t;
cin>>t;
switch(t){
case 1:{
ll x,y;
cin>>x>>y;
cout<<query(1,x,y,1)<<endl;
break;
}
case 2:{
ll x,y;
cin>>x>>y;
cout<<query(1,x,y,2)<<endl;
break;
}
case 3:{
ll x,y,z;
cin>>x>>y>>z;
update(1,x,y,0,z%mod);
break;
}
case 4:{
ll x,y,z;
cin>>x>>y>>z;
update(1,x,y,z%mod,1);
break;
}
}
}
return 0;
}
|