#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 MAXM = 2500006;
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], f[SQRT], g[SQRT];
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 magicFunc(void) {
for (int i = 0; i <= row + 1; ++i) {
sum[i].assign(col + 2, 0);
f[i].assign(col + 2, 0);
g[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);
addRec(max(x - d, 1), max(y - d, 1), min(x + d, row), min(y + d, col), a);
addRec(x, y, x, y, b);
}
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);
int z = max(1, min(d, max(x, y) - 1));
f[max(1, x - 1)][max(1, y - 1)] += b;
g[max(1, x - z)][max(1, y - z)] -= 1LL * b * (z + 1);
if(x - d - 1 < 1 && y - d - 1 < 1)
continue;
f[max(1, x - d - 1)][max(1, y - d - 1)] -= b;
}
for (int i = row; i > 0; --i) {
for (int j = col; j > 0; --j) {
sum[i][j] += g[i][j] + f[i][j];
f[max(1, i - 1)][max(1, j - 1)] += f[i][j];
f[i][j] = g[i][j] = 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);
++x, ++y;
if(x >= row || y >= col)
continue;
f[min(row, x + 1)][min(col, y + 1)] += b;
if(x + d <= row && y + d <= col)
g[min(row, x + d)][min(col, y + d)] -= 1LL * b * (d + 1);
if(x + d + 1 > row || y + d + 1 > col)
continue;
f[min(row, x + d + 1)][min(col, y + d + 1)] -= b;
}
for (int i = 1; i <= row; ++i) {
for (int j = 1; j <= col; ++j) {
sum[i][j] += g[i][j] + f[i][j];
f[min(row + 1, i + 1)][min(col + 1, j + 1)] += f[i][j];
f[i][j] = g[i][j] = 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);
++x;
if(x >= row)
continue;
f[min(row, x + 1)][max(1, y - 1)] -= b;
if(x + d <= row)
g[min(row, x + d)][max(1, y - d)] += 1LL * b * (d + 1);
if(x + d + 1 > row)
continue;
f[min(row, x + d + 1)][max(1, y - d - 1)] += b;
}
for (int i = 1; i <= row; ++i) {
for (int j = col; j > 0; --j) {
sum[i][j] += g[i][j] + f[i][j];
f[min(row + 1, i + 1)][max(1, j - 1)] += f[i][j];
f[i][j] = g[i][j] = 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);
++y;
if(y >= col)
continue;
f[max(1, x - 1)][min(col, y + 1)] -= b;
if(y + d <= col)
g[max(1, x - d)][min(col, y + d)] += 1LL * b * (d + 1);
if(y + d + 1 > col)
continue;
f[max(1, x - d - 1)][min(col, y + d + 1)] += b;
}
for (int i = row; i > 0; --i) {
for (int j = 1; j <= col; ++j) {
sum[i][j] += g[i][j] + f[i][j];
f[max(1, i - 1)][min(col + 1, j + 1)] += f[i][j];
f[i][j] = g[i][j] = 0;
}
}
for (int i = 1; i <= row; ++i) {
for (int j = 1; j <= col; ++j) {
sum[i][j] += sum[i - 1][j] + sum[i][j - 1] - sum[i - 1][j - 1];
}
}
for (int i = 1; i <= row; ++i) {
for (int j = 1; j <= col; ++j) {
sum[i][j] += sum[i - 1][j] + sum[i][j - 1] - sum[i - 1][j - 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);
for (int i = 1; i <= nArr; ++i) {
cin >> p[i].x >> p[i].y >> p[i].a >> p[i].b;
p[i].b = min(p[i].b, p[i].a);
if(isSwapped)
swap(p[i].x, p[i].y);
}
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);
}
}
magicFunc();
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
process();
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
81 ms |
178512 KB |
Output is correct |
2 |
Correct |
44 ms |
7044 KB |
Output is correct |
3 |
Correct |
43 ms |
6520 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
78 ms |
178512 KB |
Output is correct |
2 |
Correct |
46 ms |
7144 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
58 ms |
61532 KB |
Output is correct |
2 |
Correct |
43 ms |
6996 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
66 ms |
67424 KB |
Output is correct |
2 |
Correct |
44 ms |
6992 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
173 ms |
182864 KB |
Output is correct |
2 |
Correct |
47 ms |
7248 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
99 ms |
77420 KB |
Output is correct |
2 |
Correct |
44 ms |
6944 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
146 ms |
65924 KB |
Output is correct |
2 |
Correct |
47 ms |
7292 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
92 ms |
53972 KB |
Output is correct |
2 |
Correct |
49 ms |
6988 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
229 ms |
185144 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
231 ms |
185200 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
250 ms |
67600 KB |
Output is correct |
2 |
Correct |
249 ms |
67920 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
253 ms |
67664 KB |
Output is correct |
2 |
Correct |
245 ms |
67664 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
222 ms |
70228 KB |
Output is correct |
2 |
Correct |
214 ms |
68332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
219 ms |
68176 KB |
Output is correct |
2 |
Correct |
248 ms |
185152 KB |
Output is correct |