samedi 30 avril 2016

facing error in output for different test cases

Problem

Alexey is playing with an array, A, of n integers. His friend, Ivan, asks him to calculate the sum of the maximum values for all subsegments of A. More formally, he wants Alexey to find F(A)=∑(l=1 to n)∑(r=l to n) max(l≤x≤r)A[x].

Alexey solved Ivan's challenge faster than expected, so Ivan decides to add another layer of difficulty by having Alexey answer m queries. The ith query contains subsegment [Li,Ri] and he must calculate the sum of maximum values on all subsegments inside subsegment [Li,Ri][Li,Ri].

More formally, for each query ii, Alexey must calculate the following function:

F(A,Li,Ri)=∑(l=Li to Ri) ∑(r=l to Ri) max(l≤x≤r)A[x].

Can you help Alexey solve this problem?

Input Format

The first line contains 2 space-separated positive integers, n (the length of array A) and m (number of queries), respectively. The second line contains n space-separated integers, a0,a1,…,an−1 describing each element aj (where 0≤j

Constraints 1≤n,m≤135000 −10^9≤ai≤10^9 1≤Li≤Ri≤n

Output Format For each query ii (where 0 ≤ i < m ), print its answer on a new line.

I tried to solve this but not passing all the test cases. Help me to find the error.

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;

long max(long *a,long x,long y){
    long m =a[x];
    for(long i=x+1;i<=y;i++){
        if(m<a[i])
            m=a[i];
    }
        return m;
}

void find(long a[] , long l[][2] ,long n,long m){
    for(long i=0;i<m;i++){
        long sum=0;
        for(long x=l[i][0];x<=l[i][1];x++){
            for(long y=x;y<=l[i][1];y++){
                 long m = max(a,x,y);
                 sum+=m;
            }
        }
        cout<<sum<<endl;

    }

}

int main() {
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */   
    try{
        long n, m;
        cin>>n>>m;
        if(n>135000||m>135000||n<1||m<1){
            throw 10;
        }
        long a[n];
        long l[m][2];
        for(long i=0;i<n;i++){
            cin>>a[i];
            if(a[i]>pow(10,9)||a[i]<pow(10,-9)){
                throw 10;
            }    
        }
        for(long j=0;j<m;j++)
            for(long k=0;k<2;k++){
            cin>>l[j][k];
            if(l[j][k]>n||l[j][k]<1){
                throw 10;
            }
            l[j][k]--;
        }
        find(a,l,n,m);
    }catch(...){
        cout<<"Constraint violated";
    }
    return 0;
}

Aucun commentaire:

Enregistrer un commentaire