#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef long double ld;
typedef complex<ld> cd;
typedef pair<int, int> pi;
typedef pair<ll,ll> pl;
typedef pair<ld,ld> pd;
typedef vector<int> vi;
typedef vector<ld> vd;
typedef vector<ll> vl;
typedef vector<pi> vpi;
typedef vector<pl> vpl;
typedef vector<cd> vcd;
template <class T> using Tree = tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update>;
#define FOR(i, a, b) for (int i=a; i<(b); i++)
#define F0R(i, a) for (int i=0; i<(a); i++)
#define FORd(i,a,b) for (int i = (b)-1; i >= a; i--)
#define F0Rd(i,a) for (int i = (a)-1; i >= 0; i--)
#define sz(x) (int)(x).size()
#define mp make_pair
#define pb push_back
#define f first
#define s second
#define lb lower_bound
#define ub upper_bound
#define all(x) x.begin(), x.end()
const int MOD = 1000000007;
const ll INF = 1e18;
const int MX = 100001;
int W,H,N;
ll** create(int x, int y) {
ll **a = new ll*[x+1];
F0R(i,x+1) {
a[i] = new ll[y+1];
F0R(j,y+1) a[i][j] = 0;
}
return a;
}
struct cum {
ll **tri[2][2];
ll **rect;
void init() {
F0R(i,2) F0R(j,2) tri[i][j] = create(W,H);
rect = create(W,H);
}
void updRect(int x0, int x1, int y0, int y1, ll val) {
x0 = max(x0,1), x1 = min(x1,W), y0 = max(y0,1), y1 = min(y1,H);
if (x0 > x1 || y0 > y1) return;
rect[x0][y0] += val;
if (x1 < W) rect[x1+1][y0] -= val;
if (y1 < H) rect[x0][y1+1] -= val;
if (x1 < W && y1 < H) rect[x1+1][y1+1] += val;
}
void upRight(int x, int y, int sz, ll val) {
updRect(x,x+(y+sz-H-1),1,H,val);
if (y+sz <= H) tri[0][0][x][y+sz] += val;
else if (x+(y+sz-H) <= W) tri[0][0][x+(y+sz-H)][H] += val;
if (x+sz+1 <= W) tri[0][0][x+sz+1][y-1] -= val;
updRect(x,x+sz,1,y-1,-val);
}
void upLeft(int x, int y, int sz, ll val) {
updRect(x-((y+sz)-H-1),x,1,H,val);
if (y+sz <= H) tri[0][1][x][y+sz] += val;
else if (x-(y+sz-H) >= 1) tri[0][1][x-(y+sz-H)][H] += val;
if (x-sz-1 >= 1) tri[0][1][x-sz-1][y-1] -= val;
updRect(x-sz,x,1,y-1,-val);
}
void downLeft(int x, int y, int sz, ll val) {
updRect(x-(1-(y-sz)-1),x,1,H,val); // ??
if (y-sz >= 1) tri[1][1][x][y-sz] += val;
else if (x-(1-(y-sz)) >= 1) tri[1][1][x-(1-(y-sz))][1] += val;
if (x-sz-1 >= 1 && y < H) tri[1][1][x-sz-1][y+1] -= val;
updRect(x-sz,x,y+1,H,-val);
}
void downRight(int x, int y, int sz, ll val) {
// cout << "AH " << x << " " << (x+(1-(y-sz)-1)) << "\n";
updRect(x,x+(1-(y-sz)-1),1,H,val); // ??
// tri[1][0][1][1] ++;
if (y-sz >= 1) tri[1][0][x][y-sz] += val;
else if (x+(1-(y-sz)) <= W) tri[1][0][x+(1-(y-sz))][1] += val;
if (x+sz+1 <= W && y < H) tri[1][0][x+sz+1][y+1] -= val;
updRect(x,x+sz,y+1,H,-val);
}
void prop() {
FOR(i,1,W+1) FOR(j,1,H+1) {
updRect(i,i,1,j,tri[0][0][i][j]);
if (i != W) tri[0][0][i+1][j-1] += tri[0][0][i][j];
}
FORd(i,1,W+1) FOR(j,1,H+1) {
updRect(i,i,1,j,tri[0][1][i][j]);
tri[0][1][i-1][j-1] += tri[0][1][i][j];
}
FORd(i,1,W+1) FOR(j,1,H+1) {
updRect(i,i,j,H,tri[1][1][i][j]);
if (j != H) tri[1][1][i-1][j+1] += tri[1][1][i][j];
}
FOR(i,1,W+1) FOR(j,1,H+1) {
updRect(i,i,j,H,tri[1][0][i][j]);
if (i != W && j != H) tri[1][0][i+1][j+1] += tri[1][0][i][j];
}
FOR(i,1,W+1) FOR(j,1,H+1) rect[i][j] += rect[i-1][j]+rect[i][j-1]-rect[i-1][j-1];
}
};
cum c, cx, cy;
ll **res;
void gen() {
ios_base::sync_with_stdio(0); cin.tie(0);
cin >> W >> H >> N;
c.init(), cx.init(), cy.init();
res = create(W,H);
F0R(i,N) {
int x,y,a,b; cin >> x >> y >> a >> b;
res[x][y] += a;
int sz = a/b-1;
if (sz >= 0) {
c.upLeft(x+sz+1,y+1,sz,a+(ll)b*x); cx.upLeft(x+sz+1,y+1,sz,-b);
c.upLeft(x,y-sz-1,sz,a-(ll)b*y); cy.upLeft(x,y-sz-1,sz,b);
c.downRight(x,y+sz+1,sz,a+(ll)b*y); cy.downRight(x,y+sz+1,sz,-b);
c.downRight(x-sz-1,y-1,sz,a-(ll)b*x); cx.downRight(x-sz-1,y-1,sz,b);
c.upRight(x-sz-1,y,sz,a-(ll)b*x); cx.upRight(x-sz-1,y,sz,b);
c.upRight(x+1,y-sz-1,sz,a-(ll)b*y); cy.upRight(x+1,y-sz-1,sz,b);
c.downLeft(x+sz+1,y,sz,a+(ll)b*x); cx.downLeft(x+sz+1,y,sz,-b);
c.downLeft(x-1,y+sz+1,sz,a+(ll)b*y); cy.downLeft(x-1,y+sz+1,sz,-b);
}
}
c.prop(), cx.prop(), cy.prop();
FOR(i,1,W+1) {
FOR(j,1,H+1) {
res[i][j] += c.rect[i][j]+cx.rect[i][j]*i+cy.rect[i][j]*j;
// cout << res[i][j] << "\t";
res[i][j] += res[i-1][j]+res[i][j-1]-res[i-1][j-1];
}
//cout << "\n";
}
//exit(0);
}
int main() {
gen();
int Q; cin >> Q;
F0R(i,Q) {
int x1,y1,x2,y2; cin >> x1 >> y1 >> x2 >> y2;
cout << (ll)round(((ld)res[x2][y2]-res[x1-1][y2]-res[x2][y1-1]+res[x1-1][y1-1])/(x2-x1+1)/(y2-y1+1)) << "\n";
}
}
/* Look for:
* the exact constraints (multiple sets are too slow for n=10^6 :( )
* special cases (n=1?)
* overflow (ll vs int?)
* array bounds
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1104 ms |
466160 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1115 ms |
591340 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
809 ms |
630972 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
798 ms |
660876 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1139 ms |
790716 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1134 ms |
840576 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
813 ms |
840576 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
868 ms |
840576 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1103 ms |
854432 KB |
Time limit exceeded |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1119 ms |
890328 KB |
Time limit exceeded |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
867 ms |
890328 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1083 ms |
890328 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
848 ms |
890328 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
838 ms |
890328 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |