#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);
}
assert(n <= m);
vector<vector<ll>> radiation(n + 2, vector<ll> (m + 2, 0)), v(n + 2, vector<ll> (m + 2, 0)), jmen(n + 2, vector<ll> (m + 2, 0));
function<void(int, int, int, int, int)> proc = [&] (int dist, int r, int c, int a, int b) {
int stop = min(max(m - c, c - 1), a / b);
assert(a - dist * b >= 1);
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);
assert(L <= R);
jmen[r][L] += (a + 1LL * b * c);
jmen[r][R + 1] -= (a + 1LL * b * c);
for (int c2 = L; c2 <= R; c2++) {
v[r][c2] -= 1LL * b * c2;
}
}
if (dist + 1 <= min(stop, c - 1)) {
L = max(c - stop, 1);
R = c - dist - 1;
assert(L <= R);
jmen[r][L] += (a - 1LL * b * c);
jmen[r][R + 1] -= (a - 1LL * b * c);
for (int c2 = L; c2 <= R; c2++) {
v[r][c2] += 1LL * b * c2;
}
}
}
};
int ops;
cin >> ops;
for (int i = 1; i <= ops; i++) {
int r, c, a, b;
cin >> r >> c >> a >> b;
if (Inverse) {
swap(r, c);
}
for (int r2 = 1; r2 <= n; r2++) {
for (int c2 = 1; c2 <= m; c2++) {
int dist = max(abs(r - r2), abs(c - c2));
radiation[r2][c2] += max(0LL, a - 1LL * b * dist);
}
}
for (int dist = 0; (r + dist <= n || r - dist >= 1) && a - dist * b >= 1; dist++) {
if (r + dist <= n) {
proc(dist, r + dist, c, a, b);
}
if (r - dist >= 1 && dist >= 1) {
proc(dist, r - dist, c, a, b);
}
}
}
for (int r = 1; r <= n; r++) {
for (int c = 1; c <= m; c++) {
jmen[r][c] += jmen[r][c - 1];
v[r][c] += jmen[r][c];
}
}
assert(v == radiation);
{
/// transform the radiation in the form of prefix rectangle sum
for (int i = 1; i <= n; i++) {
ll cur = 0;
for (int j = 1; j <= m; j++) {
cur += radiation[i][j];
radiation[i][j] = radiation[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 = radiation[r2][c2] - radiation[r1 - 1][c2] - radiation[r2][c1 - 1] + radiation[r1 - 1][c1 - 1];
ll area = (r2 - r1 + 1) * (c2 - c1 + 1);
cout << total / area + (total % area >= area - total % area) << "\n";
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
397 ms |
195976 KB |
Output is correct |
2 |
Correct |
57 ms |
2668 KB |
Output is correct |
3 |
Correct |
51 ms |
2204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
383 ms |
195952 KB |
Output is correct |
2 |
Correct |
57 ms |
2764 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
332 ms |
59368 KB |
Output is correct |
2 |
Correct |
58 ms |
2636 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
286 ms |
65920 KB |
Output is correct |
2 |
Correct |
79 ms |
2668 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
277 ms |
198196 KB |
Output is correct |
2 |
Correct |
61 ms |
2940 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
403 ms |
81032 KB |
Output is correct |
2 |
Correct |
56 ms |
2656 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
433 ms |
61772 KB |
Output is correct |
2 |
Correct |
60 ms |
2940 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
381 ms |
53628 KB |
Output is correct |
2 |
Correct |
59 ms |
2708 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1099 ms |
195924 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1056 ms |
195932 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1073 ms |
59340 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1092 ms |
59212 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1082 ms |
60596 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1080 ms |
59328 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |