The Challenge Day - CHADAY CodeChef Medium-Easy Question

Welcome back to my blog.

Here is the link to the PROBLEM .

So , the problem looks fairly simple , we just need to iterate on all players and need to find which all of compos will give positive scores .

Now , for each player , if its number does not lie on the boundary of l and r of any challenge(m) , then the answer of that player is the same as before .
This little trick helps us bring our answer in the expected time limits .

Now , if it is a boundary of l or r , then we go on to compute a matrix S whose ith element is sum of the scores upto (i-1)th challenge .

and the answer to a compo , is S[right]-S[left-1] , if its greater than 0 , we add it to the answer .

Do take a look at the following C++ code for better understanding .

Code:-

#include <iostream>
typedef long long int ll;
#include<vector>

using namespace std;

int main() {
    int t;
    cin>>t;
    while(t--)
    {
        ll l[100005],r[100005],L[100005],R[100005],S[100005],v[100005],vis[100005];
        ll i=1,j=1,k=1,n,m,q;
        cin>>n>>m>>q;
        
        i=1;
        while(i!=m+1)
        {
            cin>>l[i]>>r[i]>>v[i];
            i++;
        }
        i=1;
        while(i!=m+1)
        {
            vis[l[i]]=vis[r[i]+1]=1;
            i++;
        }
        while(j!=q+1)
        {
            cin>>L[j]>>R[j];
            j++;
        }
        i=1;
        vis[1]=1;
        ll ans=0;
        while(i!=n+1)
        {
            if(vis[i])
            {
            ans=0;
            j=1;
            S[0]=0;
            while(j!=m+1)
            {
                if(i>=l[j] && i<=r[j])
                {
                    S[j]=S[j-1]+v[j];
                }
                else
                {
                    S[j]=S[j-1];
                }
                j++;
            }
            j=1;
            while(j!=q+1)
            {
                if(S[R[j]]-S[L[j]-1]>0)
                {
                    ans=ans+(S[R[j]]-S[L[j]-1]);
                }
                j++;
            }
            }
            cout<<ans<<" ";
            i++;
        }
        cout<<endl;
    }
// your code goes here
return 0;
}

Do leave your doubts and queries in the comments section . 

Comments

Popular Posts