Submission #594226

# Submission time Handle Problem Language Result Execution time Memory
594226 2022-07-12T08:53:52 Z andrei_boaca Nuclearia (CEOI15_nuclearia) C++14
100 / 100
402 ms 472424 KB
#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#define int long long
using namespace std;
typedef long long ll;
typedef long double ld;
vector<vector<ll>> v,S1,S2,toadd;
int n,m,k,q;
int nradds=0;
void add(int x1,int y1,int x2,int y2,ll val)
{
    x1=max(x1,1LL);
    y1=max(y1,1LL);
    x2=min(x2,n);
    y2=min(y2,m);
    v[x1][y1]+=val;
    v[x2+1][y1]-=val;
    v[x1][y2+1]-=val;
    v[x2+1][y2+1]+=val;
}
void putadd(int x1,int y1,int x2,int y2,ll val)
{
    toadd[x1][y1]+=val;
    toadd[x2+1][y1]-=val;
    toadd[x1][y2+1]-=val;
    toadd[x2+1][y2+1]+=val;
}
ll getsum(int x1,int y1,int x2,int y2)
{
    return v[x2][y2]-v[x1-1][y2]-v[x2][y1-1]+v[x1-1][y1-1];
}
ll put[2][2500005];
ll s[2500005];
signed main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cin>>n>>m>>k;
    if(m==1)
    {
        for(int z=1;z<=k;z++)
        {
            ll x,y,a,b;
            cin>>x>>y>>a>>b;
            //assert(y==1);
            ll nr=a/b;
            put[0][x]+=a;
            put[0][x+1]-=a;
            ll st=x-nr;
            ll dr=x-1;
            if(dr>0)
            {
                st=max(st,1LL);
                ll lg=dr-st+2;
                put[0][st]+=a-lg*b;
                put[0][dr+1]-=a-lg*b;
                put[0][dr+1]-=(lg-1)*b;
                put[1][st]+=b;
                put[1][dr+1]-=b;
            }
            st=x+1;
            dr=x+nr;
            if(st<=n)
            {
                dr=min(dr,n*1LL);
                ll lg=dr-st+2;
                put[0][st]+=a;
                put[0][dr+1]-=a;
                put[0][dr+1]-=(lg-1)*(-b);
                put[1][st]-=b;
                put[1][dr+1]+=b;
            }
        }
        ll suma=0;
        for(int i=1;i<=n;i++)
        {
            suma+=put[0][i];
            s[i]=suma;
        }
        ll si=0;
        suma=0;
        for(int i=1;i<=n;i++)
        {
            si+=put[1][i];
            suma+=si;
            s[i]+=suma;
        }
        for(int i=1;i<=n;i++)
            s[i]+=s[i-1];
        cin>>q;
        while(q--)
        {
            int x1,y1,x2,y2;
            cin>>x1>>y1>>x2>>y2;
            //assert(y1==1&&y2==1);
            ll val=s[x2]-s[x1-1];
            ll rez=round(ld(val)/ld(x2-x1+1));
            cout<<rez<<'\n';
        }
        return 0;
    }
    v.resize(n+5);
    S1.resize(n+5);
    S2.resize(n+5);
    toadd.resize(n+5);
    for(int i=0;i<=n+4;i++)
    {
        v[i].resize(m+5);
        S1[i].resize(m+5);
        S2[i].resize(m+5);
        toadd[i].resize(m+5);
    }
    for(int z=1;z<=k;z++)
    {
        int x,y,a,b;
        cin>>x>>y>>a>>b;
        int nr=a;
        int x1=x,y1=y,x2=x,y2=y;
        int cnt=a/b;
        if(a%b==0)
            cnt--;
        if(cnt==0)
        {
            add(x,y,x,y,a);
            continue;
        }
        x1=x-cnt;
        x2=x+cnt;
        y1=y-cnt;
        y2=y+cnt;

        add(x1,y1,x2,y2,a);
        add(x1,y1,x2,y2,-(cnt+1)*b);
        /*while(x1<=x)
        {
            add(x1,y1,x2,y2,b);
            x1++;
            y1++;
            x2--;
            y2--;
        }*/



        int i=x1,j=y1;
        /*while(i<=x)
        {
            int p=max(i,1LL);
            int q=max(j,1LL);
            v[p][q]+=b;
            i++;
            j++;
        }*/
        ll nr11=max(0LL,min(1-x1,1-y1));
        v[1][1]+=nr11*b;
        i+=nr11;
        j+=nr11;

        if(i<1)
        {
            ll cnt=1-i;
            ll st=j;
            ll dr=j+cnt-1;
            putadd(1,st,1,dr,b);
            i+=cnt;
            j+=cnt;
        }
        if(j<1)
        {
            ll cnt=1-j;
            ll st=i;
            ll dr=i+cnt-1;
            putadd(st,1,dr,1,b);
            i+=cnt;
            j+=cnt;
        }

        S1[i][j]+=b;
        S1[x+1][y+1]-=b;

        i=x+1;
        j=y+1;
        /*while(i<=x2+1&&i<=n&&j<=m&&j<=y2+1)
        {
            v[i][j]+=b;
            i++;
            j++;
        }*/
        S1[i][j]+=b;
        if(!(x2+2>n||y2+2>m))
            S1[x2+2][y2+2]-=b;




        i=x1;
        j=y2+1;
        ll num=max(0LL,j-m);

        i+=num;
        j-=num;

        if(i<1)
        {
            ll cnt=1-i;
            ll st=j-cnt+1;
            ll dr=j;
            putadd(1,st,1,dr,-b);
            i+=cnt;
            j-=cnt;
        }
        num=x-i;
        S2[i][j]-=b;
        S2[i+num+1][j-(num+1)]+=b;

        /*while(i<=x)
        {
            int p=max(i,1LL);
            int q=j;
            if(q<=m)
                v[p][q]-=b;
            i++;
            j--;
        }*/
        i=i+num+1;
        j=j-(num+1);
        ll limit=min(x2+1,n);

        if(i>limit)
            continue;

        ll to1=max(0LL,j);
        num=limit-i+1;

        ll ij=i+min(num-1,to1-1);
        ll jj=j-min(num-1,to1-1);

        S2[i][j]-=b;
        S2[ij+1][jj-1]+=b;

        i=ij+1;
        j=jj-1;
        if(i<=limit)
        {
            ll st=i;
            ll dr=limit;
            putadd(st,1,dr,1,-b);
        }
        /*while(i<=x2+1&&i<=n)
        {
            int p=i;
            int q=max(j,1LL);
            v[p][q]-=b;
            i++;
            j--;
        }*/
        //S2[i][j]-=b;
        /*if(x2+2>n||y1-1<1)
            continue;
        S2[x2+2][y1-1]+=b;*/
    }
    for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++)
            toadd[i][j]+=toadd[i-1][j]+toadd[i][j-1]-toadd[i-1][j-1];
    for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++)
            v[i][j]+=toadd[i][j];
    for(int i=1;i<=n;i++)
    {
        ll x=i,y=1;
        ll suma=0;
        while(x<=n&&y<=m)
        {
            suma+=S1[x][y];
            v[x][y]+=suma;
            x++;
            y++;
        }
    }
    for(int j=2;j<=m;j++)
    {
        ll x=1,y=j;
        ll suma=0;
        while(x<=n&&y<=m)
        {
            suma+=S1[x][y];
            v[x][y]+=suma;
            x++;
            y++;
        }
    }
    for(int i=1;i<=n;i++)
    {
        ll x=i,y=m;
        ll suma=0;
        while(x<=n&&y>0)
        {
            suma+=S2[x][y];
            v[x][y]+=suma;
            x++;
            y--;
        }
    }
    for(int j=1;j<m;j++)
    {
        ll x=1,y=j;
        ll suma=0;
        while(x<=n&&y>0)
        {
            suma+=S2[x][y];
            v[x][y]+=suma;
            x++;
            y--;
        }
    }
    /*for(int i=1;i<=n;i++,cout<<'\n')
        for(int j=1;j<=m;j++)
            cout<<v[i][j]<<' ';*/
    for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++)
            v[i][j]+=v[i-1][j]+v[i][j-1]-v[i-1][j-1];
    /*for(int i=1;i<=n;i++,cout<<'\n')
        for(int j=1;j<=m;j++)
            cout<<v[i][j]<<' ';*/
    for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++)
        {
            v[i][j]=v[i][j]+v[i-1][j]+v[i][j-1]-v[i-1][j-1];
            //cout<<s[i][j]<<' ';
        }
    //assert(nradds<=k*max(n,m));
    cin>>q;
    while(q--)
    {
        int x1,y1,x2,y2;
        cin>>x1>>y1>>x2>>y2;
        ll suma=getsum(x1,y1,x2,y2);
        ll cells=(y2-y1+1)*(x2-x1+1);
        ll rez=round(ld(suma)/ld(cells));
        cout<<rez<<'\n';
    }
    return 0;
}

Compilation message

nuclearia.cpp: In function 'int main()':
nuclearia.cpp:117:13: warning: unused variable 'nr' [-Wunused-variable]
  117 |         int nr=a;
      |             ^~
# Verdict Execution time Memory Grader output
1 Correct 18 ms 20308 KB Output is correct
2 Correct 65 ms 2872 KB Output is correct
3 Correct 58 ms 2568 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 25 ms 20436 KB Output is correct
2 Correct 59 ms 2976 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 75 ms 80012 KB Output is correct
2 Correct 66 ms 2892 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 69 ms 98416 KB Output is correct
2 Correct 65 ms 2968 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 126 ms 22480 KB Output is correct
2 Correct 65 ms 3128 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 138 ms 12004 KB Output is correct
2 Correct 59 ms 2884 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 192 ms 82060 KB Output is correct
2 Correct 73 ms 3212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 321 ms 175196 KB Output is correct
2 Correct 60 ms 2964 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 229 ms 61652 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 199 ms 61652 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 296 ms 82224 KB Output is correct
2 Correct 315 ms 81776 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 366 ms 81808 KB Output is correct
2 Correct 295 ms 81692 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 324 ms 86524 KB Output is correct
2 Correct 300 ms 87260 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 313 ms 82756 KB Output is correct
2 Correct 402 ms 472424 KB Output is correct