Submission #680070

# Submission time Handle Problem Language Result Execution time Memory
680070 2023-01-09T21:00:12 Z bin9638 Nuclearia (CEOI15_nuclearia) C++17
100 / 100
843 ms 380884 KB
#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

#pragma GCC optimize("Ofast")
#pragma GCC optimize("inline")
#pragma GCC optimize("omit-frame-pointer")
#pragma GCC optimize("unroll-loops")

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];

void solve(int check)
{
    //duoi
    //cout<<"cc\n";
    for(int i=0;i<=n;i++)
        for(int j=0;j<=m;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;i++)
        for(int j=0;j<=m;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&&tv[i].y+L<=m)
        {
          //  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&&j+1<=m)
            {
                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[(check==0 ? i : n-i+1)][(check==0 ? j : m-j+1)]+=(sum[i][j].fs+sum[i][j].sc*i);
        }

    //trai
    for(int i=0;i<=n;i++)
        for(int j=0;j<=m;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;i++)
        for(int j=0;j<=m;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;
        }
    }
   // return;
    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++)
        {
            if(i<n)
            {
                sum[i][j].fs+=sum[i+1][j].fs;
                sum[i][j].sc+=sum[i+1][j].sc;
            }
            a[(check==0 ? i : n-i+1)][(check==0 ? j : m-j+1)]+=(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;
    int dao=0;
    if(m==1)
    swap(n,m),dao=1;
    for(int i=0;i<=n;i++)
    {
        a[i].resize(m+1,0);
        f[i].resize(m+1);
        sum[i].resize(m+1);
    }
    for(int i=1;i<=q;i++)
    {
        cin>>tv[i].x>>tv[i].y>>tv[i].a>>tv[i].b;
        if(dao==1)
            swap(tv[i].x,tv[i].y);
        a[tv[i].x][tv[i].y]+=tv[i].a;
    }
    solve(0);
    for(int i=1;i<=q;i++)
    {
        tv[i].x=n-tv[i].x+1;
        tv[i].y=m-tv[i].y+1;
    }
    solve(1);
    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];
   // return 0;
    cin>>q;
    while(q--)
    {
        int xs,ys,xe,ye;
        cin>>xs>>ys>>xe>>ye;
        if(dao==1)
        {
            swap(xs,ys);
            swap(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 Correct 359 ms 372172 KB Output is correct
2 Correct 143 ms 178852 KB Output is correct
3 Correct 130 ms 178376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 363 ms 372108 KB Output is correct
2 Correct 145 ms 178872 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 452 ms 274644 KB Output is correct
2 Correct 149 ms 178880 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 620 ms 279388 KB Output is correct
2 Correct 143 ms 178840 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 470 ms 374332 KB Output is correct
2 Correct 142 ms 179100 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 287 ms 257152 KB Output is correct
2 Correct 146 ms 178996 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 402 ms 276828 KB Output is correct
2 Correct 158 ms 179164 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 481 ms 257032 KB Output is correct
2 Correct 142 ms 178964 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 591 ms 380736 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 556 ms 380708 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 843 ms 282884 KB Output is correct
2 Correct 755 ms 282912 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 680 ms 282956 KB Output is correct
2 Correct 567 ms 282720 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 808 ms 285008 KB Output is correct
2 Correct 710 ms 283640 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 676 ms 283252 KB Output is correct
2 Correct 579 ms 380884 KB Output is correct