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

#include <bits/stdc++.h>
using namespace std;
#define lli long long int
#define nl "\\n";
#define fastio                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define db double
#define mod 1e9 + 7
#define lb lower_bound
#define ub upper_bound
#define vlli vector<lli>
#define mslli multiset<lli>
#define inf INT_MAX
#define sp " "
#define pb push_back
#define pie 3.14159265358979323846
#define test                                                                   \\
  lli t;                                                                       \\
  cin >> t;                                                                    \\
  while (t--)
int main() {
  /*#ifndef ONLINE_JUDGE
      freopen("input.txt", "r", stdin);
      freopen("output.txt", "w", stdout);
#endif

      ios_base::sync_with_stdio(false);
      cin.tie(NULL);*/
  fastio;
  lli x, c, d;
  cin >> x >> c >> d;
  if (x <= 0) {
    /*while(x<0&&c>0)
    {
        x=x+d;
        c--;
    }*/
    lli x1 = abs(x);
    lli t = x1 / d;
    if (t >= c) {
      x = x + (c * d);
      c = 0;
    } else {
      x = x + (t * d);
      c = c - t;
    }
    lli k = abs(x);
    lli k1 = abs(x + d);
    if (c % 2 == 0)
      cout << k;
    else
      cout << k1;
  } else {
    /*while(x>0&&c>0)
    {
        x=x-d;
        c--;
    }*/
    lli t = x / d;
    if (t >= c) {
      x = x - (c * d);
      c = 0;
    } else {
      x = x - (t * d);
      c = c - t;
    }
    lli k = abs(x);
    lli k1 = abs(x - d);
    if (c % 2 == 0)
      cout << k;
    else
      cout << k1;
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define lli long long int
#define nl "\\n";
#define fastio                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define db double
#define mod 1e9 + 7
#define lb lower_bound
#define ub upper_bound
#define vlli vector<lli>
#define mslli multiset<lli>
#define inf INT_MAX
#define sp " "
#define pb push_back
#define pie 3.14159265358979323846
#define test                                                                   \\
  lli t;                                                                       \\
  cin >> t;                                                                    \\
  while (t--)
int main() {
  /*#ifndef ONLINE_JUDGE
      freopen("input.txt", "r", stdin);
      freopen("output.txt", "w", stdout);
#endif

      ios_base::sync_with_stdio(false);
      cin.tie(NULL);*/
  // fastio;
  lli x, c, d;
  cin >> x >> c >> d;
  if (x <= 0) {
    while (x < 0 && c > 0) {
      x = x + d;
      c--;
    }
    lli k = abs(x);
    lli k1 = abs(x - d);
    if (c % 2 == 0)
      cout << k;
    else
      cout << k1;
  } else {
    while (x > 0 && c > 0) {
      x = x - d;
      c--;
    }
    lli k = abs(x);
    lli k1 = abs(x + d);
    if (c % 2 == 0)
      cout << k;
    else
      cout << k1;
  }
}