#include <bits/stdc++.h>
using namespace std;
#define REP(a,b,c) for(int a=int(b); a<int(c); a++)
#define REV(a,b,c) for(int a=int(c-1); a>=int(b); a--)
#define RE(a,b) REP(a,0,b)
#define RE1(a,b) REP(a,1,b+1)
#define REI(a,b,c) REP(a,b,c+1)
#define FOR(a,b) for(auto& a : b)
#define all(a) a.begin(), a.end()
#define pb push_back
#define sz size()
#define fi first
#define se second
typedef long long ll;
typedef pair<int,int> ii;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<ii> vii;
template<class T> void IN(T& a) {cin>>a;}
template<class T, class... L> void IN(T& a, L&... b) {IN(a); IN(b...);}
template<class T> void OUT(const T& a) {cout<<a;}
template<class T, class... L> void OUT(const T& a, const L&... b) {OUT(a); OUT(b...);}
template<class... T> void OUTL(const T&... a) {OUT(a..., '\n');}
const int MX = 5e6+1e5;
ll w, h, n, w1;
ll x[MX], y[MX], a[MX], b[MX];
ll c[MX], d[MX], V[MX], SUM[MX];
int f(int i, int j) {return i+j*w1;}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
// input
IN(w,h,n); w1 = w+1;
RE(i,n) IN(x[i],y[i],a[i],b[i]), x[i]--, y[i]--;
RE(i,n) {
ll r = (a[i]-1)/b[i];
ll mnj=max(y[i]-r,0ll);
ll mxj=min(h-1,y[i]+r);
REI(j,mnj,mxj) {
ll cr = abs(j-y[i]);
// increasing
ll bg = max(0ll, x[i] - r);
ll ed = x[i] - cr - 1;
if(ed >= 0) {
ll v = a[i] - b[i]*x[i];
d[f(bg,j)] += b[i];
c[f(bg,j)] += v;
d[f(ed+1,j)] -= b[i];
c[f(ed+1,j)] -= v;
}
// same
bg = max(0ll, x[i] - cr);
ed = min(w-1, x[i] + cr);
ll v = a[i] - b[i]*cr;
c[f(bg,j)] += v;
c[f(ed+1,j)] -= v;
// decreasing
bg = x[i] + cr + 1;
ed = min(w-1, x[i] + r);
if(bg < w) {
ll v = a[i] + b[i]*x[i];
d[f(bg,j)] -= b[i];
c[f(bg,j)] += v;
d[f(ed+1,j)] += b[i];
c[f(ed+1,j)] -= v;
}
}
}
REP(i,1,w) RE(j,h) {
c[f(i,j)] += c[f(i-1,j)];
d[f(i,j)] += d[f(i-1,j)];
}
RE(i,w) RE(j,h) V[f(i,j)] = c[f(i,j)] + (ll)i*d[f(i,j)];
RE(i,w) SUM[f(i,0)] = 0;
RE(i,h) SUM[f(0,i)] = 0;
RE(i,w) RE(j,h) SUM[f(i+1,j+1)] = SUM[f(i+1,j)] + SUM[f(i,j+1)] - SUM[f(i,j)] + V[f(i,j)];
// anser queries
ll q;
IN(q);
RE(i,q) {
ll x1, x2, y1, y2;
IN(x1,y1,x2,y2); x1--; y1--;
ll sm = SUM[f(x2,y2)] - SUM[f(x1,y2)] - SUM[f(x2,y1)] + SUM[f(x1,y1)];
ll area = abs(x2-x1)*abs(y2-y1);
OUTL((sm+area/2)/area);
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
72 ms |
98168 KB |
Output is correct |
2 |
Correct |
80 ms |
4892 KB |
Output is correct |
3 |
Correct |
72 ms |
4088 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
87 ms |
98168 KB |
Output is correct |
2 |
Correct |
95 ms |
4728 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
156 ms |
78712 KB |
Output is correct |
2 |
Correct |
77 ms |
4472 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
267 ms |
82552 KB |
Output is correct |
2 |
Correct |
89 ms |
4856 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
254 ms |
104232 KB |
Output is correct |
2 |
Correct |
86 ms |
5368 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
169 ms |
45432 KB |
Output is correct |
2 |
Correct |
107 ms |
4728 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
356 ms |
84624 KB |
Output is correct |
2 |
Correct |
81 ms |
5112 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
169 ms |
41464 KB |
Output is correct |
2 |
Correct |
76 ms |
4428 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
308 ms |
114316 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
303 ms |
114332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1073 ms |
50144 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1097 ms |
50040 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1091 ms |
49912 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1076 ms |
49544 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |