-3
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;


int main() {
    //start of input
    long n,m,k;
    cin>>n>>m>>k;
    bool a[n][m];
    for(long i=0;i<n;i++){
        for(long j=0;j<m;j++){
            a[i][j] = true;
        }
    }
    long b[k][3];
    for(long i=0;i<3;i++){
        for(long j=0;j<k;j++){
            cin>>b[i][j];
        }
    }
    //start of main logic
    for(long i=0;i<k;i++){     
        long row = b[i][0]-1;
        long col_init = b[i][1]-1;
        long col_fin = b[i][2]-1;
        while(col_init<=col_fin){
            a[row][col_init]=false;
            col_init++;
        }
    }
    long count=0;
    //counting number of empty area
    for(long i=0;i<n;i++){
        for(long j=0;j<m;j++){
        if(a[i][j]==true){count++;}
        }
    }
    cout<<count;
    return 0;
}

Constraints :
1 <= n,m <= 109
1 <= k <= 1000

so in above code:

I am getting segmentation fault in above code for certain cases.
Its running properly for small inputs but having problem with large ones.

  • 2
    This is not allowed `bool a[n][m];` as `n` and `m` must be known at *compile time* not *run time* – Cory Kramer Jul 21 '17 at 17:56
  • 2
    Have you tried using a debugger? – Curious Jul 21 '17 at 17:57
  • 2
    Copy and pasting the same bland description does not improve your question. You should have used those characters to add a detailed explanation of when/where/how it breaks. – NathanOliver Jul 21 '17 at 17:58
  • How large is a large input? What are the inputs? – Thomas Matthews Jul 21 '17 at 18:04
  • An extension to @CoryKramer 's comment. Some compilers allow Variable Length Arrays (VLA) .Looks like yours is one of them. Avoid VLA. It's possible this is the failure as there is nothing to prevent a sufficiently large `m * n` from making `a` (For your sake don't use variable names like this in your code. Alphabet soup code sucks to debug) too large to fit in Automatic storage. If you used a `vector` or other user of Dynamic allocation A) you get a much bigger memory pool and B) a thrown exception if there isn't enough storage. – user4581301 Jul 21 '17 at 18:40

1 Answers1

0

The question states that 1 <= n,m <= 109

So making an array like a[n][m] will take too much of memory, that is why you are getting segmentation fault, think of a different method to solve it...

Stop reading now if you dont want any hint on how to solve the problem

HINT : Value of k is less than 1000 , save all the train tracks in a vector array, that wont exceed memory, and then try to add the number of available positions to install a lamppost by going through those 'k' tracks one by one, think a while on how you can do this.