Problem p03774 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define fst first
#define snd second
#define all(cont) cont.begin(), cont.end()
#define foreach(it, l) for (auto it = l.begin(); it != l.end(); it++)
#define fore(i, a, b) for (int i = a, almo5t = b; i < almo5t; ++i)
#define SZ(x) ((int)x.size())
#define EPS 1e-9
#define PI 3.1415926535897932384626433832795
#define MOD 1000000007
#define FIN                                                                    \
  std::ios_base::sync_with_stdio(false);                                       \
  cin.tie(NULL);                                                               \
  cout.tie(NULL)

const int N = 0;

typedef long long int ll;

using namespace std;

ll solve(ll w, ll a, ll b) {
  int res;
  int c = min(a, b);
  int d = max(a, b);
  c += w;
  res = d - c < 0 ? 0 : d - c;
  return res;
}

int main() {
  FIN;
#ifndef ONLINE_JUDGE
  freopen("../in.txt", "r", stdin);
  freopen("../out.txt", "w", stdout);
#endif
  int n, m;
  cin >> n >> m;
  vector<pair<int, int>> students;
  vector<pair<int, int>> checkPoints;

  fore(i, 0, n) {
    int val1, val2;
    cin >> val1 >> val2;
    students.pb(make_pair(val1, val2));
  }
  fore(i, 0, m) {
    int x, y;
    cin >> x >> y;
    checkPoints.pb(make_pair(x, y));
  }

  fore(i, 0, n) {
    ll min = 300000000;
    int idx = -1;
    fore(j, 0, m) {
      ll temp = abs(students[i].first - checkPoints[j].first) +
                abs(students[i].second - checkPoints[j].second);
      // cout<<temp<<"- \n";
      if (temp < min) {
        min = temp;
        idx = j + 1;
      }
    }
    cout << idx << "\n";
  }

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define fst first
#define snd second
#define all(cont) cont.begin(), cont.end()
#define foreach(it, l) for (auto it = l.begin(); it != l.end(); it++)
#define fore(i, a, b) for (int i = a, almo5t = b; i < almo5t; ++i)
#define SZ(x) ((int)x.size())
#define EPS 1e-9
#define PI 3.1415926535897932384626433832795
#define MOD 1000000007
#define FIN                                                                    \
  std::ios_base::sync_with_stdio(false);                                       \
  cin.tie(NULL);                                                               \
  cout.tie(NULL)

const int N = 0;

typedef long long int ll;

using namespace std;

ll solve(ll w, ll a, ll b) {
  int res;
  int c = min(a, b);
  int d = max(a, b);
  c += w;
  res = d - c < 0 ? 0 : d - c;
  return res;
}

int main() {
  int n, m;
  cin >> n >> m;
  vector<pair<int, int>> students;
  vector<pair<int, int>> checkPoints;

  fore(i, 0, n) {
    int val1, val2;
    cin >> val1 >> val2;
    students.pb(make_pair(val1, val2));
  }
  fore(i, 0, m) {
    int x, y;
    cin >> x >> y;
    checkPoints.pb(make_pair(x, y));
  }

  fore(i, 0, n) {
    ll min = 300000000;
    int idx = -1;
    fore(j, 0, m) {
      ll temp = abs(students[i].first - checkPoints[j].first) +
                abs(students[i].second - checkPoints[j].second);
      // cout<<temp<<"- \n";
      if (temp < min) {
        min = temp;
        idx = j + 1;
      }
    }
    cout << idx << "\n";
  }

  return 0;
}