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

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <tuple>
#include <vector>

using namespace std;
#define int long long
#define rep(i, n) for (int i = 0; i < n; ++i)
#define rep1(i, n) for (int i = 1; i <= n; ++i)
#define ALL(x) x.begin(), x.end()
#define debug(output) cout << #output << "= " << output << endl

using P = pair<int, int>;
using lint = long long;
using ll = long long;
const lint inf = 1e18 + 7;
const int MOD = 998244353;
// 区間加算を行うセグ木
// indexは0~n-1まで
class Seg_Tree {
private:
  int n;
  vector<int> node;

public:
  int process(int left, int right) {
    if (left == inf)
      return right;
    if (right == inf)
      return left;
    int ans = left;
    if (left > right)
      ans = right;
    return ans;
  }
  Seg_Tree(vector<int> v) {
    int num = 1;
    int size = v.size();
    while (num < size) {
      num *= 2;
    }
    n = num;
    node.resize(2 * num - 1, 0);
    for (int i = 0; i < size; i++)
      node[i + num - 1] = v[i];
    for (int i = n - 2; i >= 0; i--)
      node[i] = process(node[i * 2 + 1], node[i * 2 + 2]);
  }
  int get(int x) {
    x += (n - 1);
    int res = node[x];
    while (x > 0) {
      x = (x - 1) / 2;
      res += node[x];
      res %= MOD;
    }
    res += node[0];
    res %= MOD;
    return res;
  }
  void add(int a, int b, int value, int k = 0, int l = 0, int r = -1) {
    if (r < 0)
      r = n;
    if (a >= r || b <= l)
      goto FINE;
    if (a <= l && b >= r) {
      node[k] += value;
      node[k] %= MOD;
      goto FINE;
    }
    add(a, b, value, k * 2 + 1, l, (l + r) / 2);
    add(a, b, value, k * 2 + 2, (l + r) / 2, r);
  // return process(vl,vr);
  FINE:;
  }
};
signed main() {
  int n, k;
  cin >> n >> k;
  vector<int> l(k), r(k);
  rep(i, k) cin >> l[i] >> r[i];
  Seg_Tree dp(vector<int>(n * 2 + 10, 0));
  dp.add(1, 2, 1);
  for (int j = 1; j <= n; ++j) {
    rep(i, k) { dp.add(j + l[i], j + r[i] + 1, dp.get(j)); }
  }
  cout << dp.get(n) << "\\n";
  rep1(i, n) {
    lint hoge = dp.get(i);
    // debug(hoge);
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <tuple>
#include <vector>

using namespace std;
// #define int long long
#define rep(i, n) for (int i = 0; i < n; ++i)
#define rep1(i, n) for (int i = 1; i <= n; ++i)
#define ALL(x) x.begin(), x.end()
#define debug(output) cout << #output << "= " << output << endl

using P = pair<int, int>;
using lint = long long;
using ll = long long;
const lint inf = 1e18 + 7;
const int MOD = 998244353;
signed main() {
  int n, k;
  cin >> n >> k;
  vector<int> a;
  rep(i, k) {
    int l, r;
    cin >> l >> r;
    for (int j = l; j <= r; ++j)
      a.push_back(j);
  }
  vector<int> dp(n + 1, 0);
  dp[1] = 1;
  int num = a.size();
  sort(a.begin(), a.end());
  for (int j = 1; j < n; ++j) {
    rep(i, num) {
      if (a[i] + j > n)
        break;
      dp[a[i] + j] += dp[j];
      dp[a[i] + j] %= MOD;
    }
  }
  cout << dp[n] << "\\n";
  return 0;
}