This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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 |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |