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

#include <algorithm>
#include <bitset>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;
using ll = long long;
using vll = vector<long long>;
using sll = set<long long>;
const long long ll_max = 9223372036854775807;
const long long ll_min = -9223372036854775807;
const int int_max = 2147483647;

template <typename T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }
template <typename T> T lcm(T a, T b) { return a / gcd(a, b) * b; }

template <typename T> map<T, T> getPrimeFactor(T n) {
  map<T, T> ret;
  for (T i = 2; i * i <= n; ++i) {
    while (n % i == 0) {
      ret[i]++;
      n /= i;
    }
  }
  if (n != 1)
    ret[n] = 1;
  return ret;
}

template <typename T> bool IsPrimeNumber(T num) {
  if (num <= 2)
    return true;
  else if (num % 2 == 0)
    return false;

  double sqrtNum = sqrt(num);
  for (int i = 3; i <= sqrtNum; i += 2) {
    if (num % i == 0) {
      return false;
    }
  }

  return true;
}

template <typename T> T modinv(T a, T m) {
  T b = m, u = 1, v = 0;
  while (b) {
    long long t = a / b;
    a -= t * b;
    swap(a, b);
    u -= t * v;
    swap(u, v);
  }
  u %= m;
  if (u < 0)
    u += m;
  return u;
}

// 繰り返し二乗法
ll pow2(ll n, ll p, ll mod) {
  if (p == 0 || n == 1) {
    return 1;
  } else {
    ll ret = pow2(n * n % mod, p / 2, mod);
    if (p % 2 == 1) {
      ret *= n;
    }
    return ret % mod;
  }
}

#define rep(i, s, e) for (ll i = s; i < e; i++)
#define repeq(i, s, e) for (ll i = s; i <= e; i++)

int main() {
  const ll mod = 998244353;
  ll n, k;
  cin >> n >> k;

  vll t(n + 1, 0);
  vector<pair<ll, ll>> vp(k);
  rep(i, 0, k) { cin >> vp[i].first >> vp[i].second; }

  t[1] = 1;
  repeq(i, 2, n) {
    t[i] = t[i - 1];
    rep(j, 0, k) {
      ll tmp = t[max(0LL, i - vp[j].first)] - t[max(0LL, i - vp[j].second - 1)];
      if (tmp < 0) {
        tmp += mod;
      }
      t[i] += tmp;
      t[i] %= mod;
    }
  }

  ll ans = t[n] - t[n - 1];
  if (ans < 0) {
    ans += mod;
  }
  cout << ans << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;
using ll = long long;
using vll = vector<long long>;
using sll = set<long long>;
const long long ll_max = 9223372036854775807;
const long long ll_min = -9223372036854775807;
const int int_max = 2147483647;

template <typename T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }
template <typename T> T lcm(T a, T b) { return a / gcd(a, b) * b; }

template <typename T> map<T, T> getPrimeFactor(T n) {
  map<T, T> ret;
  for (T i = 2; i * i <= n; ++i) {
    while (n % i == 0) {
      ret[i]++;
      n /= i;
    }
  }
  if (n != 1)
    ret[n] = 1;
  return ret;
}

template <typename T> bool IsPrimeNumber(T num) {
  if (num <= 2)
    return true;
  else if (num % 2 == 0)
    return false;

  double sqrtNum = sqrt(num);
  for (int i = 3; i <= sqrtNum; i += 2) {
    if (num % i == 0) {
      return false;
    }
  }

  return true;
}

template <typename T> T modinv(T a, T m) {
  T b = m, u = 1, v = 0;
  while (b) {
    long long t = a / b;
    a -= t * b;
    swap(a, b);
    u -= t * v;
    swap(u, v);
  }
  u %= m;
  if (u < 0)
    u += m;
  return u;
}

// 繰り返し二乗法
ll pow2(ll n, ll p, ll mod) {
  if (p == 0 || n == 1) {
    return 1;
  } else {
    ll ret = pow2(n * n % mod, p / 2, mod);
    if (p % 2 == 1) {
      ret *= n;
    }
    return ret % mod;
  }
}

#define rep(i, s, e) for (ll i = s; i < e; i++)
#define repeq(i, s, e) for (ll i = s; i <= e; i++)

ll n;
vector<pair<ll, ll>> t;
const ll mod = 998244353;
map<ll, ll> dic;
ll func(ll pos) {
  auto itr = dic.find(pos);
  if (itr != dic.end()) {
    return itr->second;
  }
  ll ret = 0;
  for (auto x : t) {
    repeq(y, x.first, x.second) {
      if (pos + y == n) {
        ret++;
      } else if (pos + y < n) {
        ret += func(pos + y);
      }
      ret %= mod;
    }
  }
  dic[pos] = ret;
  return ret;
}

int main() {

  ll k;
  cin >> n >> k;

  t.resize(k);
  rep(i, 0, k) { cin >> t[i].first >> t[i].second; }

  cout << func(1) << endl;
  return 0;
}