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

#include <algorithm>
#include <assert.h>
#include <bitset>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <list>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <time.h>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;

typedef long long ll;
typedef vector<long long> vi;
typedef pair<long long, long long> ii;
typedef vector<pair<long long, long long>> vii;
typedef stringstream ss;
ll mil() { return numeric_limits<long long>::min(); }
ll mal() { return numeric_limits<long long>::max(); }

#define R3(i, a, b) for (long long i = (a); i < (b); ++i)
#define R2(i, b) for (long long i = 0; i < (b); ++i)
#define R1(b) for (long long i = 0; i < (b); ++i)
#define RR(i, a, b) for (long long i = a; i > b; --i)
#define PB(x) push_back(x)
#define MP(x, y) make_pair(x, y)

istringstream lcin;
void rl() {
  string s;
  getline(cin, s);
  lcin = istringstream(s);
}
// build: ctrl + f9

// run: shift + f10
// debug: shift + f9
// sanitize: shift + f11

ll mods(ll a, ll b, ll m) { return (a % m + b % m) % m; }

ll modp(ll a, ll b, ll m) { return ((a % m) * (b % m)) % m; }

ll modd(ll a, ll b, ll m) { return ((a % m) / (b % m)) % m; }

ll Cnm(ll c, ll n, ll m) {
  ll tot = 1;
  R1(c) { tot = (tot * (n - i) % m) % m; }
  ll div = 1;
  R1(c) { div = (div * (i + 1) % m) % m; }

  return (tot / div) % m;
}

ll cmin(ll i, ll N, ll m) {
  /*ll tot = 0;
  for(int j = 0; j < i; ++j) {
      tot = (tot + j%m)%m;
  }*/

  return ((i - 1) * (i) / 2);

  // return tot;
}

ll cmax(ll i, ll N, ll m) {
  /*ll tot = 0;
  for(int j = N + 1 - i; j <= N; ++j) {
      tot = (tot + j%m)%m;
  }*/

  // return tot;
  ll tot = ((N) * (N + 1) / 2) - ((N - i) * (N - i + 1)) / 2;
  return tot;
}

int main(int, char **) {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  ll N, K;

  cin >> N >> K;

  ll m = 1000000007;

  ll tot = 0;

  for (ll i = K; i <= N + 1; ++i) {
    tot = (tot + (cmax(i, N, m) - cmin(i, N, m) + 1) % m) % m;
  }

  cout << tot << endl;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <assert.h>
#include <bitset>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <list>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <time.h>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;

typedef long long ll;
typedef vector<long long> vi;
typedef pair<long long, long long> ii;
typedef vector<pair<long long, long long>> vii;
typedef stringstream ss;
ll mil() { return numeric_limits<long long>::min(); }
ll mal() { return numeric_limits<long long>::max(); }

#define R3(i, a, b) for (long long i = (a); i < (b); ++i)
#define R2(i, b) for (long long i = 0; i < (b); ++i)
#define R1(b) for (long long i = 0; i < (b); ++i)
#define RR(i, a, b) for (long long i = a; i > b; --i)
#define PB(x) push_back(x)
#define MP(x, y) make_pair(x, y)

istringstream lcin;
void rl() {
  string s;
  getline(cin, s);
  lcin = istringstream(s);
}
// build: ctrl + f9

// run: shift + f10
// debug: shift + f9
// sanitize: shift + f11

ll mods(ll a, ll b, ll m) { return (a % m + b % m) % m; }

ll modp(ll a, ll b, ll m) { return ((a % m) * (b % m)) % m; }

ll modd(ll a, ll b, ll m) { return ((a % m) / (b % m)) % m; }

ll Cnm(ll c, ll n, ll m) {
  ll tot = 1;
  R1(c) { tot = (tot * (n - i) % m) % m; }
  ll div = 1;
  R1(c) { div = (div * (i + 1) % m) % m; }

  return (tot / div) % m;
}

ll cmin(ll i, ll N, ll m) {
  ll tot = 0;
  for (int j = 0; j < i; ++j) {
    tot = (tot + j % m) % m;
  }

  return tot;
}

ll cmax(ll i, ll N, ll m) {
  ll tot = 0;
  for (int j = N + 1 - i; j <= N; ++j) {
    tot = (tot + j % m) % m;
  }

  return tot;
}

int main(int, char **) {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  ll N, K;

  cin >> N >> K;

  ll m = 1000000007;

  ll tot = 0;

  for (ll i = K; i <= N + 1; ++i) {
    tot = (tot + (cmax(i, N, m) - cmin(i, N, m) + 1) % m) % m;
  }

  cout << tot << endl;
}