#include <bits/stdc++.h>
using namespace std;
#define all(x) begin(x), end(x)
#define sz(x) ((int)size(x))
#define trace(x) cout << #x << ": " << (x) << endl;
typedef long long ll;
const int N = 200000;
int x[N], y[N], a[N], b[N];
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int nn, mm, n;
cin >> nn >> mm >> n;
for (int i = 0; i < n; ++i) {
cin >> x[i] >> y[i] >> a[i] >> b[i];
--x[i], --y[i];
}
vector<vector<ll>> v(nn, vector<ll>(mm)), pref(nn, vector<ll>(mm));
if (nn == 1) {
vector<ll> added(mm + 1), bb(mm + 1);
auto add = [&](int l, int r, int x, int y) {
if (l > r) return;
added[l] += x;
bb[l + 1] += y;
bb[r + 1] -= y * 1LL * (r - l);
added[r + 1] -= x;
};
for (int i = 0; i < n; ++i) {
int u = a[i] / b[i];
int L = max(0, y[i] - u);
add(L, y[i], a[i] - b[i] * u, b[i]);
int R = min(mm - 1, y[i] + u);
add(y[i] + 1, R, a[i] - b[i], -b[i]);
}
ll ad = 0;
ll sum = 0;
for (int i = 0; i < mm; ++i) {
sum += added[i];
ad += bb[i];
sum += ad;
v[0][i] = sum;
}
} else if (n * nn * 1LL * mm <= 100000000) {
for (int k = 0; k < n; ++k) {
for (int i = 0; i < nn; ++i) {
for (int j = 0; j < mm; ++j) {
v[i][j] += max(0ll, a[k] - b[k] * (ll) max(abs(i - x[k]), abs(j - y[k])));
}
}
}
}
for (int i = 0; i < nn; ++i) {
for (int j = 0; j < mm; ++j) {
pref[i][j] = v[i][j];
if (i) pref[i][j] += pref[i - 1][j];
if (j) pref[i][j] += pref[i][j - 1];
if (i && j) pref[i][j] -= pref[i - 1][j - 1];
}
}
auto get = [&pref](int x1, int y1, int x2, int y2) {
ll ans = pref[x2][y2];
if (x1) ans -= pref[x1 - 1][y2];
if (y1) ans -= pref[x2][y1 - 1];
if (x1 && y1) ans += pref[x1 - 1][y1 - 1];
return ans;
};
int q;
cin >> q;
for (int i = 0; i < q; ++i) {
int x1, y1, x2, y2;
cin >> x1 >> y1 >> x2 >> y2;
--x1, --y1, --x2, --y2;
ll sum = get(x1, y1, x2, y2);
ll S = (x2 - x1 + 1) * (ll) (y2 - y1 + 1);
cout << (sum * 2 / S + 1) / 2 << '\n';
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
789 ms |
274248 KB |
Output is correct |
2 |
Correct |
62 ms |
2780 KB |
Output is correct |
3 |
Incorrect |
49 ms |
820 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
787 ms |
274320 KB |
Output is correct |
2 |
Correct |
81 ms |
2700 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
193 ms |
39860 KB |
Output is correct |
2 |
Correct |
57 ms |
2740 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
197 ms |
40524 KB |
Output is correct |
2 |
Incorrect |
63 ms |
2892 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
547 ms |
276428 KB |
Output is correct |
2 |
Correct |
68 ms |
2992 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
746 ms |
112232 KB |
Output is correct |
2 |
Correct |
80 ms |
2696 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
307 ms |
41872 KB |
Output is correct |
2 |
Correct |
70 ms |
2932 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
521 ms |
57536 KB |
Output is correct |
2 |
Correct |
59 ms |
2648 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
456 ms |
277952 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
460 ms |
277992 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
183 ms |
43444 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
184 ms |
43312 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
199 ms |
43684 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
189 ms |
43432 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |