/**
____ ____ ____ ____ ____
||a |||t |||o |||d |||o ||
||__|||__|||__|||__|||__||
|/__\|/__\|/__\|/__\|/__\|
**/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N_MAX = 2500000;
vector <ll> mat[N_MAX + 2];
vector <ll> UL[N_MAX + 2];
vector <ll> UR[N_MAX + 2];
vector <ll> DL[N_MAX + 2];
vector <ll> DR[N_MAX + 2];
ll U[N_MAX + 2];
ll D[N_MAX + 2];
ll L[N_MAX + 2];
ll R[N_MAX + 2];
int N, M;
void addUL (int x, int y, int len, int val) {
mat[1][1] += (ll) 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;
}
}
}
}
void addUR (int x, int y, int len, int 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 addDL (int x, int y, int len, int 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, int val) {
UL[x][y] += val;
if (x + len <= N && y + len <= M) {
UL[x + len][y + len] -= val;
}
}
void add (int x, int y, int len, int val) {
int x1 = x - len + 1, y1 = y - len + 1;
int x2 = x + len - 1, y2 = y + len - 1;
x1 = max(1, x1); y1 = max(1, y1);
x2 = min(N, x2); y2 = min(M, y2);
mat[x1][y1] += val;
mat[x1][y2 + 1] -= val;
mat[x2 + 1][y1] -= val;
mat[x2 + 1][y2 + 1] += val;
}
int main () {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> N >> M;
for (int i = 0; i <= N + 1; i++) {
mat[i] = vector <ll> (M + 2, 0);
UL[i] = vector <ll> (M + 2, 0);
UR[i] = vector <ll> (M + 2, 0);
DL[i] = vector <ll> (M + 2, 0);
DR[i] = vector <ll> (M + 2, 0);
}
int K;
cin >> K;
while (K--) {
int x, y, a, b;
cin >> x >> y >> a >> b;
int len = a / b;
addUL(x, y, len, +b);
addUR(x, y + 1, len, -b);
addDL(x + 1, y, len, -b);
addDR(x + 1, y + 1, len, +b);
add(x, y, len + 1, a % b);
}
for (int i = 1; i <= N; i++) {
U[i] += U[i - 1];
mat[i][1] += U[i];
}
for (int i = N; i >= 1; i--) {
D[i] += D[i + 1];
mat[i][1] += D[i];
}
for (int j = 1; j <= M; j++) {
L[j] += L[j - 1];
mat[1][j] += L[j];
}
for (int j = M; j >= 1; j--) {
R[j] += R[j + 1];
mat[1][j] += R[j];
}
for (int i = 1; i <= N; i++) {
for (int j = 1; j <= M; j++) {
UL[i][j] += UL[i - 1][j - 1];
UR[i][j] += UR[i - 1][j + 1];
}
}
for (int i = N; i >= 1; i--) {
for (int j = M; j >= 1; j--) {
DL[i][j] += DL[i + 1][j - 1];
DR[i][j] += DR[i + 1][j + 1];
}
}
for (int i = 1; i <= N; i++) {
for (int j = 1; j <= M; j++) {
mat[i][j] += UL[i][j];
mat[i][j] += UR[i][j];
mat[i][j] += DL[i][j];
mat[i][j] += DR[i][j];
mat[i][j] += mat[i - 1][j] + mat[i][j - 1] - mat[i - 1][j - 1];
}
}
for (int i = 1; i <= N; i++) {
for (int j = 1; j <= M; j++) {
mat[i][j] += mat[i - 1][j] + mat[i][j - 1] - mat[i - 1][j - 1];
}
}
int Q;
cin >> Q;
while (Q--) {
int x1, y1, x2, y2;
cin >> x1 >> y1 >> x2 >> y2;
ll sum = mat[x2][y2] - mat[x1 - 1][y2] - mat[x2][y1 - 1] + mat[x1 - 1][y1 - 1];
ll area = (ll) (x2 - x1 + 1) * (y2 - y1 + 1);
ll answer = sum / area;
if (sum % area >= (area + 1) / 2 && area != 1) {
answer++;
}
cout << answer << "\n";
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
959 ms |
724276 KB |
Output is correct |
2 |
Correct |
192 ms |
296432 KB |
Output is correct |
3 |
Correct |
192 ms |
295784 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
983 ms |
724384 KB |
Output is correct |
2 |
Correct |
185 ms |
296196 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
216 ms |
392628 KB |
Output is correct |
2 |
Correct |
188 ms |
296256 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
211 ms |
403752 KB |
Output is correct |
2 |
Correct |
199 ms |
296316 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1080 ms |
726584 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
589 ms |
468564 KB |
Output is correct |
2 |
Correct |
188 ms |
296284 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
310 ms |
394728 KB |
Output is correct |
2 |
Correct |
198 ms |
296524 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
490 ms |
421460 KB |
Output is correct |
2 |
Correct |
187 ms |
296268 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1122 ms |
726924 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1124 ms |
726784 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
444 ms |
394572 KB |
Output is correct |
2 |
Correct |
413 ms |
394256 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
419 ms |
394432 KB |
Output is correct |
2 |
Correct |
396 ms |
394236 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
383 ms |
398328 KB |
Output is correct |
2 |
Correct |
430 ms |
395852 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
457 ms |
395004 KB |
Output is correct |
2 |
Correct |
511 ms |
628876 KB |
Output is correct |