//#define _GLIBCXX_DEBUG
#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 = 200001, M = 2500002;
int X[N], Y[N], A[N], B[N], n, m, k;
ll **UL;
ll **UR;
ll **DL;
ll **DR;
ll **pref;
ll L[M], U[M];
void addDL(int x, int y, int len, ll val) {
UR[x][y] += val;
if (x + len - 1 <= n && y - len + 1 >= 1) {
UR[x + len][y - len] -= val;
} else {
int mn = min(n - x + 1, y) - 1;
x += mn, y -= mn;
UR[x + 1][y - 1] -= val;
len -= mn;
if (y == 1) {
U[x + 1] += val;
if (x + len <= n) {
U[x + len] -= val;
}
}
}
};
void addDR(int x, int y, int len, ll val) {
UL[x][y] += val;
if (x + len - 1 <= n && y + len - 1 <= m) {
UL[x + len][y + len] -= val;
}
};
void addUL(int x, int y, int len, ll val) {
pref[1][1] += val * max(0, len - max(x, y));
DR[x][y] += val;
if (x - len + 1 >= 1 && y - len + 1 >= 1) {
DR[x - len][y - len] -= val;
} else {
int mn = min(x, y) - 1;
x -= mn, y -= mn;
DR[x - 1][y - 1] -= val;
len -= mn;
if (y == 1) {
U[x] -= val;
U[max(1, x - len + 1)] += val;
} else if (x == 1) {
L[y] -= val;
L[max(1, y - len + 1)] += val;
}
}
};
void addUR(int x, int y, int len, ll val) {
DL[x][y] += val;
if (x - len + 1 >= 1 && y + len - 1 <= m) {
DL[x - len][y + len] -= val;
} else {
int mn = min(x, m - y + 1) - 1;
x -= mn, y += mn;
DL[x - 1][y + 1] -= val;
len -= mn;
if (x == 1) {
L[y + 1] += val;
if (y + len <= m) {
L[y + len] -= val;
}
}
}
};
void addRect(int x1, int y1, int x2, int y2, ll val) {
x1 = max(x1, 1), y1 = max(y1, 1), y2 = min(y2, m), x2 = min(x2, n);
pref[x1][y1] += val;
pref[x2 + 1][y2 + 1] += val;
pref[x1][y2 + 1] -= val;
pref[x2 + 1][y1] -= val;
};
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n >> m >> k;
for (int i = 1; i <= k; ++i) {
cin >> X[i] >> Y[i] >> A[i] >> B[i];
}
pref = new ll *[n + 2];
UL = new ll *[n + 2];
UR = new ll *[n + 2];
DL = new ll *[n + 2];
DR = new ll *[n + 2];
for (int i = 0; i <= n + 1; i++) {
pref[i] = new ll[m + 2];
UL[i] = new ll[m + 2];
UR[i] = new ll[m + 2];
DL[i] = new ll[m + 2];
DR[i] = new ll[m + 2];
for (int j = 0; j <= m + 1; j++) {
pref[i][j] = 0;
UL[i][j] = 0;
UR[i][j] = 0;
DL[i][j] = 0;
DR[i][j] = 0;
}
}
for (int i = 1; i <= k; ++i) {
int h = A[i] / B[i];
addRect(X[i] - h, Y[i] - h, X[i] + h, Y[i] + h, A[i] % B[i]);
addUR(X[i], Y[i] + 1, h, -B[i]);
addUL(X[i], Y[i], h, B[i]);
addDR(X[i] + 1, Y[i] + 1, h, B[i]);
addDL(X[i] + 1, Y[i], h, -B[i]);
}
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= m; ++j) {
UR[i][j] += UR[i - 1][j + 1];
UL[i][j] += UL[i - 1][j - 1];
}
}
for (int i = n; i >= 1; --i) {
for (int j = 1; j <= m; ++j) {
DL[i][j] += DL[i + 1][j - 1];
DR[i][j] += DR[i + 1][j + 1];
}
}
for (int i = 1; i <= m; ++i) {
L[i] += L[i - 1];
pref[1][i] += L[i];
}
for (int i = 1; i <= n; ++i) {
U[i] += U[i - 1];
pref[i][1] += U[i];
}
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= m; ++j) {
pref[i][j] += DR[i][j] + DL[i][j] + UR[i][j] + UL[i][j];
pref[i][j] = pref[i][j] + pref[i - 1][j] + pref[i][j - 1] - pref[i - 1][j - 1];
}
}
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= m; ++j) {
pref[i][j] = pref[i][j] + pref[i - 1][j] + pref[i][j - 1] - pref[i - 1][j - 1];
}
}
int q;
cin >> q;
for (int i = 1; i <= q; ++i) {
int x1, y1, x2, y2;
cin >> x1 >> y1 >> x2 >> y2;
ll sum = pref[x2][y2] - pref[x1 - 1][y2] - pref[x2][y1 - 1] + pref[x1 - 1][y1 - 1];
ll S = (x2 - x1 + 1) * (ll) (y2 - y1 + 1);
cout << ((sum * 2 / S + 1) >> 1ll) << '\n';
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
625 ms |
509056 KB |
Output is correct |
2 |
Correct |
54 ms |
2764 KB |
Output is correct |
3 |
Correct |
51 ms |
2304 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
627 ms |
509316 KB |
Output is correct |
2 |
Correct |
54 ms |
2776 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
70 ms |
99252 KB |
Output is correct |
2 |
Correct |
55 ms |
2768 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
68 ms |
109336 KB |
Output is correct |
2 |
Correct |
53 ms |
2676 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
741 ms |
511380 KB |
Output is correct |
2 |
Correct |
58 ms |
3148 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
355 ms |
206316 KB |
Output is correct |
2 |
Correct |
54 ms |
2776 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
153 ms |
101064 KB |
Output is correct |
2 |
Correct |
56 ms |
2948 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
263 ms |
143680 KB |
Output is correct |
2 |
Correct |
53 ms |
2756 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
912 ms |
514704 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
904 ms |
514720 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
259 ms |
104272 KB |
Output is correct |
2 |
Correct |
257 ms |
103908 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
260 ms |
104020 KB |
Output is correct |
2 |
Correct |
253 ms |
103732 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
243 ms |
107776 KB |
Output is correct |
2 |
Correct |
262 ms |
105652 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
258 ms |
104808 KB |
Output is correct |
2 |
Correct |
333 ms |
318956 KB |
Output is correct |