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

#include <bits/stdc++.h>
using namespace std;
typedef unsigned int INT;
typedef unsigned long long LONG;
typedef long long LL;
typedef pair<int, int> PII;
const int N = 1e5 + 10;
const int mod = 998244353;
#define debug(x) cerr << #x << ": " << (x) << '\\n'
#define FW freopen("data2.txt", "w", stdout);
// 快速读入输出，只适用于整数，使用时流同步打开，C语言不准关流
//  getchar()<<cin(关流)<<scanf()<<cin
template <class T> inline bool Read(T &ret) {
  char c;
  int sgn;
  if (static_cast<void>(c = getchar()), c == EOF)
    return 0;
  while (c != '-' && (c < '0' || c > '9'))
    c = getchar();
  sgn = (c == '-') ? -1 : 1;
  ret = (c == '-') ? 0 : (c - '0');
  while (static_cast<void>(c = getchar()), c >= '0' && c <= '9')
    ret = ret * 10 + (c - '0');
  ret *= sgn;
  return 1;
}
// 输出只能用于整数
template <class T> inline void Write(T x) {
  if (x < 0)
    putchar('-'), x = -x;
  if (x > 9)
    Write(x / 10);
  putchar(x % 10 + '0');
}

LL dp[N * 2] = {0};
int lowbit(int x) { return (x & -x); }
void add(int x, int val) {
  for (int i = x; i < 2 * N; i += lowbit(i)) {
    dp[i] = (dp[i] % mod + val) % mod;
  }
}
LL sum(int x) {
  LL ans = 0;
  while (x) {
    ans = (ans % mod + dp[x]) % mod;
    x -= lowbit(x);
  }
  return ans % mod;
}

LL rang_sum(int l, int r) { return (sum(r) - sum(l - 1)) + mod; }
void slove(int question_num) {
  int n, k;
  cin >> n >> k;
  unordered_map<int, int> num;
  for (int i = 1; i <= k; ++i) {
    int a, b;
    cin >> a >> b;
    num[a] = b;
  }
  add(1, 1);
  for (int i = 2; i <= n; ++i) {
    for (auto j : num) {
      int a = j.first, b = j.second;
      if (a >= i)
        continue;
      if (b >= i) {
        LL cur = rang_sum(1, i - a);
        add(i, cur);
      } else {
        LL cur = rang_sum(i - b, i - a);
        add(i, cur);
      }
    }
  }
  cout << (rang_sum(n, n) % mod) << endl;
}

int main() {
  int num = 1;
  // cin >> num;
  for (int i = 1; i <= num; ++i) {
    slove(i);
  }
  // system("pause");
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef unsigned int INT;
typedef unsigned long long LONG;
typedef long long LL;
typedef pair<int, int> PII;
const int N = 1e5 + 10;
const int mod = 998244353;
#define debug(x) cerr << #x << ": " << (x) << '\\n'
#define FW freopen("data2.txt", "w", stdout);
// 快速读入输出，只适用于整数，使用时流同步打开，C语言不准关流
//  getchar()<<cin(关流)<<scanf()<<cin
template <class T> inline bool Read(T &ret) {
  char c;
  int sgn;
  if (static_cast<void>(c = getchar()), c == EOF)
    return 0;
  while (c != '-' && (c < '0' || c > '9'))
    c = getchar();
  sgn = (c == '-') ? -1 : 1;
  ret = (c == '-') ? 0 : (c - '0');
  while (static_cast<void>(c = getchar()), c >= '0' && c <= '9')
    ret = ret * 10 + (c - '0');
  ret *= sgn;
  return 1;
}
// 输出只能用于整数
template <class T> inline void Write(T x) {
  if (x < 0)
    putchar('-'), x = -x;
  if (x > 9)
    Write(x / 10);
  putchar(x % 10 + '0');
}

int dp[N * 2] = {0};
void slove(int question_num) {
  int n, k;
  cin >> n >> k;
  unordered_map<int, int> num;
  for (int i = 1; i <= k; ++i) {
    int a, b;
    cin >> a >> b;
    num[a] = b;
  }
  dp[1] = 1;
  for (int i = 2; i <= n; ++i) {
    for (auto j : num) {
      for (int k = j.first; k <= j.second; ++k) {
        int cur = i - k;
        if (cur <= 0)
          break;
        dp[i] = (dp[i] + dp[cur]) % mod;
      }
    }
  }
  cout << (dp[n] % mod) << endl;
}

int main() {
  int num = 1;
  // cin >> num;
  for (int i = 1; i <= num; ++i) {
    slove(i);
  }
  // system("pause");
  return 0;
}