#include<bits/stdc++.h>
using namespace std;
#define inf 0x3f3f3f3f
#define sz(x) int((x).size())
#define fi first
#define se second
typedef long long ll;
typedef pair<int, int> ii;
template<class X, class Y>
inline bool maximize(X &x, const Y &y) {return (x < y ? x = y, 1 : 0);}
template<class X, class Y>
inline bool minimize(X &x, const Y &y) {return (x > y ? x = y, 1 : 0);}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int Random(int l, int r) {
return uniform_int_distribution<int>(l, r)(rng);
}
const int MAXN = 200005;
const int SQRT = 1600;
struct Point {
int x, y, a, b;
} p[MAXN];
struct Query {
int x, y, u, v;
} qr[MAXN];
vector<ll> sum[SQRT];
ll dpl[MAXN], dplt[MAXN], dpr[MAXN], dprt[MAXN];
int row, col, nArr, numQuery;
inline void addRec(int x, int y, int u, int v, int w) {
sum[x][y] += w;
sum[u + 1][y] -= w;
sum[x][v + 1] -= w;
sum[u + 1][v + 1] += w;
}
void brute(void) {
for (int i = 0; i <= row + 1; ++i)
sum[i].assign(col + 2, 0);
for (int i = 1; i <= nArr; ++i) {
int x(p[i].x), y(p[i].y), a(p[i].a), b(p[i].b), d(a / b);
d = max({x - max(x - d, 1), y - max(y - d, 1), min(x + d, row) - x, min(y + d, col) - y});
for (int j = 0; j <= d; ++j) {
int x1 = max(x - j, 1), y1 = max(y - j, 1), u1 = min(x + j, row), v1 = min(y + j, col);
addRec(x1, y1, u1, v1, (j < d ? +1LL * b * (j + 1) : 0LL) - 1LL * b * j);
if(j == d)
addRec(x1, y1, u1, v1, a);
}
}
for (int x = 1; x <= row; ++x) {
for (int y = 1; y <= col; ++y) {
sum[x][y] += sum[x - 1][y] + sum[x][y - 1] - sum[x - 1][y - 1];
}
}
for (int x = 1; x <= row; ++x) {
for (int y = 1; y <= col; ++y) {
sum[x][y] += sum[x - 1][y] + sum[x][y - 1] - sum[x - 1][y - 1];
}
}
for (int t = 0; t < numQuery; ++t) {
int x(qr[t].x), y(qr[t].y), u(qr[t].u), v(qr[t].v), sizen = (u - x + 1) * (v - y + 1);
ll totRad = sum[u][v] - sum[u][y - 1] - sum[x - 1][v] + sum[x - 1][y - 1];
cout << totRad / sizen + (totRad % sizen >= (sizen + 1) / 2) << '\n';
}
}
void subrow1(void) {
for (int i = 0; i <= row + 1; ++i)
sum[i].assign(col + 2, 0);
for (int i = 1; i <= nArr; ++i) {
int y(p[i].y), a(p[i].a), b(p[i].b), d(a / b);
sum[1][max(y - d, 1)] += a;
sum[1][min(y + d, col) + 1] -= a;
dpl[y - 1] -= b;
if(y > d + 1) {
dpl[y - d - 1] += b;
dplt[y - d - 1] += 1LL * b * d;
}
dpr[y + 1] -= b;
if(y + d + 1 <= col) {
dpr[y + d + 1] += b;
dprt[y + d + 1] += 1LL * b * d;
}
}
for (int i = 1; i <= col; ++i)
sum[1][i] += sum[1][i - 1];
ll sumn(0), add(0);
for (int i = col; i > 0; --i) {
add += dpl[i];
sumn += add + dplt[i];
sum[1][i] += sumn;
}
add = sumn = 0;
for (int i = 1; i <= col; ++i) {
add += dpr[i];
sumn += add + dprt[i];
sum[1][i] += sumn + sum[1][i - 1];
}
for (int t = 0; t < numQuery; ++t) {
int x(qr[t].x), y(qr[t].y), u(qr[t].u), v(qr[t].v), sizen = (u - x + 1) * (v - y + 1);
ll totRad = sum[u][v] - sum[u][y - 1] - sum[x - 1][v] + sum[x - 1][y - 1];
cout << totRad / sizen + (totRad % sizen >= (sizen + 1) / 2) << '\n';
}
}
void process() {
cin >> row >> col >> nArr;
bool isSwapped = (row > col);
if(isSwapped)
swap(row, col);
ll sumf(0);
for (int i = 1; i <= nArr; ++i) {
cin >> p[i].x >> p[i].y >> p[i].a >> p[i].b;
if(isSwapped)
swap(p[i].x, p[i].y);
int x(p[i].x), y(p[i].y), d(p[i].a / p[i].b);
sumf += max({x - max(1, x - d), y - max(1, y - d), min(row, x + d) - x, min(col, y + d) - y}) + 1;
}
cin >> numQuery;
for (int t = 0; t < numQuery; ++t) {
cin >> qr[t].x >> qr[t].y >> qr[t].u >> qr[t].v;
if(isSwapped) {
swap(qr[t].x, qr[t].y);
swap(qr[t].u, qr[t].v);
}
}
if(1LL * nArr * row * col <= 1e8 || sumf <= 1e8) {
brute();
} else
if(row == 1) {
subrow1();
}
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
process();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
368 ms |
63316 KB |
Output is correct |
2 |
Correct |
47 ms |
10840 KB |
Output is correct |
3 |
Correct |
46 ms |
10172 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
330 ms |
63280 KB |
Output is correct |
2 |
Correct |
45 ms |
10772 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
27 ms |
24152 KB |
Output is correct |
2 |
Correct |
44 ms |
10576 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
38 ms |
26116 KB |
Output is correct |
2 |
Correct |
45 ms |
10832 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
182 ms |
71120 KB |
Output is correct |
2 |
Correct |
50 ms |
11472 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
369 ms |
35772 KB |
Output is correct |
2 |
Correct |
45 ms |
10832 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
107 ms |
31808 KB |
Output is correct |
2 |
Correct |
50 ms |
11148 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
219 ms |
28028 KB |
Output is correct |
2 |
Correct |
45 ms |
10576 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
146 ms |
143700 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
154 ms |
143652 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
759 ms |
37636 KB |
Output is correct |
2 |
Execution timed out |
1035 ms |
35528 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1010 ms |
35664 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
83 ms |
15508 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
84 ms |
15760 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |