#include<bits/stdc++.h>
#define pb push_back
#define pli pair<int,int>
#define fi first
#define se second
#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL);
using namespace std;
using ll=long long;
const ll inf=1e18;
const ll mod=1e9+7;
ll h,w;
vector<vector<ll>> p1,p2,p3;
vector<ll>a2,pre;
ll cd=0;
void add(ll x,ll y,ll a,ll b)
{
ll r=(a-1)/b;
// p1 p2 p3
// p1 la prefix sum cho a
// p2 la prefix sum cho b cheo 1
// p3 la prefix sum cho b cheo 2
p1[max(1ll,x-r)][max(1ll,y-r)]+=a-(r+1)*b;
p1[min(x+r+1,w+1)][max(1ll,y-r)]-=a-(r+1)*b;
p1[max(1ll,x-r)][min(h+1,y+r+1)]-=a-(r+1)*b;
p1[min(w+1,x+r+1)][min(h+1,y+r+1)]+=a-(r+1)*b;
ll k=min({x-1,y-1,r});
p2[x-k][y-k]+=b;
if(k!=r&&x<y)
{
a2[max(0ll,y-r)]+=b;
a2[y-k]-=b;
}
k=min({w-x,h-y,r});
p2[x+k+2][y+k+2]-=b;
k=min({r,x-1,h-y});
p3[x-k][y+k+1]-=b;
if(k!=r&&x-1<h-y)
{
a2[y+k+2]-=b;
a2[min(y+r+2,h+1)]+=b;
}
k=min({r,w-x,y-1});
p3[x+k+2][y-k-1]+=b;
if(r>y-1)
{
// cham y=0 khi nao
ll bk=r-y;
ll L=x-y;
ll R=x+y+1;
// time for update
//cout << L-bk<<'\n';
if(L>=1)
{
pre[L+1]-=b;
pre[max(1ll,L-bk)]+=b;
if(L-bk<1)
{
cd+=(1-L+bk)*b;
}
}
else
{
cd+=(bk+1)*b;
}
if(R<=w)
{
pre[R]-=b;
pre[min(w+1,R+bk+1)]+=b;
}
}
}
ll n;
using ld=long double;
ll get(ll x1,ll y1,ll x2,ll y2)
{
return p1[x2][y2]-p1[x1-1][y2]-p1[x2][y1-1]+p1[x1-1][y1-1];
}
void solve()
{
cin >> w >> h;
p1.resize(w+3,vector<ll>(h+3,0));
p2.resize(w+3,vector<ll>(h+3,0));
p3.resize(w+3,vector<ll>(h+3,0));
pre.resize(w+3,0);
a2.resize(h+3,0);
cin >> n;
for(int i=1;i<=n;i++)
{
ll x,y,a,b;
cin >> x >> y >> a >> b;
//cout << x <<' '<<y<<' '<<a<<' '<<b<<'\n';
add(x,y,a,b);
}
for(int i=1;i<=w;i++) for(int j=1;j<=h;j++) p2[i][j]+=p2[i-1][j-1];
for(int i=1;i<=w;i++) for(int j=h;j>=1;j--) p3[i][j]+=p3[i-1][j+1];
for(int i=1;i<=w;i++) for(int j=1;j<=h;j++) p2[i][j]+=p3[i][j];
for(int i=1;i<=w;i++)
{
for(int j=1;j<=h;j++)
{
//cout << p2[i][j]<<' ';
}
//cout << '\n';
}
for(int i=1;i<=h;i++)
{
a2[i]+=a2[i-1];
p2[0][i]=a2[i];
//cout << a2[i]<<' ';
}
for(int i=1;i<=w;i++) for(int j=1;j<=h;j++) p2[i][j]+=p2[i-1][j];
for(int i=1;i<=w;i++)
{
pre[i]+=pre[i-1];
}
pre[0]=cd;
for(int i=1;i<=w;i++) pre[i]+=pre[i-1];
for(int i=1;i<=w;i++)
{
p2[i][0]=pre[i];
//cout << pre[i]<<' ';
}
for(int i=1;i<=w;i++) for(int j=1;j<=h;j++) p2[i][j]+=p2[i][j-1];
for(int i=1;i<=w;i++) for(int j=1;j<=h;j++) p1[i][j]+=p1[i-1][j];
for(int i=1;i<=w;i++) for(int j=1;j<=h;j++) p1[i][j]+=p1[i][j-1];
for(int i=1;i<=w;i++) for(int j=1;j<=h;j++) p1[i][j]+=p2[i][j];
for(int i=1;i<=w;i++) for(int j=1;j<=h;j++) p1[i][j]+=p1[i-1][j]+p1[i][j-1]-p1[i-1][j-1];
ll q;
cin >> q;
for(int i=1;i<=q;i++)
{
ll x1,y1,x2,y2;
cin >> x1 >> y1 >> x2 >> y2;
ll sum=get(x1,y1,x2,y2);
ll sz=(x2-x1+1)*(y2-y1+1);
cout << sum/sz+(sum%sz>=(sz+1)/2)<<'\n';
}
}
int main()
{
fastio
//freopen("c.INP","r",stdin);
//freopen("c.OUT","w",stdout);
solve();
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
477 ms |
548188 KB |
Output is correct |
2 |
Correct |
48 ms |
2756 KB |
Output is correct |
3 |
Correct |
42 ms |
2300 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
466 ms |
548268 KB |
Output is correct |
2 |
Correct |
45 ms |
2644 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
58 ms |
59984 KB |
Output is correct |
2 |
Correct |
58 ms |
2632 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
59 ms |
70000 KB |
Output is correct |
2 |
Correct |
48 ms |
4692 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
561 ms |
550512 KB |
Output is correct |
2 |
Correct |
54 ms |
3156 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
292 ms |
221924 KB |
Output is correct |
2 |
Correct |
45 ms |
2724 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
135 ms |
61960 KB |
Output is correct |
2 |
Correct |
47 ms |
4948 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
201 ms |
112208 KB |
Output is correct |
2 |
Correct |
44 ms |
2640 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
660 ms |
550696 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
681 ms |
550828 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
224 ms |
61952 KB |
Output is correct |
2 |
Correct |
228 ms |
61576 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
231 ms |
61712 KB |
Output is correct |
2 |
Correct |
227 ms |
61700 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
181 ms |
63692 KB |
Output is correct |
2 |
Correct |
201 ms |
70224 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
201 ms |
62364 KB |
Output is correct |
2 |
Correct |
246 ms |
284128 KB |
Output is correct |