#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
signed main() {
ios::sync_with_stdio(0); cin.tie(0);
/// freopen ("input.txt", "r", stdin);
bool Inverse = 0;
int n, m;
cin >> n >> m;
if (n > m) {
Inverse = 1;
swap(n, m);
}
vector<vector<ll>> deri(n + 2, vector<ll> (m + 2, 0)),
jmen(n + 2, vector<ll> (m + 2, 0)),
difs(n + 2, vector<ll> (m + 2, 0)),
v(n + 2, vector<ll> (m + 2, 0));
int ops;
cin >> ops;
for (int i = 1; i <= ops; i++) {
int r1, c, a, b;
cin >> r1 >> c >> a >> b;
if (Inverse) {
swap(r1, c);
}
for (int dist = 0; (r1 + dist <= n || r1 - dist >= 1) && a - dist * b >= 1; dist++) {
if (r1 + dist <= n) {
int r = r1 + dist;
int stop = min(max(m - c, c - 1), a / b);
int L = max(1, c - dist), R = min(m, c + dist);
jmen[r][L] += (a - dist * b);
jmen[r][R + 1] -= (a - dist * b);
if (dist + 1 <= stop) {
if (dist + 1 <= min(stop, m - c)) {
L = c + dist + 1;
R = min(c + stop, m);
jmen[r][L] += (a + 1LL * b * c);
jmen[r][R + 1] -= (a + 1LL * b * c);
difs[r][L] -= 1LL * b * L;
deri[r][L + 1] -= b;
deri[r][R + 1] += b;
difs[r][R + 1] += 1LL * b * R;
}
if (dist + 1 <= min(stop, c - 1)) {
L = max(c - stop, 1);
R = c - dist - 1;
jmen[r][L] += (a - 1LL * b * c);
jmen[r][R + 1] -= (a - 1LL * b * c);
difs[r][L] += 1LL * b * L;
deri[r][L + 1] += b;
deri[r][R + 1] -= b;
difs[r][R + 1] -= 1LL * b * R;
}
}
}
if (r1 - dist >= 1 && dist >= 1) {
int r = r1 - dist;
int stop = min(max(m - c, c - 1), a / b);
int L = max(1, c - dist), R = min(m, c + dist);
jmen[r][L] += (a - dist * b);
jmen[r][R + 1] -= (a - dist * b);
if (dist + 1 <= stop) {
if (dist + 1 <= min(stop, m - c)) {
L = c + dist + 1;
R = min(c + stop, m);
jmen[r][L] += (a + 1LL * b * c);
jmen[r][R + 1] -= (a + 1LL * b * c);
difs[r][L] -= 1LL * b * L;
deri[r][L + 1] -= b;
deri[r][R + 1] += b;
difs[r][R + 1] += 1LL * b * R;
}
if (dist + 1 <= min(stop, c - 1)) {
L = max(c - stop, 1);
R = c - dist - 1;
jmen[r][L] += (a - 1LL * b * c);
jmen[r][R + 1] -= (a - 1LL * b * c);
difs[r][L] += 1LL * b * L;
deri[r][L + 1] += b;
deri[r][R + 1] -= b;
difs[r][R + 1] -= 1LL * b * R;
}
}
}
}
}
for (int r = 1; r <= n; r++) {
for (int c = 1; c <= m; c++) {
deri[r][c] += deri[r][c - 1];
difs[r][c] += deri[r][c];
difs[r][c] += difs[r][c - 1];
jmen[r][c] += jmen[r][c - 1];
v[r][c] += jmen[r][c];
v[r][c] += difs[r][c];
}
}
{
for (int i = 1; i <= n; i++) {
ll cur = 0;
for (int j = 1; j <= m; j++) {
cur += v[i][j];
v[i][j] = v[i - 1][j] + cur;
}
}
}
int q;
cin >> q;
while (q--) {
int r1, c1, r2, c2;
cin >> r1 >> c1 >> r2 >> c2;
if (Inverse) {
swap(r1, c1);
swap(r2, c2);
}
ll total = v[r2][c2] - v[r1 - 1][c2] - v[r2][c1 - 1] + v[r1 - 1][c1 - 1];
ll area = (r2 - r1 + 1) * (c2 - c1 + 1);
cout << total / area + (total % area >= area - total % area) << "\n";
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
140 ms |
254736 KB |
Output is correct |
2 |
Correct |
59 ms |
2604 KB |
Output is correct |
3 |
Correct |
56 ms |
2316 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
140 ms |
254800 KB |
Output is correct |
2 |
Correct |
59 ms |
2704 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
45 ms |
78924 KB |
Output is correct |
2 |
Correct |
59 ms |
2636 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
49 ms |
87404 KB |
Output is correct |
2 |
Correct |
63 ms |
2636 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
252 ms |
256884 KB |
Output is correct |
2 |
Correct |
71 ms |
2980 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
153 ms |
104452 KB |
Output is correct |
2 |
Correct |
66 ms |
2620 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
145 ms |
81356 KB |
Output is correct |
2 |
Correct |
59 ms |
2912 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
133 ms |
69264 KB |
Output is correct |
2 |
Correct |
57 ms |
2708 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
343 ms |
257016 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
320 ms |
257028 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1086 ms |
79060 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1095 ms |
79100 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1088 ms |
80632 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1092 ms |
79004 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |