-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharithmeticprog.cpp
More file actions
234 lines (191 loc) · 5.2 KB
/
Copy patharithmeticprog.cpp
File metadata and controls
234 lines (191 loc) · 5.2 KB
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
#include <bits/stdc++.h>
#define clr(x) memset((x), 0, sizeof(x))
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define mp make_pair
#define in(x) int (x); input((x));
#define x first
#define y second
typedef int itn;
#define next next12345
#define prev prev12345
#define left lefdsf232
#define right rig43783
#define x1 x12345
#define y1 y12345
using namespace std;
template<typename T>
T gcd(T x, T y) {
while (y > 0) {
x %= y;
swap(x, y);
}
return x;
}
template<class _T>
inline _T sqr(const _T &x) {
return x * x;
}
template<class _T>
inline string tostr(const _T &a) {
ostringstream os("");
os << a;
return os.str();
}
typedef long double ld;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> PII;
const long double PI = 3.1415926535897932384626433832795L;
template<typename T>
inline void input(T &a) {
static int ed;
a = 0;
while (!isdigit(ed = getchar()) && ed != '-') { }
char neg = 0;
if (ed == '-') {
neg = 1;
ed = getchar();
}
while (isdigit(ed)) {
a = 10 * a + ed - '0';
ed = getchar();
}
if (neg) a = -a;
}
template<typename T = int>
inline T nxt() {
T res;
input(res);
return res;
}
mt19937 generator;
long long mod = 1000000007;
long long half = (mod + 1) / 2;
const int N = 1 << 20;
long long add[N], mul[N], sq[N];
int sz;
void Update(int r, long long add_add, long long mul_add, long long sq_add) {
for (; r < sz; r |= r + 1) {
add[r] += add_add;
add[r] %= mod;
mul[r] += mul_add;
mul[r] %= mod;
sq[r] += sq_add;
sq[r] %= mod;
}
}
long long get(long long r) {
long long R = r;
long long res_add = 0, res_mul = 0, res_sq = 0;
for (; r >= 0; r = (r & (r + 1)) - 1) {
res_add += add[r];
res_mul += mul[r];
res_sq += sq[r];
}
res_add %= mod;
res_mul %= mod;
res_sq %= mod;
return (res_add + res_mul * R % mod + res_sq * R % mod * (R - 1) % mod * half % mod) % mod;
}
void update(int l, int r, long long A, long long B) {
if (l > r) return;
Update(l, (-B * (l - 1) % mod + A * l % mod * (l - 1) % mod * half % mod) % mod, (B - A * (l - 1) % mod) % mod,
A % mod);
Update(r, (B * r % mod + A * r % mod * (r + 1) % mod * half % mod - A * r % mod * l % mod) % mod,
(-B + A * (l - 1)) % mod, (-A) % mod);
}
long long get(int l, int r) {
return ((get(r) - get(l - 1)) % mod + mod) % mod;
}
long long a[N];
void update2(int l, int r, long long mul, long long add) {
long long cur = 0;
for (int i = l; i <= r; ++i) {
a[i] += add + cur;
a[i] %= mod;
cur += mul;
cur %= mod;
}
}
long long get2(int l, int r) {
long long res = 0;
for (int i = l; i <= r; ++i)
res += a[i];
return (res % mod + mod) % mod;
}
int n, m, q;
int pos(int x, int y) {
return y - x + n - 1;
}
int main() {
//#define int long
#ifdef LOCAL
freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
#else
#define fname "race"
//freopen(fname".in", "r", stdin);
//freopen(fname".out", "w", stdout);
#endif
n = nxt(), m = nxt(), q = nxt();
sz = pos(0, m - 1) + 1;
char buf[10];
while (q--) {
scanf("%s", buf);
if (buf[1] == 'c') {
int col = nxt() - 1;
long long delta = nxt();
int dmin = pos(n - 1, col);
int dmax = pos(0, col);
update(dmin, dmax, -delta, delta * n % mod);
}
if (buf[1] == 'r') {
int row = nxt() - 1;
long long delta = nxt();
int dmin = pos(row, 0);
int dmax = pos(row, m - 1);
update(dmin, dmax, delta, delta);
}
if (buf[1] == 's') {
int r1 = nxt() - 1, c1 = nxt() - 1;
int r2 = nxt() - 1, c2 = nxt() - 1;
long long delta = nxt();
if ((r1 == r2 || c1 == c2)) {
int d1 = pos(r1, c1);
int d2 = pos(r2, c2);
if (d1 > d2) {
swap(d1, d2);
}
update(d1, d2, 0, delta);
continue;
}
int width = min(r2 - r1 + 1, c2 - c1 + 1);
int d1 = pos(r2, c1);
int d2 = pos(r1, c1);
int d3 = pos(r2, c2);
int d4 = pos(r1, c2);
if (d2 > d3) {
swap(d2, d3);
}
assert(d1 <= d2 && d2 <= d3 && d3 <= d4);
//cerr << r1 << " " << c1 << " " << r2 << " " << c2 << "\n";
//cerr << d1 << " " << d2 << " " << d3 << " " << d4 << "\n";
//cerr << width << "\n";
assert(d2 - d1 + 1 == width);
assert(d4 - d3 + 1 == width);
update(d1, d2 - 1, delta, delta);
update(d2, d3 - 1, 0, delta * width % mod);
update(d3, d4, (-delta) % mod, delta * width % mod);
}
if (buf[1] == 'd') {
int dmin = nxt() - 1;
int dmax = nxt() - 1;
cout << get(dmin, dmax) << "\n";
}
}
#ifdef LOCAL
cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC * 1000 << " ms." << endl;
#endif
return 0;
}