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

/*
  Task: I. Kevin and Grid
  Link: http://codeforces.com/contest/1392/problem/I
  Lang: cpp17
*/
#include <bits/stdc++.h>
using namespace std;

#define eb emplace_back
#define pb push_back
#define INF LLONG_MAX
#define FLT_INF pow(10.0, 16)

using ll = long long;
using vll = vector<ll>;
using ull = unsigned long long;
using int2 = pair<ll, ll>;
using int3 = tuple<ll, ll, ll>;

template <typename A> string to_string(const A &v);
string to_string(const string &s) { return '"' + s + '"'; }
string to_string(const char *s) { return to_string((string)s); }
string to_string(bool b) { return (b ? "true" : "false"); }

template <typename A, typename B> string to_string(const pair<A, B> &p) {
  return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}

template <typename A, typename B, typename C>
string to_string(const tuple<A, B, C> &p) {
  return "(" + to_string(std::get<0>(p)) + ", " + to_string(std::get<1>(p)) +
         ", " + to_string(std::get<2>(p)) + ")";
}

template <typename A> string to_string(const A &v) {
  bool first = true;
  string res = "{";
  for (const auto &x : v) {
    if (!first) {
      res += ", ";
    }
    first = false;
    res += to_string(x);
  }
  res += "}";
  return res;
}

template <typename T> void sort(vector<T> &val) {
  sort(val.begin(), val.end());
}

template <typename T> void reverse(vector<T> &val) {
  reverse(val.begin(), val.end());
}

template <typename T> void sort_b(vector<T> &val) {
  sort(val.begin(), val.end(), greater<T>());
}

template <typename T> T pair_sum(pair<T, T> &val) {
  return val.first + val.second;
}

template <typename T> T min(T a, T b, T c) { return min(min(a, b), c); }

template <typename T> T max(T a, T b, T c) { return max(max(a, b), c); }

template <typename T> T mul_mod(T a, T b, T mod) {
  T res = 0;
  a %= mod;
  while (b) {
    if (b & 1) {
      res = (res + a) % mod;
    }
    a = (2 * a) % mod;
    b >>= 1;
  }
  return res;
}

void debug_out() { cerr << endl; }

template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) {
  cerr << " " << to_string(H);
  debug_out(T...);
}

#ifdef LOCAL
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) ;
#endif

#define COMMON_MOD pow(10, 9) + 7
#define PI 3.14159265

// sort(a.begin(), a.end(), less<ll>()); greater<ll>()
// cout<<fixed<<setprecision(20)<<(s/kl)*2;
/* printf("%lld %lld \\n", cur_n, m / 2 + 1);
sort(a.begin(), a.end(), [ ](ll& lhs, ll& rhs) {return abs(lhs) < abs(rhs);});
*/

struct vertex {
  vll childs;
  bool seen;
};

const vertex ZERO_VERTEX = {};

class tree {
public:
  vector<vertex> verteces;

  tree(ll i) { verteces.assign(i, ZERO_VERTEX); }
};

struct node {
  ll first;
  ll second;
  ll color;
  bool operator<(const node &a) const { return first < a.first; }

  bool operator>(const node &a) const { return first > a.first; }
};

string to_string(node a) {
  return "{" + to_string(a.first) + ", " + to_string(a.second) + ", " +
         to_string(a.color) + "}";
}

class mytask {
public:
  mytask() {}

  int solve() {
    ll n, k;
    cin >> n >> k;
    // set<ll> steps;
    vector<int2> intervals;
    for (ll i = 0; i < k; i++) {
      ll l, r;
      cin >> l >> r;
      intervals.pb({l, r});
    }

    vll intervals_sums(k);
    vll a(n + 1);
    a[1] = 1;
    for (ll i = 2; i <= n; i++) {

      for (ll j = 0; j < k; j++) {
        auto [l, r] = intervals[j];
        if (i >= l)
          intervals_sums[j] += a[i - l];
        if (i > r)
          intervals_sums[j] -= a[i - r - 1];

        a[i] += intervals_sums[j];
        a[i] = a[i] % 998244353;
      }
    }
    debug(a);
    cout << a[n];
    return 0;
  }
};

int main() {

  ios::sync_with_stdio(false);
  cin.tie(0);

  auto task = mytask();
  task.solve();

  // cout << endl;

  return 0;
}

Error: TLE

Fixed submission:

/*
  Task: I. Kevin and Grid
  Link: http://codeforces.com/contest/1392/problem/I
  Lang: cpp17
*/
#include <bits/stdc++.h>
using namespace std;

#define eb emplace_back
#define pb push_back
#define INF LLONG_MAX
#define FLT_INF pow(10.0, 16)

using ll = long long;
using vll = vector<ll>;
using ull = unsigned long long;
using int2 = pair<ll, ll>;
using int3 = tuple<ll, ll, ll>;

template <typename A> string to_string(const A &v);
string to_string(const string &s) { return '"' + s + '"'; }
string to_string(const char *s) { return to_string((string)s); }
string to_string(bool b) { return (b ? "true" : "false"); }

template <typename A, typename B> string to_string(const pair<A, B> &p) {
  return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}

template <typename A, typename B, typename C>
string to_string(const tuple<A, B, C> &p) {
  return "(" + to_string(std::get<0>(p)) + ", " + to_string(std::get<1>(p)) +
         ", " + to_string(std::get<2>(p)) + ")";
}

template <typename A> string to_string(const A &v) {
  bool first = true;
  string res = "{";
  for (const auto &x : v) {
    if (!first) {
      res += ", ";
    }
    first = false;
    res += to_string(x);
  }
  res += "}";
  return res;
}

template <typename T> void sort(vector<T> &val) {
  sort(val.begin(), val.end());
}

template <typename T> void reverse(vector<T> &val) {
  reverse(val.begin(), val.end());
}

template <typename T> void sort_b(vector<T> &val) {
  sort(val.begin(), val.end(), greater<T>());
}

template <typename T> T pair_sum(pair<T, T> &val) {
  return val.first + val.second;
}

template <typename T> T min(T a, T b, T c) { return min(min(a, b), c); }

template <typename T> T max(T a, T b, T c) { return max(max(a, b), c); }

template <typename T> T mul_mod(T a, T b, T mod) {
  T res = 0;
  a %= mod;
  while (b) {
    if (b & 1) {
      res = (res + a) % mod;
    }
    a = (2 * a) % mod;
    b >>= 1;
  }
  return res;
}

void debug_out() { cerr << endl; }

template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) {
  cerr << " " << to_string(H);
  debug_out(T...);
}

#ifdef LOCAL
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) ;
#endif

#define COMMON_MOD pow(10, 9) + 7
#define PI 3.14159265

// sort(a.begin(), a.end(), less<ll>()); greater<ll>()
// cout<<fixed<<setprecision(20)<<(s/kl)*2;
/* printf("%lld %lld \\n", cur_n, m / 2 + 1);
sort(a.begin(), a.end(), [ ](ll& lhs, ll& rhs) {return abs(lhs) < abs(rhs);});
*/

struct vertex {
  vll childs;
  bool seen;
};

const vertex ZERO_VERTEX = {};

class tree {
public:
  vector<vertex> verteces;

  tree(ll i) { verteces.assign(i, ZERO_VERTEX); }
};

struct node {
  ll first;
  ll second;
  ll color;
  bool operator<(const node &a) const { return first < a.first; }

  bool operator>(const node &a) const { return first > a.first; }
};

string to_string(node a) {
  return "{" + to_string(a.first) + ", " + to_string(a.second) + ", " +
         to_string(a.color) + "}";
}

class mytask {
public:
  mytask() {}

  int solve() {
    ll n, k;
    cin >> n >> k;
    set<ll> steps;
    for (ll i = 0; i < k; i++) {
      ll l, r;
      cin >> l >> r;
      for (ll j = l; j <= r; j++) {
        steps.insert(j);
      }
    }
    debug(steps);
    vll a(n + 1);
    a[1] = 1;
    for (ll i = 2; i <= n; i++) {
      debug(i);
      for (ll v : steps) {
        if (v < i) {
          a[i] += a[i - v];
          a[i] = a[i] % 998244353;
        }
      }
    }
    debug(a);
    cout << a[n];
    return 0;
  }
};

int main() {

  ios::sync_with_stdio(false);
  cin.tie(0);

  auto task = mytask();
  task.solve();

  // cout << endl;

  return 0;
}