#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 + 1, vector<ll> (m + 1, 0)), v(n + 1, vector<ll> (m + 1, 0));
function<void(int, int, int, int, int)> proc = [&] (int dist, int r2, int c, int a, int b) {
for (int c2 = 1; c2 <= m; c2++) {
int dist_max = max(dist, abs(c - c2));
v[r2][c2] += max(0LL, a - 1LL * b * dist_max);
}
};
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; 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);
}
}
}
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 |
378 ms |
98152 KB |
Output is correct |
2 |
Correct |
62 ms |
2612 KB |
Output is correct |
3 |
Correct |
57 ms |
2252 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
412 ms |
98156 KB |
Output is correct |
2 |
Correct |
65 ms |
2764 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
352 ms |
39508 KB |
Output is correct |
2 |
Correct |
60 ms |
2596 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
354 ms |
42464 KB |
Output is correct |
2 |
Correct |
55 ms |
2616 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
261 ms |
100300 KB |
Output is correct |
2 |
Correct |
64 ms |
2864 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
540 ms |
41756 KB |
Output is correct |
2 |
Correct |
55 ms |
2600 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
484 ms |
41896 KB |
Output is correct |
2 |
Correct |
62 ms |
2892 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
476 ms |
30040 KB |
Output is correct |
2 |
Correct |
64 ms |
2648 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1084 ms |
98088 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1075 ms |
98148 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1097 ms |
39508 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1099 ms |
39508 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1101 ms |
40412 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1088 ms |
39556 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |