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

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef vector<ll> vi;
typedef vector<vector<ll>> vvi;

#define deb(x) cerr << #x << ":" << x << "\\n";
#define dvi(x)                                                                 \\
  for (ll i = 0; i < x.size(); i++)                                            \\
    cerr << #x << "[" << i << "]:" << x[i] << " ";
#define iter(v) for (auto it : v)
#define wt                                                                     \\
  ll t;                                                                        \\
  cin >> t;                                                                    \\
  while (t--)
#define rl(x)                                                                  \\
  ll x;                                                                        \\
  cin >> x;
#define rvi(x, n)                                                              \\
  vi x(n);                                                                     \\
  for (ll i = 0; i < n; i++)                                                   \\
    cin >> x[i];
#define rs(x)                                                                  \\
  string x;                                                                    \\
  cin >> x;
#define w(x) cout << x << endl;
#define wvi(x)                                                                 \\
  for (ll i = 0; i < x.size(); i++)                                            \\
    cout << x[i] << " ";                                                       \\
  cout << endl;
#define ws(x) cout << x << endl;
#define all(v) v.begin(), v.end()
#define pb push_back
#define ppb pop_back
#define pll pair<ll, ll>
#define fo(i, n) for (ll i = 0; i < n; i++)
#define forr(i, a, b) for (ll i = a; i < b; i++)
#define yes cout << "YES\\n";
#define no cout << "NO\\n";
const ll N = 1e9 + 7;

map<long long, long long>
factorize(long long n) { // returns map containing factor and multiplicity, Eg:
                         // 60 = {{2,2},{3,1},{5,1}}
  map<long long, long long> ans;
  for (long long i = 2; i * i <= n; i++) {
    while (n % i == 0) {
      ans[i]++;
      n /= i;
    }
  }
  if (n > 1) {
    ans[n]++;
    n = 1;
  }
  return ans;
}

void FAST() { // FAST Input Output, DONT use both scnaf,prllf / cin,cout , Use
              // any ONE system
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
}

ll powM(ll x, ll y, ll m) { // returns (a^b)%m ( ^ is exponent )
  ll ans = 1, r = 1;
  x %= m;
  while (r > 0 && r <= y) {
    if (r & y) {
      ans *= x;
      ans %= m;
    }
    r <<= 1;
    x *= x;
    x %= m;
  }
  return ans;
}

int main() {
  FAST();
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
//    freopen("output.txt", "w", stdout);
#endif
  //	wt{
  rl(x) rl(k) rl(d) if (x > 0) {
    if (k > x / d) {
      k -= x / d;
      if (x % d < abs(x % d - d))
        x = x % d;
      else
        x = x % d - d, k--;
    } else {
      x -= k * d;
      cout << abs(x);
      exit(0);
    }
  }
  else {
    if (k > abs(x) / d) {
      k -= abs(x) / d;
      if (abs(x) % d)
        x = (abs(x) % d) * -1;
      else {
        x = abs(abs(x) % d - d);
        k--;
      }
    } else {
      x += k * d;
      cout << abs(x);
      exit(0);
    }
  }
  if (k % 2 == 0)
    cout << abs(x);
  else {
    if (abs(x - d) > abs(x + d))
      cout << abs(x + d);
    else
      cout << abs(x - d);
  }
  //	}
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef vector<ll> vi;
typedef vector<vector<ll>> vvi;

#define deb(x) cerr << #x << ":" << x << "\\n";
#define dvi(x)                                                                 \\
  for (ll i = 0; i < x.size(); i++)                                            \\
    cerr << #x << "[" << i << "]:" << x[i] << " ";
#define iter(v) for (auto it : v)
#define wt                                                                     \\
  ll t;                                                                        \\
  cin >> t;                                                                    \\
  while (t--)
#define rl(x)                                                                  \\
  ll x;                                                                        \\
  cin >> x;
#define rvi(x, n)                                                              \\
  vi x(n);                                                                     \\
  for (ll i = 0; i < n; i++)                                                   \\
    cin >> x[i];
#define rs(x)                                                                  \\
  string x;                                                                    \\
  cin >> x;
#define w(x) cout << x << endl;
#define wvi(x)                                                                 \\
  for (ll i = 0; i < x.size(); i++)                                            \\
    cout << x[i] << " ";                                                       \\
  cout << endl;
#define ws(x) cout << x << endl;
#define all(v) v.begin(), v.end()
#define pb push_back
#define ppb pop_back
#define pll pair<ll, ll>
#define fo(i, n) for (ll i = 0; i < n; i++)
#define forr(i, a, b) for (ll i = a; i < b; i++)
#define yes cout << "YES\\n";
#define no cout << "NO\\n";
const ll N = 1e9 + 7;

map<long long, long long>
factorize(long long n) { // returns map containing factor and multiplicity, Eg:
                         // 60 = {{2,2},{3,1},{5,1}}
  map<long long, long long> ans;
  for (long long i = 2; i * i <= n; i++) {
    while (n % i == 0) {
      ans[i]++;
      n /= i;
    }
  }
  if (n > 1) {
    ans[n]++;
    n = 1;
  }
  return ans;
}

void FAST() { // FAST Input Output, DONT use both scnaf,prllf / cin,cout , Use
              // any ONE system
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
}

ll powM(ll x, ll y, ll m) { // returns (a^b)%m ( ^ is exponent )
  ll ans = 1, r = 1;
  x %= m;
  while (r > 0 && r <= y) {
    if (r & y) {
      ans *= x;
      ans %= m;
    }
    r <<= 1;
    x *= x;
    x %= m;
  }
  return ans;
}

int main() {
  FAST();
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
//    freopen("output.txt", "w", stdout);
#endif
  //	wt{
  rl(x) rl(k) rl(d) while (abs(x - d) > abs(x + d) && k) {
    x = x + d;
    k--;
  }
  while (abs(x - d) < abs(x + d) && k) {
    x = x - d;
    k--;
  }
  if (k % 2 == 0)
    cout << abs(x);
  else {
    if (abs(x - d) > abs(x + d))
      cout << abs(x + d);
    else
      cout << abs(x - d);
  }
  //	}
}