//#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
#pragma GCC optimize ("O3,Ofast,fast-math,unroll-loops")
#pragma GCC target ("avx2,tune=native")
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;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, m, k;
cin >> n >> m >> k;
vector<int> x(k + 1), y(k + 1);
vector<ll> a(k + 1), b(k + 1);
for (int i = 1; i <= k; ++i) {
cin >> x[i] >> y[i] >> a[i] >> b[i];
}
vector<vector<ll>>
DL(n + 2, vector<ll>(m + 2)), pref(n + 2, vector<ll>(m + 2)), DR(n + 2, vector<ll>(m + 2)),
UR(n + 2, vector<ll>(m + 2)), UL(n + 2, vector<ll>(m + 2));
vector<ll> L(m + 2), R(m + 2), U(n + 2), D(n + 2);
auto 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;
}
}
}
};
auto 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;
}
};
auto 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) {
D[x - 1] += val;
if (x - len >= 1) {
D[x - len] -= val;
}
} else if (x == 1) {
R[y - 1] += val;
if (y - len >= 1) {
R[y - len] -= val;
}
}
}
};
auto 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;
}
}
}
};
auto 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;
};
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 = m; i >= 1; --i) {
R[i] += R[i + 1];
pref[1][i] += R[i];
}
for (int i = 1; i <= n; ++i) {
U[i] += U[i - 1];
pref[i][1] += U[i];
}
for (int i = n; i >= 1; --i) {
D[i] += D[i + 1];
pref[i][1] += D[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) / 2 << '\n';
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
886 ms |
724504 KB |
Output is correct |
2 |
Correct |
62 ms |
2724 KB |
Output is correct |
3 |
Correct |
60 ms |
2304 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
874 ms |
724332 KB |
Output is correct |
2 |
Correct |
56 ms |
2728 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
122 ms |
99536 KB |
Output is correct |
2 |
Correct |
65 ms |
2676 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
121 ms |
110068 KB |
Output is correct |
2 |
Correct |
55 ms |
2612 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
991 ms |
726488 KB |
Output is correct |
2 |
Correct |
61 ms |
3152 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
476 ms |
292264 KB |
Output is correct |
2 |
Correct |
78 ms |
2624 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
220 ms |
101128 KB |
Output is correct |
2 |
Correct |
60 ms |
2912 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
336 ms |
186640 KB |
Output is correct |
2 |
Correct |
59 ms |
2628 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1126 ms |
731408 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1127 ms |
731396 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
365 ms |
106412 KB |
Output is correct |
2 |
Correct |
353 ms |
105496 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
330 ms |
105860 KB |
Output is correct |
2 |
Correct |
334 ms |
105372 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
336 ms |
107732 KB |
Output is correct |
2 |
Correct |
355 ms |
108188 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
329 ms |
106684 KB |
Output is correct |
2 |
Correct |
479 ms |
340108 KB |
Output is correct |