1485B - Replace and Keep Sorted - CodeForces Solution


dp implementation math *1200

Please click on ads to support us..

C++ Code:

// © Urvish Patel

#include<bits/stdc++.h>
using namespace std;

#define fast  ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);

#define ll long long int

#define endll "\n"

#define f(var,srt,ed) for(int var=srt;var<ed;var++)
#define rf(var,srt,ed) for(int var=srt;var>ed;var--)
#define fe(var,cont) for(auto &var:cont)

#define pb push_back
#define begin begin()
#define end end()

#define gcd(a,b) __gcd(a,b)
#define lcm(a,b) ((a*b)/(__gcd(a,b)))

#define YES(x) cout<<"YES"<<x
#define NO(x) cout<<"NO"<<x

ll dp[(int)1e5+2][4];
//0 none, 1 left, 2 right, 3 both


void solve()
{
    int n,q,k;
    cin>>n>>q>>k;

    int arr[n];

    f(i,0,n)
    cin>>arr[i];

    f(i,0,n)
    {
        int left,right;

        if(i==0)
        {
            left=0;
            right=arr[i+1];

            dp[i][0]=k-1;
            dp[i][1]=k-left-1;
            dp[i][2]=right-2;
            dp[i][3]=right-left-2;
        }
        else if(i==n-1)
        {
            right=0;
            left=arr[i-1];

            dp[i][0]=k-1;
            dp[i][1]=k-left-1;
            dp[i][2]=right-2;
            dp[i][3]=right-left-2+dp[i-1][3];
        }
        else
        {
            left=arr[i-1];
            right=arr[i+1];

            dp[i][0]=k-1;
            dp[i][1]=k-left-1;
            dp[i][2]=right-2;
            dp[i][3]=right-left-2+dp[i-1][3];
        }
    }

    // f(i,0,n)
    // {
    //     f(j,0,4)
    //     cout<<dp[i][j]<<" ";

    //     cout<<endl;
    // }

    while(q--)
    {
        int l,r;
        cin>>l>>r;

        int answer=0;

        if(l==r)
        {
            answer+=dp[l-1][0];
        }
        else if(r==l+1)
        {
            answer+=dp[l-1][2];
            answer+=dp[r-1][1];
        }
        else
        {
            answer+=dp[l-1][2];
            answer+=dp[r-1][1];
            answer+=dp[r-2][3]-dp[l-1][3];
        }

        cout<<answer<<endl;
    }

}


int main()
{
    fast

    int t=1;
    //cin>>t;

    f(_,0,t)
    {
        //cout<<"Case #"<<_<<": ";
        solve();
    }




    return 0;
}


Comments

Submit
0 Comments
More Questions

158A - Next Round
71A - Way Too Long Words
160A - Twins
1A - Theatre Square
1614B - Divan and a New Project
791A - Bear and Big Brother
1452A - Robot Program
344A - Magnets
96A - Football
702B - Powers of Two
1036A - Function Height
443A - Anton and Letters
1478B - Nezzar and Lucky Number
228A - Is your horseshoe on the other hoof
122A - Lucky Division
1611C - Polycarp Recovers the Permutation
432A - Choosing Teams
758A - Holiday Of Equality
1650C - Weight of the System of Nested Segments
1097A - Gennady and a Card Game
248A - Cupboards
1641A - Great Sequence
1537A - Arithmetic Array
1370A - Maximum GCD
149A - Business trip
34A - Reconnaissance 2
59A - Word
462B - Appleman and Card Game
1560C - Infinity Table
1605C - Dominant Character