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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define int long long
#define double long double
#define rep(i, n) for (int i = 0; i < n; i++)
#define srep(i, n, m) for (int i = n; i < m; i++)
#define elif else if
#define pi 3.141592653589793
#define prique priority_queue
constexpr auto mod = 1000000007;
using namespace std;
int gcd(int a, int b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
int lcm(int a, int b) { return a * b / gcd(a, b); }
bool prime(int n) {
  int cnt = 0;
  for (int i = 1; i <= sqrt(n); i++) {
    if (n % 1 == 0)
      cnt++;
  }
  if (cnt != 1)
    return false;
  else
    return n != 1;
}
struct UnionFind {
  vector<int> r;
  UnionFind(int N) { r = vector<int>(N, -1); }
  int root(int x) {
    if (r[x] < 0)
      return x;
    return r[x] = root(r[x]);
  }
  bool unite(int x, int y) {
    x = root(x);
    y = root(y);
    if (x == y)
      return false;
    if (r[x] > r[y])
      swap(x, y);
    r[x] += r[y];
    r[y] = x;
    return true;
  }
  int size(int x) { return -r[root(x)]; }
};
vector<int> v;
int n, m, cnt = 0, ans = 0, aa[220000], ab[220000];
pair<int, int> pp[220000];
string s, ss[220000];
queue<int> que;
signed main() {
  cin >> n >> m;
  rep(i, m) { cin >> pp[i].first >> pp[i].second; }
  sort(pp, pp + m);
  aa[1] = 1;
  ab[1] = 1;
  for (int i = 2; i < n + 1; i++) {
    rep(j, m) {
      if (pp[j].first > i - 1) {
        break;
      }
      if (ab[i - pp[j].first] < ab[i - pp[j].second - 1])
        aa[i] += ab[i - pp[j].first] - ab[i - pp[j].second - 1] + 998244353;
      else
        aa[i] += ab[i - pp[j].first] - ab[i - pp[j].second - 1];
      aa[i] %= 998244353;
    }
    ab[i] = aa[i] + ab[i - 1];
  }
  cout << aa[n];
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define int long long
#define double long double
#define rep(i, n) for (int i = 0; i < n; i++)
#define srep(i, n, m) for (int i = n; i < m; i++)
#define elif else if
#define pi 3.141592653589793
#define prique priority_queue
constexpr auto mod = 1000000007;
using namespace std;
int gcd(int a, int b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
int lcm(int a, int b) { return a * b / gcd(a, b); }
bool prime(int n) {
  int cnt = 0;
  for (int i = 1; i <= sqrt(n); i++) {
    if (n % 1 == 0)
      cnt++;
  }
  if (cnt != 1)
    return false;
  else
    return n != 1;
}
struct UnionFind {
  vector<int> r;
  UnionFind(int N) { r = vector<int>(N, -1); }
  int root(int x) {
    if (r[x] < 0)
      return x;
    return r[x] = root(r[x]);
  }
  bool unite(int x, int y) {
    x = root(x);
    y = root(y);
    if (x == y)
      return false;
    if (r[x] > r[y])
      swap(x, y);
    r[x] += r[y];
    r[y] = x;
    return true;
  }
  int size(int x) { return -r[root(x)]; }
};
vector<int> v;
int n, m, cnt = 0, ans = 0, aa[220000];
pair<int, int> pp[220000];
string s, ss[220000];
queue<int> que;
signed main() {
  cin >> n >> m;
  rep(i, m) { cin >> pp[i].first >> pp[i].second; }
  sort(pp, pp + m);
  aa[1] = 1;
  rep(i, n) {
    if (aa[i + 1] == 0) {
      continue;
    }
    rep(j, m) {
      for (int k = pp[j].first; k < min(pp[j].second + 1, n - i); k++) {
        aa[i + 1 + k] += aa[i + 1];
        aa[i + 1 + k] %= 998244353;
      }
    }
  }
  cout << aa[n];
}