dimanche 24 mai 2020

why got segmentation error in my program?

https://www.hackerrank.com/challenges/gridland-metro/problem This is the link to the question in hacker rank.

#include <bits/stdc++.h>
#include <iostream>
#include <vector>

using namespace std;

int main() {
  int n, m, k;
  scanf("%d%d%d", &n, &m, &k);
  int tot = n * m;
  vector<vector<int>> track;

  for (int i = 1; i <= n; i++) {
    track[i][0] = INT_MAX;
    track[i][1] = INT_MIN;
  }

  while (k--) {
    int r, c1, c2;
    scanf("%d%d%d", &r, &c1, &c2);

    if (track[r][0] > c1 && track[r][1] < c2) {
      if (track[r][0] == INT_MAX && track[r][1] == INT_MIN) {
        track[r][0] = c1;
        track[r][1] = c2;
        tot -= c2 - c1 + 1;
      } else {
        tot -= (track[r][0] - c1) + (c2 - track[r][1]);
        track[r][0] = c1;
        track[r][1] = c2;
      }
    } else if (track[r][0] <= c1 && track[r][1] >= c2) {
      tot -= 0;
      continue;
    } else if ((track[r][1] > c1 && track[r][0] < c1) && track[r][1] < c2) {
      c1 = track[r][1] + 1;
      tot -= c2 - c1 + 1;
    }
  }

  printf("%lld", tot);
}

This is my code for the question and I am getting segmentation fault in this. PLz tell me the right way of doing this program because I guess my code is brute force one if it was made correct

Aucun commentaire:

Enregistrer un commentaire