-
Notifications
You must be signed in to change notification settings - Fork 1
/
ADDMUL.cpp
167 lines (132 loc) · 3.49 KB
/
ADDMUL.cpp
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#include <bits/stdc++.h>
#define ll long long
#define get(a) scanf("%lld", &a);
#define repVector(v) for( auto it = v.begin(); it != v.end(); it++ )
#define all(c) c.begin(), c.end()
#define pb push_back
#define FOR(i,a,b) for( ll i = a; i <= b; i++ )
#define ROF(i,a,b) for( ll i = a; i >= b; i-- )
#define debug(x) cout << "[DEBUG] " << #x << " = " << x << endl
#define matrix vector< vector<ll> >
#define F first
#define S second
#define mp make_pair
#define L 2*r
#define R 2*r+1
#define INPFILE freopen("input.in","r",stdin)
#define BOOST ios_base::sync_with_stdio(false); cin.tie(NULL);
using namespace std;
#define MOD 1000000007
struct node {
ll sum;
ll l, r;
ll lazyadd;
ll lazymul;
}tree[8*(100005)];
ll v[100005] = {0};
void display( ll r ) {
node &p = tree[r];
printf("R: %lld [ %lld, %lld ] = %lld { %lld, %lld }\n", r, p.l, p.r, p.sum, p.lazyadd, p.lazymul );
}
void construct( pair<ll,ll> p, ll r = 1 ) {
node &ptr = tree[r];
ptr.l = p.F; ptr.r = p.S;
ptr.lazyadd = 0; ptr.lazymul = 1;
if( p.F == p.S )
ptr.sum = v[p.F];
else {
ll mid = ( p.F + p.S )/2;
construct( mp(p.F,mid), L );
construct( mp(mid+1,p.S), R );
ptr.sum = ( tree[L].sum + tree[R].sum )%MOD;
}
display(r);
}
void lazyme( ll r ) {
node &ptr = tree[r];
if( ptr.lazymul != 1 ) {
ll val = ptr.lazymul;
ptr.sum = ( ptr.sum*val )%MOD;
tree[L].lazymul = ( tree[L].lazymul*val )%MOD;
tree[L].lazyadd = ( tree[L].lazyadd*val )%MOD;
tree[R].lazymul = ( tree[R].lazymul*val )%MOD;
tree[R].lazyadd = ( tree[R].lazyadd*val )%MOD;
ptr.lazymul = 1;
}
if( ptr.lazyadd ) {
ll val = ptr.lazyadd;
ptr.sum = ( ptr.sum + ( ptr.r - ptr.l + 1 )*val )%MOD;
tree[L].lazyadd = ( tree[L].lazyadd + val )%MOD;
tree[R].lazyadd = ( tree[R].lazyadd + val )%MOD;
ptr.lazyadd = 0;
}
}
void add( ll x, ll y, ll val, ll r = 1 ) {
node &ptr = tree[r];
lazyme(r);
if( ptr.l > y || ptr.r < x )
return;
if( ptr.l >= x && ptr.r <= y ) {
ptr.sum = ( ptr.sum + ( ptr.r - ptr.l + 1 )*val )%MOD;
tree[L].lazyadd = ( tree[L].lazyadd + val )%MOD;
tree[R].lazyadd = ( tree[R].lazyadd + val )%MOD;
}
else {
add( x, y, val, L );
add( x, y, val, R );
ptr.sum = ( tree[L].sum + tree[R].sum )%MOD;
}
// cout << "ADD: "; display(r);
}
void mul( ll x, ll y, ll val, ll r = 1 ) {
node &ptr = tree[r];
lazyme(r);
if( ptr.l > y || ptr.r < x )
return;
if( ptr.l >= x && ptr.r <= y ) {
ptr.sum = ( val*ptr.sum )%MOD;
tree[L].lazymul = ( tree[L].lazymul*val )%MOD;
tree[L].lazyadd = ( tree[L].lazyadd*val )%MOD;
tree[R].lazymul = ( tree[R].lazymul*val )%MOD;
tree[R].lazyadd = ( tree[R].lazyadd*val )%MOD;
}
else {
mul( x, y, val, L );
mul( x, y, val, R );
ptr.sum = ( tree[L].sum + tree[R].sum )%MOD;
}
// cout << "MUL: "; display(r);
}
node query( ll x, ll y, ll r = 1 ) {
node &ptr = tree[r];
lazyme(r);
ll mid = ( ptr.l + ptr.r )/2;
if( ptr.l >= x && ptr.r <= y )
return tree[r];
if( mid < x )
return query( x, y, R );
if( mid >= y )
return query( x, y, L );
node p;
node ql = query( x, y, L );
node qr = query( x, y, R );
p.sum = ( ql.sum + qr.sum )%MOD;
p.l = ql.l; p.r = qr.r;
return p;
}
int main() {
ll n, q;
get(n); get(q);
FOR(i,1,n)
get(v[i]);
construct( mp(1,n) );
while(q--) {
ll w, x, y, v;
get(w); get(x); get(y);
if( w != 4 ) get(v);
if( w == 1 ) add( x, y, v );
else if( w == 2 ) mul( x, y, v );
else if( w == 3 ) { mul( x, y, 0 ); add( x, y, v ); }
else if( w == 4 ) printf("%lld\n", query(x,y).sum);
}
}