alexjercan/bugnet huggingface dataset:
Problem p00517 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <iostream>

typedef std::pair<int, int> P;

const int MAX_W = 10000;
const int MAX_H = 10000;
const int MAX_N = 1000;
int h, w, n;
P t[MAX_N];

unsigned int long long sum;

int main() {
  int x, y;
  std::cin >> w >> h >> n;
  for (int i = 0; i < n; ++i) {
    std::cin >> x >> y;
    t[i].first = x - 1;
    t[i].second = y - 1;
  }
  for (int i = 0; i < n - 1; ++i) {
    int ax = t[i + 1].first - t[i].first;
    int ay = t[i + 1].second - t[i].second;
    if ((ax >= 0 && ay >= 0) || (ax < 0 && ay < 0))
      sum += std::max(std::abs(ax), std::abs(ay));
    else
      sum += std::abs(ax) + std::abs(ay);
  }
  std::cout << sum << std::endl;
  return 0;
}

Error: MLE

Fixed submission:

#include <iostream>
#include <queue>

typedef std::pair<int, int> P;

const int MAX_W = 10000;
const int MAX_H = 10000;
const int MAX_N = 1000;
int h, w, n;

int dx[6] = {1, -1, 0, 0, 1, -1}, dy[6] = {0, 0, 1, -1, 1, -1};
int board[MAX_H][MAX_W];
int d[MAX_H][MAX_W];
P t[MAX_N];

int bfs(P s, P g) {
  for (int i = 0; i < h; ++i) {
    for (int j = 0; j < w; ++j)
      d[i][j] = -1;
  }
  std::queue<P> que;
  d[s.second][s.first] = 0;
  que.push(s);

  while (!que.empty()) {
    P p = que.front();
    que.pop();
    if (p == g) {
      return d[p.second][p.first];
    }
    for (int i = 0; i < 6; ++i) {
      int nx = p.first + dx[i];
      int ny = p.second + dy[i];
      if (nx >= 0 && ny >= 0 && nx < w && ny < h && d[ny][nx] == -1) {
        d[ny][nx] = d[p.second][p.first] + 1;
        que.push(P(nx, ny));
      }
    }
  }
}

int solve() {
  int ans = 0;
  for (int i = 0; i < n - 1; ++i) {
    int res = bfs(t[i], t[i + 1]);
    ans += res;
  }
  return ans;
}

int main() {
  int x, y;
  std::cin >> w >> h >> n;
  for (int i = 0; i < n; ++i) {
    std::cin >> x >> y;
    t[i].first = x - 1;
    t[i].second = y - 1;
  }
  int ans = solve();
  std::cout << ans << std::endl;
  return 0;
}
Problem p00517 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <queue>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define rep(i, n) for (int i = 0; i < n; ++i)
#define rrep(i, n) for (int i = 1; i <= n; ++i)
using namespace std;
typedef pair<int, int> P;
const int INF = 1000000000;

int f(int x, int y, int s, int t) {
  int X, Y, asd;
  X = s - x;
  Y = t - y;
  if (X * Y >= 0) {
    X = abs(X);
    Y = abs(Y);
    asd = min(X, Y);
    return X + Y - asd;
  }
  if (X * Y < 0) {
    X = abs(X);
    Y = abs(Y);
    return X + Y;
  }
}

int main() {
  int w, h, n, a = 0, x0, y0, x, y;
  scanf("%d%d%d", &w, &h, &n);
  scanf("%d%d", &x0, &y0);
  rep(i, n - 1) {
    scanf("%d%d", &x, &y);
    a += f(x0, y0, x, y);
    x0 = x, y0 = y;
  }

  printf("%d\\n", a);

  return 0;
}

Error: -11

Fixed submission:

#include <algorithm>
#include <queue>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define rep(i, n) for (int i = 0; i < n; ++i)
#define rrep(i, n) for (int i = 1; i <= n; ++i)
using namespace std;
typedef pair<int, int> P;
const int INF = 1000000000;

int f(int x, int y, int s, int t) {
  int X, Y, asd;
  X = s - x;
  Y = t - y;
  if (X * Y >= 0) {
    asd = min(X, Y);
    return X + Y - asd;
  }
  if (X * Y < 0) {
    return X + Y;
  }
}

int main() {
  int w, h, n, a = 0, x0, y0, x, y;
  scanf("%d%d%d", &w, &h, &n);
  scanf("%d%d", x0, y0);
  rep(i, n - 1) {
    scanf("%d%d", x, y);
    a += f(x0, y0, x, y);
    x0 = x, y0 = y;
  }

  printf("%d\\n", a);

  return 0;
}
Problem p00517 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

#define FOR(i, j, k) for (int i = j; i < k; ++i)
#define rep(i, j) FOR(i, 0, j)
#define repr(i, j) for (int i = j; i >= 0; --i)
#define INF (1 << 30)
#define MOD 1e9 + 7

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;
typedef pair<P, int> Pi;

int main() {
  int W, H, N;
  scanf("%d %d %d", &W, &H, &N);
  int ans = 0;
  int nx, ny, X, Y;
  scanf("%d %d", &X, &Y);
  nx = X;
  ny = Y;
  FOR(i, 1, N) {
    scanf("%d %d", &X, &Y);
    if ((nx < X && ny < Y) || (nx > X && ny > Y)) {
      ans += min(abs(nx - X), abs(ny - Y));
      int tmp = min(abs(nx - X), abs(ny - Y));
      if (nx < X && ny < Y) {
        nx += tmp;
        ny += tmp;
      } else {
        nx -= tmp;
        ny -= tmp;
      }
    }
    ans += abs(nx - X) + abs(ny - Y);
    nx = X;
    ny = Y;
  }
  printf("%d\\n", ans);
  return 0;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define FOR(i, j, k) for (int i = j; i < k; ++i)
#define rep(i, j) FOR(i, 0, j)
#define repr(i, j) for (int i = j; i >= 0; --i)
#define INF (1 << 30)
#define MOD 1e9 + 7

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;
typedef pair<P, int> Pi;

const int dx[] = {1, 0, -1, 0, 1, -1};
const int dy[] = {0, 1, 0, -1, 1, -1};
bool went[10010][10010];
int sx, sy;
int gx, gy;

int W, H, N;

int bfs(int x, int y, int cnt) {
  queue<Pi> que;
  que.push(Pi(P(y, x), 0));

  while (!que.empty()) {
    Pi p = que.front();
    que.pop();
    if (went[p.first.first][p.first.second])
      continue;
    else
      went[p.first.first][p.first.second] = true;
    if (p.first.first == gy && p.first.second == gx) {
      sy = gy;
      sx = gx;
      return p.second;
    }
    rep(i, 6) {
      int ny = p.first.first + dy[i];
      int nx = p.first.second + dx[i];
      if (ny < H && ny >= 0 && nx < W && nx >= 0) {
        que.push(Pi(P(ny, nx), p.second + 1));
      }
    }
  }
  return 0;
}

int main() {
  scanf("%d %d %d", &W, &H, &N);
  int ans = 0;
  rep(i, N) {
    int X, Y;
    scanf("%d %d", &X, &Y);
    memset(went, false, sizeof(went));
    --X;
    --Y;
    if (i == 0) {
      sy = Y;
      sx = X;
      continue;
    }
    gx = X;
    gy = Y;
    ans += bfs(sx, sy, 0);
  }
  printf("%d\\n", ans);
  return 0;
}
Problem p00517 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <iostream>
#include <stdlib.h>
using namespace std;
int main() {
  int w, h, n, nx, ny, mx, my, tot = 0;
  cin >> w >> h >> n >> nx >> ny;
  n--;
  while (n--) {
    cin >> mx >> my;
    if ((my > ny && mx > nx) || (my < ny && mx < nx)) {
      tot +=
          (min(abs(mx - nx), abs(my - ny)) + abs(abs(mx - nx) - abs(my - ny)));
    } else {
      tot += (abs(mx - nx) + abs(my - ny));
    }
    nx = mx;
    ny = my;
  }
  cout << tot << endl;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <functional>
#include <iostream>
#include <queue>
#include <set>
#include <string.h>
#include <string>
#include <vector>

using namespace std;

// int go[1002];
queue<pair<int, pair<int, int>>> que;

int main() {
  int w, h, n;
  cin >> w >> h >> n;
  int nx, ny, mx, my, x, y, cnt, tot;
  pair<int, pair<int, int>> t;
  cin >> nx >> ny;
  for (int i = 0; i < n - 1; i++) {
    cin >> mx >> my;
    que.push(make_pair(0, make_pair(nx, ny)));
    while (true) {
      t = que.front();
      cnt = t.first;
      x = t.second.first;
      y = t.second.second;
      // cout<<x<<"   "<<y<<"    "<<cnt<<endl;
      if (x == mx && y == my) {
        tot += cnt;
        break;
      }
      if (x > 1) {
        que.push(make_pair(cnt + 1, make_pair(x - 1, y)));
      }
      if (y > 1) {
        que.push(make_pair(cnt + 1, make_pair(x, y - 1)));
      }
      if (x < w) {
        que.push(make_pair(cnt + 1, make_pair(x + 1, y)));
      }
      if (y < h) {
        que.push(make_pair(cnt + 1, make_pair(x, y + 1)));
      }
      if (x > 1 && y > 1) {
        que.push(make_pair(cnt + 1, make_pair(x - 1, y - 1)));
      }
      if (x < w && y < h) {
        que.push(make_pair(cnt + 1, make_pair(x + 1, y + 1)));
      }
      que.pop();
    }
    while (!que.empty())
      que.pop();
    nx = mx;
    ny = my;
  }
  cout << tot << endl;
  return 0;
}
Problem p00517 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <cstdlib>
using namespace std;

int x[2000], y[2000];

int main() {
  int W, H, N;
  int X, Y;

  int cnt = 0;

  scanf("%d %d %d", &W, &H, &N);
  for (int i = 0; i < N; i++) {
    scanf("%d %d", &x[i], &y[i]);
    if (i == 0) {
      X = x[i];
      Y = y[i];
    }

    if (i > 0) {
      while (x[i] != X || y[i] != Y) {
        if (X < x[i] && Y < y[i]) {
          X++;
          Y++;
        } else if (X > x[i] && Y > y[i]) {
          X--;
          Y--;
        } else if (X == x[i]) {
          if (Y > y[i]) {
            Y--;
          } else {
            Y++;
          }
        } else {
          if (X > x[i]) {
            X--;
          } else {
            X++;
          }
        }
        cnt++;
      }
    }
  }

  printf("%d\\n", cnt);

  return 0;
}

Error: TLE

Fixed submission:

#include <cstdio>
#define INF (999999)
using namespace std;

const int dx[] = {-1, -1, 0, 1, 0};
const int dy[] = {0, 1, 1, 0, -1};

int W, H, N;
int min = INF;
bool map[10000][10000];

void Search(int x, int y, int num, int i) {
  if (i == min || i == W * H || x == 0 || x > W || y == 0 || y > H) {
    return;
  }

  i++;
  num -= map[x][y];

  if (num == 0) {
    min = i;
    return;
  }

  for (int j = 0; j < 5; j++) {
    Search(x + dx[j], y + dy[j], num, i);
  }
}

int main() {
  scanf("%d %d %d", &W, &H, &N);

  for (int i = 0; i < N; i++) {
    int x, y;
    scanf("%d %d", &x, &y);

    map[x][y] = true;
  }

  for (int i = 1; i <= W; i++) {
    for (int j = 1; j <= H; j++) {
      if (map[i][j]) {
        Search(i, j, N, 0);
      }
    }
  }

  printf("%d\\n", min);
}
Problem p00517 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <math.h>

struct point {
  int x;
  int y;
};

using namespace std;

int main(void) {
  int W = 0;
  int H = 0;
  int N = 0;

  cin >> W >> H >> N;

  point start;

  cin >> start.x >> start.y;

  int cost = 0;

  for (int n = 1; n < N; n++) {
    point goal;

    cin >> goal.x >> goal.y;

    int distance = 0;

    if (((start.x < goal.x) && (start.y < goal.y)) ||
        ((goal.x < start.x) && (goal.y < start.y))) {
      point d = (point){abs(goal.x - start.x), abs(goal.y - start.y)};

      distance = d.x + d.y;

      while ((d.x > 0) && (d.y > 0)) {
        d.x--;
        d.y--;

        distance--;
      }
    } else {
      distance = abs(goal.x - start.x) + abs(goal.y - start.y);
    }

    cost += distance;

    start.x = goal.x;
    start.y = goal.y;
  }

  cout << cost << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <limits>
#include <queue>
#include <vector>

struct point {
  int x;
  int y;
};

using namespace std;

int main(void) {
  int W = 0;
  int H = 0;
  int N = 0;

  vector<point> neighbors;

  neighbors.push_back((struct point){1, 0});
  neighbors.push_back((struct point){1, 1});
  neighbors.push_back((struct point){0, 1});
  neighbors.push_back((struct point){-1, 0});
  neighbors.push_back((struct point){-1, -1});
  neighbors.push_back((struct point){0, -1});

  cin >> W >> H >> N;

  point start;

  cin >> start.x >> start.y;

  start.x--;
  start.y--;

  int total = 0;

  for (int n = 1; n < N; n++) {
    point goal;

    cin >> goal.x >> goal.y;

    goal.x--;
    goal.y--;

    vector<vector<bool>> visited(W, vector<bool>(H, false));
    queue<point> queue;

    queue.push(start);
    visited.at(start.x).at(start.y) = true;

    int cost = 0;
    bool found = false;

    while (!queue.empty()) {
      vector<point> newNodes;

      while (!queue.empty()) {
        point p = queue.front();
        queue.pop();

        //	visited.at(p.x).at(p.y) = true;

        if ((p.x == goal.x) && (p.y == goal.y)) {
          found = true;
          break;
        }

        newNodes.push_back(p);
      }

      if (found) {
        break;
      }

      for (vector<point>::iterator i = newNodes.begin(); i != newNodes.end();
           i++) {
        for (vector<point>::iterator j = neighbors.begin();
             j != neighbors.end(); j++) {
          point d = {(*i).x + (*j).x, (*i).y + (*j).y};

          if ((d.x < 0) || (d.x >= W) || (d.y < 0) || (d.y >= H)) {
            continue;
          }

          if (!visited.at(d.x).at(d.y)) {
            queue.push(d);
            visited.at(d.x).at(d.y) = true;
          }
        }
      }

      cost++;
    }

    total += cost;
    start.x = goal.x;
    start.y = goal.y;
  }

  cout << total << endl;

  return 0;
}
Problem p00517 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;
int ax[10005];
int ay[10005];
int main() {
  //???????????????????????°??????(1,1)
  int w, h, n; // w,h?????????????????????????????????
  int i = 0;
  int x = 1;
  int y = 1;
  int a, b, c; //?§?????????¢???x,y?????????????§???????
  int an = 0;  //???????¨?

  cin >> w >> h >> n;
  for (i = 0; i < n; i++) {
    cin >> ax[i] >> ay[i];
  }
  x = ax[0];
  y = ay[0];
  for (i = 1; i < n; i++) {
    a = ax[i] - x;
    b = ay[i] - y;
    if (a * b > 0) {
      if (a < 0) {
        a = -a;
        b = -b;
      }
      if (a < b) {
        c = a;
        b -= a;
        a = 0;
      } else {
        c = b;
        a -= b;
        b = 0;
      }
      an += a + b + c;

    } else {
      if (a < 0) {
        a = -a;
      }
      if (b < 0) {
        b = -b;
      }
      an += a + b;
    }
    x = ax[i];
    y = ay[i];
  }
  cout << an << endl;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
int main() {
  int x[1005];
  int y[1005];
  int a = 0; //?????????x
  int b = 0; //?????????y
  int w, h, n, z, l;
  int xx = 0;
  int yy = 0;
  int i = 0;
  int v = 0; //?¨???¬

  cin >> w >> h >> n;
  for (i = 0; i < n; i++) {
    cin >> x[i] >> y[i];
  }

  for (i = 0; i < n; i++) {
    while (1) {
      z = (x[i] - a) * (y[i] - b);
      xx = x[i] - a;
      yy = y[i] - b;
      if (xx < 0) {
        xx = 0 - xx;
      }
      if (yy < 0) {
        yy = 0 - yy;
      }

      if (z > 0) {
        if (xx < yy) {
          l = xx;
        } else {
          l = yy;
        }
        v += l;
        a = l;
        b = l;
      } else {

        v = v + xx + yy;
        a = x[i];
        b = y[i];
      }
      if (x[i] == a && y[i] == b) {
        break;
      }
    }
  }
  cout << v;
}
Problem p00517 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
int main() {
  int w, h, n, count = 0;
  cin >> w >> h >> n;
  int x[n], y[n];
  int xx[1], yy[1];
  for (int i = 0; i < n; i++) {
    cin >> x[i] >> y[i];
  }
  xx[0] = x[0];
  yy[0] = y[0];
  for (int a = 1; a < n; a++) {
    while (xx[0] != x[a] || yy[0] != y[a]) {
      if (xx[0] < x[a] && yy[0] < y[a]) {
        xx[0]++;
        yy[0]++;
        count++;
      } else if (xx[0] > x[a] && yy[0] > y[a]) {
        xx[0]--;
        yy[0]--;
        count++;
      } else if (xx[0] < x[a]) {
        xx[0]++;
        count++;
      } else if (yy[0] < y[a]) {
        yy[0]++;
        count++;
      } else if (xx[0] > x[a]) {
        xx[0]--;
        count++;
      } else if (yy[0] > y[a]) {
        yy[0]--;
        count++;
      }
    }
  }
  cout << count << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  int w, h, n, count = 0, i;
  cin >> w >> h >> n;
  int x[n], y[n];
  for (int i = 0; i < n; i++) {
    cin >> y[i] >> x[i];
  }
  for (int i = 1; i < n; i++) {
    while (x[0] != x[i] || y[0] != x[i]) {
      if (x[0] < x[i] && y[0] < y[i]) {
        x[0]++;
        y[0]++;
        count++;
      } else if (x[0] > x[i] && y[0] > y[i]) {
        x[0]--;
        y[0]--;
        count++;
      } else if (x[0] < x[i]) {
        x[0]++;
        count++;
      } else if (y[0] < y[i]) {
        y[0]++;
        count++;
      } else if (x[0] > x[i]) {
        x[0]--;
        count++;
      } else if (y[0] > y[i]) {
        y[0]--;
        count++;
      }
    }
  }
  cout << count << endl;
  return 0;
}
Problem p00517 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <cmath>
#include <iostream>
using namespace std;

typedef pair<int, int> P;
int main() {
  int W, H, N;
  cin >> W >> H >> N;

  P p[1001];
  for (int i = 0; i < N; i++) {
    cin >> p[i].first >> p[i].second;
  }

  int x = p[0].first, y = p[0].second;

  int ans = 0;
  for (int i = 1; i < N; i++) {
    int x2 = p[i].first, y2 = p[i].second;

    if (x <= x2 && y <= y2) {
      int diff = min(abs(x - x2), abs(y - y2));
      x += diff;
      y += diff;
      ans += diff;
    }

    else if (x >= x2 && y >= y2) {
      int diff = min(abs(x - x2), abs(y - y2));
      x -= diff;
      y -= diff;
      ans += diff;
    }

    ans += abs(x - x2) + abs(y - y2);

    x = x2, y = y2;
  }
  cout << ans << endl;

  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cstring>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;

int dx[] = {1, 0, -1, 0, 1, -1};
int dy[] = {0, 1, 0, -1, 1, -1};

class State {
public:
  int x, y, cnt;
  bool operator<(const State &s) const { return cnt > s.cnt; }
};

typedef pair<int, int> P;
const int MAX = 10001;
int nowx, nowy;
int H, W;
bool used[MAX][MAX];

int bfs(int x, int y) {

  memset(used, 0, sizeof(used));
  priority_queue<State> que;
  State t;
  t.x = nowx, t.y = nowy, t.cnt = 0;
  que.push(t);

  while (!que.empty()) {
    State tmp = que.top();
    que.pop();
    nowx = tmp.x;
    nowy = tmp.y;

    if (x == nowx && y == nowy)
      return tmp.cnt;
    if (used[nowy][nowx])
      continue;
    used[nowy][nowx] = true;

    for (int i = 0; i < 6; i++) {
      State tt;
      int nx = nowx + dx[i], ny = nowy + dy[i];
      if (nx < 0 || ny < 0 || nx > W || ny > H)
        continue;
      tt.x = nx, tt.y = ny, tt.cnt = tmp.cnt + 1;
      que.push(tt);
    }
  }
  return -1;
}

int main() {
  int N;
  cin >> W >> H >> N;

  vector<P> v;

  for (int i = 0; i < N; i++) {
    int x, y;
    cin >> x >> y;
    v.push_back(make_pair(x, y));
  }

  int ans = 0;
  nowx = v[0].first - 1, nowy = v[0].second - 1;

  for (int i = 0; i < N; i++) {
    ans += bfs(v[i].first - 1, v[i].second - 1);
    nowx = v[i].first - 1, nowy = v[i].second - 1;
  }

  cout << ans << endl;

  return 0;
}
Problem p00517 (C++) — buggy submission, verdict: Runtime Error

#include <cstring>
#include <iostream>
#include <queue>

using namespace std;
int main() {
  int w, h, n;
  for (; cin >> w >> h >> n;) {
    int x[n], y[n];
    for (int i = 0; i < n; i++) {
      cin >> x[i] >> y[i];
      x[i]--;
      y[i]--;
    }
    int nowx = x[0], nowy = y[0];
    int cnt = 0;
    for (int i = 0; i < n; i++) {
      int diffx = x[i] - nowx;
      int diffy = y[i] - nowy;
      int tmpcnt = 0;
      if (diffx > 0)
        while (diffx > 0 && diffy > 0) {
          diffx--;
          diffy--;
          tmpcnt++;
        }
      else if (diffx < 0)
        while (diffx < 0 && diffy < 0) {
          diffx++;
          diffy++;
          tmpcnt++;
        }
      tmpcnt += max(diffx, -diffx);
      tmpcnt += max(diffy, -diffy);
      nowx = x[i];
      nowy = y[i];
      cnt += tmpcnt;
    }
    cout << cnt << endl;
  }
}

Error: 0

Fixed submission:

#include <cstring>
#include <iostream>
#include <queue>

using namespace std;
int main() {
  int w, h, n;
  for (; cin >> w >> h >> n;) {
    int x[n], y[n];
    for (int i = 0; i < n; i++) {
      cin >> x[i] >> y[i];
      x[i]--;
      y[i]--;
    }
    int nowx = x[0], nowy = y[0];
    int cnt = 0;
    for (int i = 0; i < n; i++) {
      //	  cout<<"Nextpoint:"<<x[i]<<" "<<y[i]<<endl;
      bool closed[w][h];
      memset(closed, true, sizeof(closed));
      queue<pair<pair<int, int>, int>> qu;
      //	  if(!qu.empty())
      //   cout<<"IN!!!"<<endl;
      qu.push(make_pair(make_pair(nowx, nowy), 0));
      while (!qu.empty()) {
        pair<int, int> tmp = qu.front().first;
        int tmpcnt = qu.front().second;
        qu.pop();
        //  cout<<"Now:"<<tmp.first<<" "<<tmp.second<<" "<<tmpcnt<<endl;
        if (tmp.first == x[i] && tmp.second == y[i]) {
          nowx = x[i];
          nowy = y[i];
          cnt += tmpcnt;
          break;
        }
        int dx[] = {0, 0, 1, -1, 1, -1};
        int dy[] = {1, -1, 1, -1, 0, 0};
        for (int jj = 0; jj < 6; jj++) {
          int nextx = tmp.first + dx[jj];
          int nexty = tmp.second + dy[jj];
          if (nextx >= 0 && nextx < w && nexty >= 0 && nexty < h &&
              closed[nextx][nexty]) {
            closed[nextx][nexty] = false;
            qu.push(make_pair(make_pair(nextx, nexty), tmpcnt + 1));
          }
        }
      }
    }
    cout << cnt << endl;
  }
}
Problem p00517 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <vector>
using namespace std;
typedef pair<int, int> P;
typedef long long int ll;

int main() {
  int w, h, n;
  cin >> w >> h >> n;
  int nowx, nowy, ans = 0;
  cin >> nowx >> nowy;
  for (int i = 1; i < n; i++) {
    int x, y;
    cin >> x >> y;
    int sx = nowx - x;
    int sy = nowy - y;
    if ((sx >= 0 && sy >= 0) || (sx < 0 && sy < 0)) {
      sx = abs(sx);
      sy = abs(sy);
      while (sx != 0 && sy != 0) {
        sx--;
        sy--;
        ans++;
      }
      ans += sx;
      ans += sy;
    } else {
      sx = abs(sx);
      sy = abs(sy);
      ans += sx;
      ans += sy;
    }
    nowx = x;
    nowy = y;
  }
  cout << ans << endl;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <queue>
using namespace std;
typedef pair<int, int> P;
typedef pair<int, P> Pii;
int dx[] = {0, 1, 0, -1, 1};
int dy[] = {-1, 0, 1, -1, 1};
int main() {
  int w, h, n;
  cin >> w >> h >> n;
  int nowx, nowy;
  cin >> nowx >> nowy;
  int ans = 0;
  for (int i = 1; i < n; i++) {
    int gx, gy;
    cin >> gx >> gy;
    // bool used[10005][10005]={0};
    queue<Pii> que;
    que.push(Pii(0, P(nowx, nowy)));
    while (!que.empty()) {
      Pii p = que.front();
      que.pop();
      int cou = p.first;
      int x = p.second.first;
      int y = p.second.second;
      if (gx == x && gy == y) {
        ans += cou;
        break;
      }
      // if(used[y][x]) continue;
      // used[y][x] = true;
      for (int v = 0; v < 5; v++) {
        int nx = x + dx[v];
        int ny = y + dy[v];
        if (nx >= 1 && nx <= w && ny >= 1 && ny <= h) {
          que.push(Pii(cou + 1, P(nx, ny)));
        }
      }
    }
  }
  cout << ans << endl;
}