#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 + h + 5)));
vector<vector<long long>> addrdiag(h+5,(vector<long long>(w + h + 5)));
vector<vector<long long>> addver (h+5,(vector<long long>(w + 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){
addldiag[tp + 1][lp] -= -nuclears[i].b;
addrdiag[tp + 1][rp + 2] -= -nuclears[i].b;
addver [tp + 1][lp] -= rval;
addver [tp + 1][lp + 1] -= nuclears[i].b;
addver [tp + 1][lp + 1] -= -rval;
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+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)
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;
}*/
for(int i = 0;i<h+5;i++){
for(int j = 0;j<w + 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;
if(bp > 1){
addldiag[bp - 1][lp] -= -nuclears[i].b;
addrdiag[bp - 1][rp + 2] -= -nuclears[i].b;
addver [bp - 1][lp] -= rval;
addver [bp - 1][lp + 1] -= nuclears[i].b;
addver [bp - 1][lp + 1] -= -rval;
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+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)
v[j-h][i] += tot;
}
}
/*
cout << endl;
for(int i=1;i<=h;i++){
for(int j = 1;j<=w;j++){
cout << v[j][i] << " ";
}
cout << endl;
}*/
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) << endl;
}
}
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 |
788 ms |
783120 KB |
Output is correct |
2 |
Correct |
304 ms |
4288 KB |
Output is correct |
3 |
Correct |
310 ms |
3832 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
642 ms |
783228 KB |
Output is correct |
2 |
Correct |
292 ms |
4252 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
191 ms |
105524 KB |
Output is correct |
2 |
Incorrect |
301 ms |
4264 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
227 ms |
130552 KB |
Output is correct |
2 |
Correct |
286 ms |
4252 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1010 ms |
786824 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
608 ms |
317416 KB |
Output is correct |
2 |
Correct |
299 ms |
4296 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
531 ms |
109412 KB |
Output is correct |
2 |
Incorrect |
283 ms |
4584 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
534 ms |
172600 KB |
Output is correct |
2 |
Incorrect |
340 ms |
4300 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1106 ms |
790116 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1128 ms |
790256 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
663 ms |
117024 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
676 ms |
135244 KB |
Output is correct |
2 |
Correct |
646 ms |
118036 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
550 ms |
115788 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
620 ms |
118052 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |