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

#include <algorithm>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <numeric>
#include <string>
#include <vector>
#define _USE_MATH_DEFINES
#include <cmath>
#include <math.h>

#define rep(i, m, n) for (int i = (m); i < (n); ++i)
#define rem(i, m, n) for (int i = (m); i > (n); --i)
#define MOD 1000000007

using namespace std;

using ll = long long;
using std::swap;

// vector<long long int> v;
vector<long long int> w;

int main() {
  ll N, K, MA, MI;
  ll ANS = 0;
  cin >> N >> K;
  rep(i, K, N + 2) {
    MA = 0;
    MI = 0;
    MI = (0 + (i - 1)) * ll(i) / 2;
    MA = (N + (N - i + 1)) * ll(i) / 2;
    ANS += (MA - MI + 1) % MOD;
    ANS %= MOD;
  }
  ANS %= MOD;
  cout << ANS << '\\n';

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <numeric>
#include <string>
#include <vector>
#define _USE_MATH_DEFINES
#include <cmath>
#include <math.h>

#define rep(i, m, n) for (int i = (m); i < (n); ++i)
#define rem(i, m, n) for (int i = (m); i > (n); --i)
#define MOD 1000000007

using namespace std;

using ll = long long;
using std::swap;

// vector<long long int> v;
vector<long long int> w;

int main() {
  ll N, K, MA, MI;
  ll ANS = 0;
  cin >> N >> K;
  rep(i, K, N + 2) {
    MA = 0;
    MI = 0;
    rep(j, 0, i) MI += j;
    rem(j, N, N - i) MA += j;
    // MI = (0 + (i - 1))*i / 2;
    // MA = (N + (N - i + 1))*i / 2;
    // cout << MI << " " << MA << '\\n';
    ANS += (MA - MI + 1) % MOD;
    ANS %= MOD;
  }
  ANS %= MOD;
  cout << ANS << '\\n';

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

#include <cstring>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

int main(int argc, char *argv[]) {
  long long int i, j;
  long long int k, n;
  long long int result = 0;
  long long int tmp_pattern_max, tmp_pattern_min;
  tmp_pattern_max = tmp_pattern_min = 0;
  cin >> n >> k;

  for (i = k; i <= n + 1; i++) {
    tmp_pattern_max = i * (n - i + 1 + n) / 2;
    tmp_pattern_min = i * (0 + i - 1) / 2;

    result += tmp_pattern_max - tmp_pattern_min + 1;
  }

  cout << result % (1000000000 + 7) << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <cstring>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

int main(int argc, char *argv[]) {
  int i, j;
  int k, n;
  long long int result = 0;
  long long int tmp_pattern_max, tmp_pattern_min;
  tmp_pattern_max = tmp_pattern_min = 0;
  cin >> n >> k;

  for (i = k; i <= n + 1; i++) {
    tmp_pattern_max = tmp_pattern_min = 0;
    for (j = 0; j < i; j++) {
      tmp_pattern_max += n - j;
      tmp_pattern_min += j;
    }

    result += tmp_pattern_max - tmp_pattern_min + 1;
  }

  cout << result % (1000000000 + 7) << endl;

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

#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define pb(x) push_back(x)
#define PI pair<int, int>
#define PL pair<long int, long int>
#define PLL pair<ll, ll>
#define Tcase                                                                  \\
  int t;                                                                       \\
  cin >> t;                                                                    \\
  while (t--)
#define MOD 1000000007
ll sum = 0;
int main() {
  int n, k;
  cin >> n >> k;
  ll first = 0, last = 0;
  for (int i = 0; i < k; i++) {
    first += i;
  }
  for (int i = 0; i < k; i++) {
    last += (n - i);
  }
  sum = ((sum) % MOD + (last - first + 1) % MOD) % MOD;
  // cout<<"wa      "<<first<<"  "<<last<<endl;
  for (int i = k + 1; i <= n + 1; i++) {
    int f = i;
    first += i - 1;
    last += n - i + 1;
    /*for(int i=0;i<f;i++){
    first+=i;
    }
    for(int i=0;i<f;i++){
    last+=(n-i);
    }*/
    // cout<<"wa  "<<first<<"    "<<last<<"\\n";
    sum = ((sum) % MOD + (last - first + 1) % MOD) % MOD;
  }
  cout << sum << "\\n";
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define pb(x) push_back(x)
#define PI pair<int, int>
#define PL pair<long int, long int>
#define PLL pair<ll, ll>
#define Tcase                                                                  \\
  int t;                                                                       \\
  cin >> t;                                                                    \\
  while (t--)
#define MOD 1000000007
ll sum = 0;
int main() {
  int n, k;
  cin >> n >> k;
  for (int i = k; i <= n + 1; i++) {
    int f = i;
    ll first = 0, last = 0;
    for (int i = 0; i < f; i++) {
      first += i;
    }
    for (int i = 0; i < f; i++) {
      last += (n - i);
    }
    sum = ((sum) % MOD + (last - first + 1) % MOD) % MOD;
  }
  cout << sum << "\\n";
  return 0;
}
Problem p02708 (C++) — buggy submission, verdict: Time Limit Exceeded

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

const int MOD = 1000000007;

int main() {
  int N, K;
  cin >> N >> K;
  // long long sum = 0;
  vector<int> sum(N + 1, 0);

  // 計算例
  // for(int i=K; i<=N+1; ++i){
  //     long long max=0;
  //     long long min=0;
  //     for(int j=0; j<i; ++j){
  //         min += j;
  //     }
  //     for(int j=N; j> N-i; --j){
  //         max += j;
  //     }
  //     sum += (max - min+1);
  // }
  long long cumsum = 1;
  sum[K - 1] = 1;
  for (int i = K; i <= N; ++i) {
    cumsum += i;
    sum[i] = (sum[i - 1] + cumsum) % MOD;
  }

  long long res = sum[N] % MOD;
  cout << res << endl;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

const int MAX = 510000;
const int MOD = 1000000007;

int main() {
  int N, K;
  cin >> N >> K;
  long long sum = 0;

  // 計算例
  for (int i = K; i <= N + 1; ++i) {
    int max = 0;
    int min = 0;
    for (int j = 0; j < i; ++j) {
      min += j;
    }
    for (int j = N; j > N - i; --j) {
      max += j;
    }
    sum += (max - min + 1);
  }
  cout << sum << endl;
}
Problem p02708 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rrep(i, m, n) for (int i = (m); i < (n); i++)
#define fore(i, a) for (auto &i : a)
#define debug(a, b) cout << "debug : " << a << " , " << b << endl
using ll = long long;
const ll INF = 1LL << 60;
const ll mod = 1E9 + 7;

ll next_comb(ll nCk, ll n, ll k, ll mod) {
  return ((n - k) * nCk / (k + 1)) % mod;
}

ll kaizyo(ll n) {
  if (n == 1)
    return (ll)1;
  return n * kaizyo(n - 1);
}

int main() {
  ll n, k;
  cin >> n >> k;
  n = n + 1;
  vector<ll> r(n + 1, 0);
  rrep(i, 1, n) r[i + 1] = i + r[i];
  ll sum = 0;
  for (int i = k; i <= n; i++) {
    sum += (r[n] - r[n - i] - r[i] + 1);
    sum %= mod;
    // debug("sum",sum);
  }
  cout << sum << endl;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rrep(i, m, n) for (int i = (m); i < (n); i++)
#define fore(i, a) for (auto &i : a)
#define debug(a, b) cout << "debug : " << a << " , " << b << endl
using ll = long long;
const ll INF = 1LL << 60;
const ll mod = 1E9 + 7;

ll next_comb(ll nCk, ll n, ll k, ll mod) {
  return ((n - k) * nCk / (k + 1)) % mod;
}

ll kaizyo(ll n) {
  if (n == 1)
    return (ll)1;
  return n * kaizyo(n - 1);
}

int main() {
  ll n, k;
  cin >> n >> k;
  n = n + 1;
  ll N = (n - 1) * n / 2;
  ll sum = 0, nck = kaizyo(n) / kaizyo(k) / kaizyo(n - k);
  for (int i = k; i <= n; i++) {
    sum += nck;
    sum %= mod;
    for (int j = 0; j < (ll)(n - i + 1) / 2; j++) {
      sum -= pow(j, k / 2) + 1;
    }
    nck = next_comb(nck, n, k, mod);
  }
  cout << sum << endl;
}
Problem p02708 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define ll long long int

ll sum(ll a, ll b) {
  ll count = 0;
  count = (b - a + 1) * (a + b) / 2;
  return count;
}
int main(void) {
  ll N = 0, M = 0;
  cin >> N >> M;
  ll s = 0;

  for (ll i = M; i <= N + 1; i++) {
    s += (sum(N + 1 - i, N) - sum(0, i - 1) + 1);
  }

  cout << s % 1000000007 << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long int

ll sum(ll a, ll b) {
  ll count = 0;
  for (ll i = a; i <= b; i++)
    count += i;
  return count;
}
int main(void) {
  ll N = 0, M = 0;
  cin >> N >> M;
  ll s = 0;

  for (ll i = M; i <= N + 1; i++) {
    s += (sum(N + 1 - i, N) - sum(0, i - 1) + 1);
  }

  cout << s % 1000000007 << endl;
}
Problem p02708 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {

  const ll mod = 1000000007;
  ll n, k;
  cin >> n >> k;
  ll ans = 0;
  for (ll i = k; i <= n + 1; i++) {
    ll tmpLeft = (i - 1) * i / 2;
    ll tmpRight = n * (n + 1) / 2 - (n - i) * (n - i + 1) / 2;
    ans += tmpRight - tmpLeft + 1;
    ans %= mod;
  }
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

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

int main() {

  const ll mod = 1000000007;
  ll n, k;
  cin >> n >> k;
  ll ans = 0;
  for (ll i = k; i <= n + 1; i++) {
    ll tmpLeft = 0;
    for (ll j = 0; j < i; j++) {
      tmpLeft += j;
    }
    ll tmpRight = 0;
    for (ll j = n - i + 1; j <= n; j++) {
      tmpRight += j;
    }
    ans += tmpRight - tmpLeft + 1;
    ans %= mod;
  }
  cout << ans << endl;
  return 0;
}
Problem p02708 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;

int main() {
  long long int N, K;
  long long int sum = 0;
  long long int memo[200000] = {0};
  long long int memo2[200000] = {0};
  long long int a = 0, b = 0, c = 0, d = 0;
  cin >> N >> K;

  if (N + 1 == K) {
    cout << 1;
    return 0;
  }

  for (int i = 1; i < N + 1; i++) {
    memo[i] = memo[i - 1] + i;
    memo2[i] = memo2[i - 1] + N + 1 - i;
    // cout << memo[i] << "    " << memo2[i] << endl;
  }
  for (int i = K; i < N + 1; i++) {
    sum += memo2[i] - memo[i - 1] + 1;
    while (sum > 1000000007) {
      sum -= 1000000007;
    }
    // cout << sum << endl;
  }
  cout << sum + 1;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

int main() {
  long long int N, K;
  long long int sum = 0;
  long long int a = 0, b = 0;
  cin >> N >> K;

  if (N + 1 == K) {
    cout << 1;
    return 0;
  }
  for (long long int i = K; i < N + 1; i++) {
    for (long long int j = 0; j < i; j++) {
      a += j;
      b += N - j;
    }
    sum += b - a + 1;
    while (sum > 1000000007) {
      sum -= 1000000007;
    }

    a = 0;
    b = 0;
    // cout << N<<"   "<<i<<"    "<<fire(N, i) << endl;
  }
  cout << sum + 1;
}
Problem p02708 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  long long N, K;
  cin >> N >> K;
  long long p;
  p = 0;
  for (long long i = K; i <= N + 1; i++) {
    long long q, r;
    q = (0 + i - 1) * i / 2;
    r = (N + N - i + 1) * i / 2;
    p += (r - q) + 1;
    p %= 1000000007;
  }
  cout << p;
}

Error: TLE

Fixed submission:

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

int main() {
  long long N, K;
  cin >> N >> K;
  long long p;
  p = 0;
  for (int i = K; i <= N + 1; i++) {
    long long q, r;
    q = 0;
    r = 0;
    for (int j = 1; j <= i; j++) {
      q += j - 1;
      r += (N - j + 1);
    }
    p += (r - q) + 1;
    p %= 1000000007;
  }
  cout << p;
}
Problem p02708 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; ++i)
#define rrep(i, n) for (int i = n - 1; i >= 0; --i)
#define fi first
#define se second
using namespace std;
using lint = long long;
using uint = unsigned int;
using ulint = unsigned long long;
using ldouble = long double;
using pii = pair<int, int>;
using pli = pair<lint, lint>;
using pdd = pair<double, double>;
using pld = pair<ldouble, ldouble>;
using v1i = vector<int>;
using v1li = vector<lint>;
using v2i = vector<vector<int>>;
using v2li = vector<vector<lint>>;
using v3i = vector<vector<vector<int>>>;
using v3li = vector<vector<vector<lint>>>;
using v1b = vector<bool>;
using v2b = vector<vector<bool>>;
using v3b = vector<vector<vector<bool>>>;
using v1c = vector<char>;
using v2c = vector<vector<char>>;
using v3c = vector<vector<vector<char>>>;
constexpr lint mod1 = 1e9 + 7;
constexpr lint mod2 = 998244353;

int main() {
  lint n, k, s = 0;
  cin >> n >> k;
  ++n;
  v1li v(n + 1);
  v[0] = 0;
  rep(i, n) v[i + 1] = v[i] + i + 1;
  for (int i = k; i <= n; ++i) {
    s += v[n] - v[n - i] - v[i] + 1;
    s %= mod1;
  }
  cout << s << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; ++i)
#define rrep(i, n) for (int i = n - 1; i >= 0; --i)
#define fi first
#define se second
using namespace std;
using lint = long long;
using uint = unsigned int;
using ulint = unsigned long long;
using ldouble = long double;
using pii = pair<int, int>;
using pli = pair<lint, lint>;
using pdd = pair<double, double>;
using pld = pair<ldouble, ldouble>;
using v1i = vector<int>;
using v1li = vector<lint>;
using v2i = vector<vector<int>>;
using v2li = vector<vector<lint>>;
using v3i = vector<vector<vector<int>>>;
using v3li = vector<vector<vector<lint>>>;
using v1b = vector<bool>;
using v2b = vector<vector<bool>>;
using v3b = vector<vector<vector<bool>>>;
using v1c = vector<char>;
using v2c = vector<vector<char>>;
using v3c = vector<vector<vector<char>>>;
constexpr lint mod1 = 1e9 + 7;
constexpr lint mod2 = 998244353;

int main() {
  lint n, k, s = 0;
  cin >> n >> k;
  ++n;
  v1li v(n + 1);
  v[0] = 0;
  rep(i, n) v[i + 1] = v[i] + i + 1;
  for (int i = k; k <= n; ++i) {
    s += v[n] - v[n - k] - v[k];
    s %= mod1;
  }
  cout << s << endl;
  return 0;
}
Problem p02708 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>

using namespace std;

int main() {
  long int n, k, min, max;
  long int ans = 0;
  const long int MOD = 1000000007;
  cin >> n >> k;
  for (long int i = k; i <= n + 1; ++i) {
    min = (i - 1) * i / 2;
    max = (n + 1) * n / 2 - (n - i) * (n - i + 1) / 2;
    ans += (max - min + 1);
    ans %= MOD;
  }
  cout << ans;
}


Error: -8

Fixed submission:

#include <iostream>

using namespace std;

int main() {
  int a, b;
  cin >> a >> b;
  long int c = 1;
  long int h = 0;
  const int d = a + 1;
  const int g = a - b + 2;
  long int e[g];
  long int f[g];
  for (int j = b; j <= d; j++) {
    for (int i = 0; i < j; i++) {
      e[i] = d - i;
      f[i] = i + 1;
      c *= e[i];
      c %= 1000000007;
      h *= f[i];
      h %= 1000000007;
    }
  }
  c /= h;
  cout << c;
}
Problem p02708 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>

using namespace std;
using ll = long long;

int main() {
  auto const M = (int)1e9 + 7;
  ll N, K, R = 0;
  cin >> N >> K;

  for (ll k = K; k <= (N + 1); ++k) {
    auto const t = k * (N - k + 1) + 1;
    R = (R + t) % M;
  }

  cout << R << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

inline int factorial(int N) {
  int r = 0;
  for (int i = 1; i <= N; ++i) {
    r += i;
  }
  return r;
}

long mod_pow(long a, long b, long M) {
  if (b == 0) {
    return 1;
  } else if (b % 2 == 0) {
    auto const d = mod_pow(a, b / 2, M);
    return (d * d) % M;
  } else {
    return (a * mod_pow(a, b - 1, M)) % M;
  }
}

int main() {
  auto const M = 1000000007L;
  auto const C = mod_pow(10, 100, M);
  int N, K;
  cin >> N >> K;
  vector<bool> v(M);

  int fact = factorial(K - 1);
  for (long a = K; a <= (N + 1); ++a) {
    int const U = a * N - fact;
    for (long b = fact; b <= U; ++b) {
      auto const d = (a * C + b) % M;
      v[d] = true;
    }
    fact += a;
  }

  cout << count(v.begin(), v.end(), true) << endl;
  return 0;
}
Problem p02708 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

#define rep(i, n) for (int i = 0; i < (n); i++)
#define rrep(i, n) for (int i = (n)-1; i >= 0; i--)
#define rep1(i, n) for (int i = 1; i <= (n); i++)
#define rrep1(i, n) for (int i = (n); i > 0; i--)

#define ll long long
#define pi pair<int, int>
#define pll pair<ll, ll>

#define MOD 1000000007
#define INF 1000000000000000LL

using namespace std;

int main() {
  ll n, k;
  cin >> n >> k;

  ll result = 0;
  ll fk = 0;
  ll lk = 0;
  rep(i, k - 1) fk += i;
  for (int i = n; i > n - k + 1; i--)
    lk += i;

  for (int i = k; i <= n + 1; i++) {
    fk += i - 1;
    lk += n - i + 1;
    ll c = lk - fk + 1;
    result += c;
    result %= MOD;
    //    cout<<i<<' '<<fk<<' '<<lk<<endl;
  }

  cout << result << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#define rep(i, n) for (int i = 0; i < (n); i++)
#define rrep(i, n) for (int i = (n)-1; i >= 0; i--)
#define rep1(i, n) for (int i = 1; i <= (n); i++)
#define rrep1(i, n) for (int i = (n); i > 0; i--)

#define ll long long
#define pi pair<int, int>
#define pll pair<ll, ll>

#define MOD 1000000007
#define INF 1000000000000000LL

using namespace std;

int main() {
  ll n, k;
  cin >> n >> k;

  ll result = 0;
  for (int i = k; i <= n + 1; i++) {
    ll fk = 0;
    rep(j, i) fk += j;
    //    rep(j, i)cout<<j<<' ';
    ll lk = 0;
    for (int j = n; j > n - i; j--)
      lk += j;
    //    for(int j=n; j>n-i; j--)cout<<j<<' ';
    ll c = lk - fk + 1;
    //    cout<<i<<' '<<fk<<' '<<lk<<endl;
    result += c;
    result %= MOD;
  }

  cout << result << endl;

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

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define all(x) x.begin(), x.end()
#define int ll
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const int MOD = 1e9 + 7;

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  int n, k;
  cin >> n >> k;
  int ans = 0;
  for (int i = k; i <= n + 1; i++) {
    int minu = i * (i - 1) / 2;
    // int minu = 0; for(int j=0;j<i;j++) minu += j;
    int mxnu = i * (2 * n - i + 1) / 2;
    // int mxnu = 0; for(int j=0;j<i;j++) mxnu += (n-j);
    ans += (mxnu - minu + 1) % MOD;
    ans %= MOD;
  }
  cout << ans << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define all(x) x.begin(), x.end()
#define int ll
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const int MOD = 1e9 + 7;

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  int n, k;
  cin >> n >> k;
  int ans = 0;
  for (int i = k; i <= n + 1; i++) {
    int minu = 0;
    for (int j = 0; j < i; j++)
      minu += j;
    int mxnu = 0;
    for (int j = 0; j < i; j++)
      mxnu += (n - j);
    ans += (mxnu - minu + 1) % MOD;
    ans %= MOD;
  }
  cout << ans << endl;

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

#include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <string.h>
#include <string>
#include <vector>
using namespace std;

#define IOS                                                                    \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define endl "\\n"
#define int long long
#define PI 3.14159265358979323846264338327950L

#define INF 0x3f3f3f3f
// const int N = 2e5 + 5;

int sum(int l, int r) { return (l + r) * (r - l + 1) / 2; }

int32_t main() {

  IOS;

  int n, k;

  cin >> n >> k;

  int ans = 0;

  for (int i = k; i <= n + 1; i++) {

    int l = sum(0, i - 1);

    int r = sum(n - i + 1, n);

    ans += r - l + 1;

    ans %= 1000000007;
  }

  cout << ans << endl;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <string.h>
#include <string>
#include <vector>
using namespace std;

#define IOS                                                                    \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define endl "\\n"
#define int long long
#define PI 3.14159265358979323846264338327950L

#define INF 0x3f3f3f3f
// const int N = 2e5 + 5;

#define mod 1000000007
int32_t main() {

  IOS;

  // int t;

  // cin >> t;

  // while (t--) {

  int n, k;

  cin >> n >> k;

  int ans = 0;

  for (int i = k; k <= n + 1; i++) {

    int mini = ((i - 1) * (i) / 2) + 1;

    int maxi = (2 * n - i + 1) * i / 2;

    ans = (ans + (maxi - mini)) % mod;
  }

  cout << ans << endl;

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

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
typedef long long ll;
const int MOD = 1000000007;
int main() {
  ll n, k;
  cin >> n >> k;
  ll cnt = 0;
  for (int j = k; j <= n + 1; j++) {
    // ll min=(j-1)*j/2;
    // ll max=(n+n-j+1)*j/2;
    // for(int i=0; i<j;i++){
    //   min+=i;
    // }
    // cout << min << endl;
    // for(int i=n-j+1; i<n+1;i++){
    //   max+=i;
    // }
    // cout << max << endl;
    // cnt = (cnt + max -min+1)%MOD;
    cnt = (cnt + (n - j + 1) * j + 1) % MOD;
    // cout << (n-j+1)*j << endl;
    // cout << max - min << endl;
  }
  cout << cnt << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
typedef long long ll;
const int MOD = 1000000007;
int main() {
  ll n, k;
  cin >> n >> k;
  ll cnt = 0;
  for (int j = k; j <= n + 1; j++) {
    ll min = 0;
    ll max = 0;
    for (int i = 0; i < j; i++) {
      min += i;
    }
    for (int i = n - j + 1; i < n + 1; i++) {
      max += i;
    }
    cnt = (cnt + max - min + 1) % MOD;
  }
  cout << cnt << endl;
}
Problem p02708 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <chrono>
#include <cmath>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;
typedef long long lint;
typedef vector<int> vi;
typedef pair<int, int> pii;

typedef vector<lint> vl;
typedef pair<lint, lint> pll;

#define mpr make_pair

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);

  lint n, k;
  cin >> n >> k;

  lint mod = 1e9 + 7;
  lint res = 0;
  for (lint cnt = k; cnt <= n + 1; ++cnt) {
    lint min = 0;
    // for (int i=0; i<cnt; ++i) min = (min + i)%mod;
    min = cnt * (cnt - 1) / 2;

    lint max = 0;
    max = (n + n - cnt + 1) * cnt / 2;
    // for (int i = n; i>=n-cnt+1; --i) max= (max+i)%mod;

    res = (res + max - min + 1) % mod;
  }
  cout << res;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <chrono>
#include <cmath>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;
typedef long long lint;
typedef vector<int> vi;
typedef pair<int, int> pii;

typedef vector<lint> vl;
typedef pair<lint, lint> pll;

#define mpr make_pair

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);

  lint n, k;
  cin >> n >> k;

  lint mod = 1e9 + 7;
  lint res = 0;
  for (int cnt = k; cnt <= n + 1; ++cnt) {
    lint min = 0;
    for (int i = 0; i < cnt; ++i)
      min = (min + i) % mod;

    lint max = 0;
    for (int i = n; i >= n - cnt + 1; --i)
      max = (max + i) % mod;

    res = (res + max - min + 1) % mod;
  }
  cout << res;
}