#include<bits/stdc++.h>
using namespace std;
#define N 2500010
#define ll long long
#define ii pair<int,int>
#define fs first
#define sc second
#define pb push_back
#define iii pair<int,ii>
#define int ll
#define ld long double
struct haha
{
int x,y,a,b;
}tv[200010];
int n,m,q,tmp[1010][1010];
vector<int>a[N];
vector<ii>f[N],sum[N],cs[N];
void solve()
{
//duoi
//cout<<"cc\n";
for(int i=0;i<=n+1;i++)
for(int j=0;j<=m+1;j++)
f[i][j]=sum[i][j]={0,0};
for(int i=1;i<=q;i++)
{
f[tv[i].x][tv[i].y].fs+=tv[i].a-tv[i].b*tv[i].x;
f[tv[i].x][tv[i].y].sc+=tv[i].b;
int L=(tv[i].a+tv[i].b-1)/tv[i].b;
if(tv[i].x-L>=1)
{
f[tv[i].x-L][max(1ll,tv[i].y-L)].fs-=(tv[i].a-tv[i].b*tv[i].x);
f[tv[i].x-L][max(1ll,tv[i].y-L)].sc-=tv[i].b;
}
}
for(int i=n;i>=1;i--)
for(int j=m;j>=1;j--)
{
sum[i][j].fs+=f[i][j].fs;
sum[i][j].sc+=f[i][j].sc;
if(i>1)
{
f[i-1][max(1ll,j-1)].fs+=f[i][j].fs;
f[i-1][max(1ll,j-1)].sc+=f[i][j].sc;
}
}
for(int i=0;i<=n+1;i++)
for(int j=0;j<=m+1;j++)
f[i][j]={0,0};
for(int i=1;i<=q;i++)
{
f[tv[i].x][tv[i].y].fs-=(tv[i].a-tv[i].b*tv[i].x);
f[tv[i].x][tv[i].y].sc-=(tv[i].b);
int L=(tv[i].a+tv[i].b-1)/tv[i].b;
if(tv[i].x-L>=1)
{
// cout<<i<<" "<<L<<endl;
f[tv[i].x-L][min(m+1,tv[i].y+L)].fs+=(tv[i].a-tv[i].b*tv[i].x);
f[tv[i].x-L][min(m+1,tv[i].y+L)].sc+=tv[i].b;
}
}
for(int i=n;i>=1;i--)
for(int j=m;j>=1;j--)
{
sum[i][j].fs+=f[i][j].fs;
sum[i][j].sc+=f[i][j].sc;
if(i>1)
{
f[i-1][min(m+1,j+1)].fs+=f[i][j].fs;
f[i-1][min(m+1,j+1)].sc+=f[i][j].sc;
}
}
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
{
sum[i][j].fs+=sum[i][j-1].fs;
sum[i][j].sc+=sum[i][j-1].sc;
a[cs[i][j].fs][cs[i][j].sc]+=(sum[i][j].fs+sum[i][j].sc*i);
}
//trai
for(int i=0;i<=n+1;i++)
for(int j=0;j<=m+1;j++)
f[i][j]=sum[i][j]={0,0};
for(int i=1;i<=q;i++)
{
f[tv[i].x][tv[i].y].fs-=(tv[i].a-tv[i].b*tv[i].y);
f[tv[i].x][tv[i].y].sc-=(tv[i].b);
int L=(tv[i].a+tv[i].b-1)/tv[i].b;
// cout<<tv[i].y<<" "<<L<<endl;
if(tv[i].y-L>=1)
{
// cout<<i<<endl;
f[max(0ll,tv[i].x-L)][tv[i].y-L].fs+=(tv[i].a-tv[i].b*tv[i].y);
f[max(0ll,tv[i].x-L)][tv[i].y-L].sc+=tv[i].b;
}
}
for(int j=m;j>=1;j--)
for(int i=n;i>=1;i--)
{
sum[i][j].fs+=f[i][j].fs;
sum[i][j].sc+=f[i][j].sc;
if(j>1)
{
f[max(0ll,i-1)][j-1].fs+=f[i][j].fs;
f[max(0ll,i-1)][j-1].sc+=f[i][j].sc;
}
}
for(int i=0;i<=n+1;i++)
for(int j=0;j<=m+1;j++)
f[i][j]={0,0};
for(int i=1;i<=q;i++)
{
f[tv[i].x][tv[i].y].fs+=(tv[i].a-tv[i].b*tv[i].y);
f[tv[i].x][tv[i].y].sc+=tv[i].b;
int L=(tv[i].a+tv[i].b-1)/tv[i].b;
if(tv[i].y-L>=1)
{
f[min(n,tv[i].x+L)][tv[i].y-L].fs-=(tv[i].a-tv[i].b*tv[i].y);
f[min(n,tv[i].x+L)][tv[i].y-L].sc-=tv[i].b;
}
}
for(int j=m;j>=1;j--)
for(int i=n;i>=1;i--)
{
sum[i][j].fs+=f[i][j].fs;
sum[i][j].sc+=f[i][j].sc;
if(j>1)
{
f[min(n,i+1)][j-1].fs+=f[i][j].fs;
f[min(n,i+1)][j-1].sc+=f[i][j].sc;
}
}
for(int i=n;i>=1;i--)
for(int j=1;j<=m;j++)
{
sum[i][j].fs+=sum[i+1][j].fs;
sum[i][j].sc+=sum[i+1][j].sc;
a[cs[i][j].fs][cs[i][j].sc]+=(sum[i][j].fs+sum[i][j].sc*j);
}
}
int32_t main()
{
// freopen("A.inp","r",stdin);
/// freopen("A.out","w",stdout);
ios::sync_with_stdio(0);
cin.tie(NULL);
cout.tie(NULL);
cin>>n>>m>>q;
for(int i=0;i<=n+1;i++)
{
a[i].resize(m+2,0);
f[i].resize(m+2);
sum[i].resize(m+2);
cs[i].resize(m+2);
for(int j=1;j<=m;j++)
cs[i][j]={i,j};
}
for(int i=1;i<=q;i++)
{
cin>>tv[i].x>>tv[i].y>>tv[i].a>>tv[i].b;
a[tv[i].x][tv[i].y]+=tv[i].a;
}
/*for(int t=1;t<=q;t++)
{
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
tmp[i][j]+=max(0ll,tv[t].a-tv[t].b*max(abs(tv[t].x-i),abs(tv[t].y-j)));
}*/
solve();
for(int i=1;i<=q;i++)
{
tv[i].x=n-tv[i].x+1;
tv[i].y=m-tv[i].y+1;
}
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
cs[i][j]={n-i+1,m-j+1};
solve();
/* for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)cout<<tmp[i][j]<<" ";cout<<endl;
}*/
/* for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
if(a[i][j]!=tmp[i][j])
{
cout<<"NGU";
return 0;
}*/
// return 0;
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
a[i][j]+=a[i-1][j];
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
a[i][j]+=a[i][j-1];
cin>>q;
while(q--)
{
int xs,ys,xe,ye;
cin>>xs>>ys>>xe>>ye;
int sum=a[xe][ye]+a[xs-1][ys-1]-a[xe][ys-1]-a[xs-1][ye];
cout<<(ll)(round((ld)1.0*sum/((ld)1.0*(xe-xs+1))/((ld)1.0*(ye-ys+1))))<<"\n";
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1118 ms |
783036 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1123 ms |
783016 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
582 ms |
373052 KB |
Output is correct |
2 |
Correct |
171 ms |
237464 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
545 ms |
386224 KB |
Output is correct |
2 |
Correct |
166 ms |
237524 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1125 ms |
782988 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
919 ms |
456748 KB |
Output is correct |
2 |
Correct |
162 ms |
237556 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
489 ms |
375176 KB |
Output is correct |
2 |
Correct |
190 ms |
237748 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
682 ms |
378400 KB |
Output is correct |
2 |
Correct |
160 ms |
237536 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1136 ms |
789328 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1141 ms |
789268 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
776 ms |
381228 KB |
Output is correct |
2 |
Correct |
825 ms |
381076 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
789 ms |
381156 KB |
Output is correct |
2 |
Correct |
619 ms |
380960 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
681 ms |
384744 KB |
Output is correct |
2 |
Correct |
814 ms |
382444 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
779 ms |
381612 KB |
Output is correct |
2 |
Correct |
757 ms |
662076 KB |
Output is correct |