Problem p02605 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <stdio.h>
using namespace std;
int n, cnt;
int x[202020], y[202020];
struct Pa {
  int x, y;
  char dir;
} p[202020];
char dir[202020];
char s[20];
bool cmp1(Pa A, Pa B) {
  if (A.y != B.y) {
    return A.y < B.y;
  }
  return A.x < B.x;
}
bool cmp2(Pa A, Pa B) {
  if (A.x != B.x) {
    return A.x < B.x;
  }
  return A.y < B.y;
}
bool cmp3(Pa A, Pa B) {
  if (A.x + A.y != B.x + B.y) {
    return A.x + A.y < B.x + B.y;
  }
  return A.x < B.x;
}
bool cmp4(Pa A, Pa B) {
  if (A.x - A.y != B.x - B.y) {
    return A.x - A.y < B.x - B.y;
  }
  return A.x < B.x;
}
void get(char A, char B) {
  cnt = 0;
  for (int i = 1; i <= n; i++) {
    if (dir[i] == A || dir[i] == B) {
      p[++cnt].x = x[i];
      p[cnt].y = y[i];
      p[cnt].dir = dir[i];
    }
  }
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d%d%s", &x[i], &y[i], s);
    dir[i] = s[0];
    p[i].x = x[i];
    p[i].y = y[i];
    p[i].dir = dir[i];
  }
  int ans = 100000000;
  get('L', 'R');
  sort(p + 1, p + cnt + 1, cmp1);
  for (int i = 1; i < cnt; i++) {
    if (p[i].y == p[i + 1].y && p[i].dir == 'R' && p[i + 1].dir == 'L') {
      ans = min(ans, 5 * (p[i + 1].x - p[i].x));
    }
  }
  get('U', 'D');
  sort(p + 1, p + cnt + 1, cmp2);
  for (int i = 1; i < cnt; i++) {
    if (p[i].x == p[i + 1].x && p[i].dir == 'U' && p[i + 1].dir == 'D') {
      ans = min(ans, 5 * (p[i + 1].y - p[i].y));
    }
  }
  get('L', 'D');
  sort(p + 1, p + cnt + 1, cmp3);

  for (int i = 1; i < cnt; i++) {
    if (p[i].x + p[i].y == p[i + 1].x + p[i + 1].y && p[i].dir == 'D' &&
        p[i + 1].dir == 'L') {
      ans = min(ans, 10 * (p[i + 1].x - p[i].x));
    }
  }
  get('U', 'R');
  sort(p + 1, p + cnt + 1, cmp3);
  for (int i = 1; i < cnt; i++) {
    if (p[i].x + p[i].y == p[i + 1].x + p[i + 1].y && p[i].dir == 'R' &&
        p[i + 1].dir == 'U') {
      ans = min(ans, 10 * (p[i + 1].x - p[i].x));
    }
  }
  get('D', 'R');
  sort(p + 1, p + cnt + 1, cmp4);
  for (int i = 1; i < cnt; i++) {
    if (p[i].x - p[i].y == p[i + 1].x - p[i + 1].y && p[i].dir == 'R' &&
        p[i + 1].dir == 'D') {
      ans = min(ans, 10 * (p[i + 1].x - p[i].x));
    }
  }
  get('U', 'L');
  sort(p + 1, p + cnt + 1, cmp4);
  for (int i = 1; i < cnt; i++) {
    if (p[i].x - p[i].y == p[i + 1].x - p[i + 1].y && p[i].dir == 'U' &&
        p[i + 1].dir == 'L') {
      ans = min(ans, 10 * (p[i + 1].x - p[i].x));
    }
  }
  if (ans != 100000000) {
    printf("%d\\n", ans);
  } else
    printf("SAFE");
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <stdio.h>
using namespace std;
int a[202020], n, cnt = 0, k;
int t[202020], x[202020], y[202020];
struct Pa {
  int x, y;
  char dir;
} p[202020];
char dir[202020];
char s[20];
bool cmp1(Pa A, Pa B) {
  if (A.y != B.y) {
    return A.y < B.y;
  }
  return A.x < B.x;
}
bool cmp2(Pa A, Pa B) {
  if (A.x != B.x) {
    return A.x < B.x;
  }
  return A.y < B.y;
}
bool cmp3(Pa A, Pa B) {
  if (A.x + A.y != B.x + B.y) {
    return A.x + A.y < B.x + B.x;
  }
  return A.x < B.x;
}
bool cmp4(Pa A, Pa B) {
  if (A.x - A.y != B.x - B.y) {
    return A.x - A.y < B.x - B.x;
  }
  return A.x < B.x;
}
void get(char A, char B) {

  cnt = 0;
  for (int i = 1; i <= n; i++) {
    if (dir[i] == A || dir[i] == B) {
      p[++cnt].x = x[i];
      p[cnt].y = y[i];
      p[cnt].dir = dir[i];
    }
  }
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d%d%s", &x[i], &y[i], s);
    dir[i] = s[0];
    p[i].x = x[i];
    p[i].y = y[i];
    p[i].dir = dir[i];
  }
  int ans = 100000000;
  get('L', 'R');
  sort(p + 1, p + cnt + 1, cmp1);
  for (int i = 1; i < cnt; i++) {
    if (p[i].y == p[i + 1].y && p[i].dir == 'R' && p[i + 1].dir == 'L') {
      ans = min(ans, 5 * (p[i + 1].x - p[i].x));
    }
  }
  get('U', 'D');
  sort(p + 1, p + cnt + 1, cmp2);
  for (int i = 1; i < cnt; i++) {
    if (p[i].x == p[i + 1].x && p[i].dir == 'U' && p[i + 1].dir == 'D') {
      ans = min(ans, 5 * (p[i + 1].y - p[i].y));
    }
  }
  get('L', 'D');
  sort(p + 1, p + cnt + 1, cmp3);

  for (int i = 1; i < cnt; i++) {
    if (p[i].x + p[i].y == p[i + 1].x + p[i + 1].y && p[i].dir == 'D' &&
        p[i + 1].dir == 'L') {
      ans = min(ans, 10 * (p[i + 1].x - p[i].x));
    }
  }
  get('U', 'R');
  sort(p + 1, p + cnt + 1, cmp3);
  for (int i = 1; i < cnt; i++) {
    if (p[i].x + p[i].y == p[i + 1].x + p[i + 1].y && p[i].dir == 'R' &&
        p[i + 1].dir == 'U') {
      ans = min(ans, 10 * (p[i + 1].x - p[i].x));
    }
  }
  get('D', 'R');
  sort(p + 1, p + cnt + 1, cmp4);
  for (int i = 1; i < cnt; i++) {
    if (p[i].x - p[i].y == p[i + 1].x - p[i + 1].y && p[i].dir == 'R' &&
        p[i + 1].dir == 'D') {
      ans = min(ans, 10 * (p[i + 1].x - p[i].x));
    }
  }
  get('U', 'L');
  sort(p + 1, p + cnt + 1, cmp4);
  for (int i = 1; i < cnt; i++) {
    if (p[i].x - p[i].y == p[i + 1].x - p[i + 1].y && p[i].dir == 'U' &&
        p[i + 1].dir == 'L') {
      ans = min(ans, 10 * (p[i + 1].x - p[i].x));
    }
  }
  if (ans != 100000000) {
    printf("%d\\n", ans);
  } else
    printf("SAFE");
  return 0;
}