#include <bits/stdc++.h>
#define N 200005
using namespace std;
struct nuclear{
int x,y,a,b;
}nuclears[N];
void solve(){
bool swp = 0;
int h,w;
cin >> w >> h;
if(h > w){
swp = 1;
swap(w,h);
}
vector<vector<long long>> v(w +2,(vector<long long>(h +2)));
int n;
cin >> n;
for(int i = 1;i<=n;i++){
cin >> nuclears[i].x >> nuclears[i].y >> nuclears[i].a >> nuclears[i].b;
if(swp)
swap(nuclears[i].x,nuclears[i].y);
}
vector<vector<long long>> addldiag(h +2,(vector<long long>(w + 2*h +2)));
vector<vector<long long>> addrdiag(h +2,(vector<long long>(w + 2*h +2)));
vector<vector<long long>> addver (h +2,(vector<long long>(w + 2*h +2)));
for(int i=1;i<=n;i++){
int lp = nuclears[i].x - nuclears[i].a/nuclears[i].b + h;
int rp = nuclears[i].x + nuclears[i].a/nuclears[i].b + h;
lp = max(1,lp);
rp = min(rp,w + h);
int lval = nuclears[i].a - (nuclears[i].x + h - lp) * nuclears[i].b;
int rval = nuclears[i].a - (rp - nuclears[i].x - h) * nuclears[i].b;
addldiag[nuclears[i].y][nuclears[i].x + 1 + h] += -nuclears[i].b;
addrdiag[nuclears[i].y][nuclears[i].x + 1 + h] += -nuclears[i].b;
addver [nuclears[i].y][lp] += lval;
addver [nuclears[i].y][lp + 1] += nuclears[i].b;
addver [nuclears[i].y][lp + 1] += -lval;
addver [nuclears[i].y][rp + 1] += nuclears[i].b;
addver [nuclears[i].y][rp + 1] += -rval;
addver [nuclears[i].y][rp + 2] += rval;
int tp = nuclears[i].y + nuclears[i].a/nuclears[i].b;
if(tp < h){
int num = nuclears[i].a/nuclears[i].b;
if(nuclears[i].x + h - num > 0)
addldiag[tp + 1][nuclears[i].x + h - num] -= -nuclears[i].b;
if(nuclears[i].x + h + 2 + num <= w + 2*h)
addrdiag[tp + 1][nuclears[i].x + h + 2 + num] -= -nuclears[i].b;
addver [tp + 1][lp] -= lval;
addver [tp + 1][lp + 1] -= nuclears[i].b;
addver [tp + 1][lp + 1] -= -lval;
addver [tp + 1][rp + 1] -= nuclears[i].b;
addver [tp + 1][rp + 1] -= -rval;
addver [tp + 1][rp + 2] -= rval;
}
}
for(int i = 1;i<=h;i++){
long long tot = 0;
long long difrate = 0;
for(int j = 1;j<=w+2*h;j++){
addldiag[i][j] += addldiag[i-1][j+1];
addrdiag[i][j] += addrdiag[i-1][j-1];
addver [i][j] += addver [i-1][j];
difrate += addldiag[i][j];
difrate += addrdiag[i][j];
difrate += addver [i][j];
tot += difrate;
if(j > h && j <= w+h)
v[j-h][i] += tot;
}
}
for(int i = 0;i<h + 2;i++){
for(int j = 0;j<w + 2*h +2;j++){
addldiag[i][j] = addrdiag[i][j] = addver[i][j] = 0;
}
}
for(int i=1;i<=n;i++){
int lp = nuclears[i].x - nuclears[i].a/nuclears[i].b + h;
int rp = nuclears[i].x + nuclears[i].a/nuclears[i].b + h;
lp = max(1,lp);
rp = min(rp,w + h);
int lval = nuclears[i].a - (nuclears[i].x + h - lp) * nuclears[i].b;
int rval = nuclears[i].a - (rp - nuclears[i].x - h) * nuclears[i].b;
addldiag[nuclears[i].y - 1][nuclears[i].x + h] += -nuclears[i].b;
addrdiag[nuclears[i].y - 1][nuclears[i].x + 2 + h] += -nuclears[i].b;
addver [nuclears[i].y - 1][lp] += lval;
addver [nuclears[i].y - 1][lp + 1] += nuclears[i].b;
addver [nuclears[i].y - 1][lp + 1] += -lval;
addver [nuclears[i].y - 1][rp + 1] += nuclears[i].b;
addver [nuclears[i].y - 1][rp + 1] += -rval;
addver [nuclears[i].y - 1][rp + 2] += rval;
int bp = nuclears[i].y - nuclears[i].a/nuclears[i].b;
if(bp > 1){
int num = nuclears[i].a/nuclears[i].b;
if(nuclears[i].x + h - num > 0)
addldiag[bp - 1][nuclears[i].x + h - num] -= -nuclears[i].b;
if(nuclears[i].x + h + num + 2 <= w + 2*h)
addrdiag[bp - 1][nuclears[i].x + h + num + 2] -= -nuclears[i].b;
addver [bp - 1][lp] -= lval;
addver [bp - 1][lp + 1] -= nuclears[i].b;
addver [bp - 1][lp + 1] -= -lval;
addver [bp - 1][rp + 1] -= nuclears[i].b;
addver [bp - 1][rp + 1] -= -rval;
addver [bp - 1][rp + 2] -= rval;
}
}
for(int i = h;i>=1;i--){
long long tot = 0;
long long difrate = 0;
for(int j = 1;j<=w+2*h;j++){
addldiag[i][j] += addldiag[i+1][j+1];
addrdiag[i][j] += addrdiag[i+1][j-1];
addver [i][j] += addver [i+1][j];
difrate += addldiag[i][j];
difrate += addrdiag[i][j];
difrate += addver [i][j];
tot += difrate;
if(j > h && j <= w+h)
v[j-h][i] += tot;
}
}
vector<vector<long long>> pre(w+2,(vector<long long>(h+2)));
for(int i = 1;i<=h;i++){
for(int j = 1;j<=w;j++){
pre[j][i] = pre[j][i-1] + pre[j-1][i] - pre[j-1][i-1] + v[j][i];
}
}
int q;
cin >> q;
while(q--){
int x1,y1,x2,y2;
cin >> x1 >> y1 >> x2 >> y2;
if(swp){
swap(x1,y1);
swap(x2,y2);
}
long double sz = (x2 - x1 + 1) * (y2 - y1 + 1);
cout << (long long)((pre[x2][y2] - pre[x2][y1-1] - pre[x1-1][y2] + pre[x1-1][y1-1] + sz/2)/sz) << '\n';
}
}
int32_t main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
#ifdef Local
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
#endif
int t=1;
//cin>>t;
while(t--){
solve();
}
#ifdef Local
cout<<endl<<fixed<<setprecision(2)<<1000.0 * clock() / CLOCKS_PER_SEC<< " milliseconds ";
#endif
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
618 ms |
450428 KB |
Output is correct |
2 |
Correct |
75 ms |
3112 KB |
Output is correct |
3 |
Correct |
66 ms |
2384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
541 ms |
450396 KB |
Output is correct |
2 |
Correct |
80 ms |
3064 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
186 ms |
110732 KB |
Output is correct |
2 |
Correct |
71 ms |
2864 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
226 ms |
117836 KB |
Output is correct |
2 |
Correct |
84 ms |
3092 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
775 ms |
454724 KB |
Output is correct |
2 |
Correct |
95 ms |
5112 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
364 ms |
184608 KB |
Output is correct |
2 |
Correct |
69 ms |
3052 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
331 ms |
114956 KB |
Output is correct |
2 |
Correct |
68 ms |
3368 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
260 ms |
121964 KB |
Output is correct |
2 |
Correct |
63 ms |
2780 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
802 ms |
457168 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
799 ms |
456988 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
471 ms |
116968 KB |
Output is correct |
2 |
Correct |
503 ms |
178764 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
458 ms |
151244 KB |
Output is correct |
2 |
Correct |
420 ms |
115904 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
430 ms |
108448 KB |
Output is correct |
2 |
Correct |
356 ms |
109444 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
407 ms |
116336 KB |
Output is correct |
2 |
Correct |
718 ms |
455904 KB |
Output is correct |