#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+5,(vector<long long>(h+5)));
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+5,(vector<long long>(w + 2*h + 5)));
vector<vector<long long>> addrdiag(h+5,(vector<long long>(w + 2*h + 5)));
vector<vector<long long>> addver (h+5,(vector<long long>(w + 2*h + 5)));
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=1;i<=h;i++){
for(int j = 1;j<=w;j++){
cout << v[j][i] << " ";
}
cout << endl;
}
cout << endl;*/
for(int i = 0;i<h+5;i++){
for(int j = 0;j<w + 2*h + 5;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;
//cout << bp << endl;
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;
//cout << addrdiag[i][j] << " ";
}
//cout << endl;
}
/*
for(int i=1;i<=h;i++){
for(int j = 1;j<=w;j++){
cout << v[j][i] << " ";
}
cout << '\n';
}*/
vector<vector<long long>> pre(w+5,(vector<long long>(h+5)));
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
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
735 ms |
783220 KB |
Output is correct |
2 |
Correct |
81 ms |
3148 KB |
Output is correct |
3 |
Correct |
60 ms |
2472 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
727 ms |
783112 KB |
Output is correct |
2 |
Correct |
59 ms |
3276 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
183 ms |
111464 KB |
Output is correct |
2 |
Correct |
64 ms |
2792 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
213 ms |
130580 KB |
Output is correct |
2 |
Correct |
61 ms |
3112 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
866 ms |
787520 KB |
Output is correct |
2 |
Correct |
76 ms |
5452 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
382 ms |
317752 KB |
Output is correct |
2 |
Correct |
79 ms |
3064 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
287 ms |
115560 KB |
Output is correct |
2 |
Correct |
87 ms |
3388 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
274 ms |
172948 KB |
Output is correct |
2 |
Correct |
80 ms |
2688 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
887 ms |
794496 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
911 ms |
794428 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
409 ms |
117684 KB |
Output is correct |
2 |
Correct |
461 ms |
185360 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
457 ms |
151588 KB |
Output is correct |
2 |
Correct |
402 ms |
116464 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
328 ms |
111412 KB |
Output is correct |
2 |
Correct |
422 ms |
116220 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
381 ms |
116960 KB |
Output is correct |
2 |
Correct |
969 ms |
796016 KB |
Output is correct |