Problem p02584 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <bitset>    // bitset
#include <cctype>    // isupper, islower, isdigit, toupper, tolower
#include <climits>
#include <cstdint> // int64_t, int*_t
#include <cstdio>  // printf
#include <deque>   // deque
#include <functional>
#include <iomanip> //setprecision
#include <iostream>
#include <map> // map
#include <math.h>
#include <queue>         // queue, priority_queue
#include <set>           // set
#include <stack>         // stack
#include <string>        // string, to_string, stoi
#include <tuple>         // tuple, make_tuple
#include <unordered_map> // unordered_map
#include <unordered_set> // unordered_set
#include <utility>       // pair, make_pair
#include <vector>        // vector
using namespace std;
#define ll long long
#define rep(i, n) for (ll i = 0; i < (ll)(n); ++i)
#define mod 1000000007
const int INF = 1000000000;
const int MAX_N = 1000;
const int MAX_M = 1000;
typedef pair<int, int> P;
char maze[MAX_N][MAX_N + 1];
int N, M, a, b, c, e, f;
int sx, sy;
int gx, gy;
int d[MAX_N][MAX_N];
int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
int bfs() {
  queue<P> que;
  que.push(P(sx, sy));
  d[sx][sy] = 0;
  while (que.size()) {
    P p = que.front();
    que.pop();
    if (p.first == gx && p.second == gy)
      break;
    for (int i = 0; i < 4; i++) {
      int nx = p.first + dx[i], ny = p.second + dy[i];
      if (0 <= nx && nx < N && 0 <= ny && ny < M && maze[nx][ny] != 'X' &&
          d[nx][ny] == INF) {
        que.push(P(nx, ny));
        d[nx][ny] = d[p.first][p.second] + 1;
      }
    }
  }
  return d[gx][gy];
}
int main() {
  ll x, k, d, ans;
  cin >> x >> k >> d;
  ll a = abs(x / d);
  // cout<<a;
  if (a > k) {
    cout << abs(abs(x) - d * k);
  } else {
    k -= a;
    x = abs(x) % d;
    if (k % 2 == 0)
      cout << abs(x);
    else
      cout << abs(abs(d - x));
  }
}


Error: TLE

Fixed submission:

#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <bitset>    // bitset
#include <cctype>    // isupper, islower, isdigit, toupper, tolower
#include <climits>
#include <cstdint> // int64_t, int*_t
#include <cstdio>  // printf
#include <deque>   // deque
#include <functional>
#include <iomanip> //setprecision
#include <iostream>
#include <map> // map
#include <math.h>
#include <queue>         // queue, priority_queue
#include <set>           // set
#include <stack>         // stack
#include <string>        // string, to_string, stoi
#include <tuple>         // tuple, make_tuple
#include <unordered_map> // unordered_map
#include <unordered_set> // unordered_set
#include <utility>       // pair, make_pair
#include <vector>        // vector
using namespace std;
#define ll long long
#define rep(i, n) for (ll i = 0; i < (ll)(n); ++i)
#define mod 1000000007
const int INF = 1000000000;
const int MAX_N = 1000;
const int MAX_M = 1000;
typedef pair<int, int> P;
char maze[MAX_N][MAX_N + 1];
int N, M, a, b, c, e, f;
int sx, sy;
int gx, gy;
int d[MAX_N][MAX_N];
int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
int bfs() {
  queue<P> que;
  que.push(P(sx, sy));
  d[sx][sy] = 0;
  while (que.size()) {
    P p = que.front();
    que.pop();
    if (p.first == gx && p.second == gy)
      break;
    for (int i = 0; i < 4; i++) {
      int nx = p.first + dx[i], ny = p.second + dy[i];
      if (0 <= nx && nx < N && 0 <= ny && ny < M && maze[nx][ny] != 'X' &&
          d[nx][ny] == INF) {
        que.push(P(nx, ny));
        d[nx][ny] = d[p.first][p.second] + 1;
      }
    }
  }
  return d[gx][gy];
}
int main() {
  ll x, k, d, ans;
  cin >> x >> k >> d;
  rep(i, k) {
    if (x > 0) {
      x -= d;
      if (x <= 0) {
        if ((k - i - 1) % 2 == 0)
          cout << abs(x);
        else
          cout << abs(x + d);
        return 0;
      }
    } else {
      x += d;
      if (x >= 0) {
        if ((k - 1 - i) % 2 == 0)
          cout << abs(x);
        else
          cout << abs(x - d);
        return 0;
      }
    }
    // cout<<x;
  }
  cout << abs(x);
}