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

#include <bits/stdc++.h>
// #include <boost/multiprecision/cpp_int.hpp>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
#pragma GCC optimize("O3")
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPP(i, n) for (int i = 1; i <= n; i++)
#define ALL(obj) (obj).begin(), (obj).end()
#define EPS (1e-9)
#define INF (1e17)
#define PI (acos(-1))
// const double PI = acos(-1);
// const double EPS = 1e-15;
// long long INF=(long long)1E17;
// #define i_7 (long long)(1e9+7)
#define i_7 998'244'353
long mod(long a) {
  long long c = a % i_7;
  if (c >= 0)
    return c;
  return c + i_7;
}
long long po(long a, long b) {
  if (b == 0) {
    return 1;
  }

  long long z = po(a, b / 2);
  z = mod(z * z);
  if (b % 2 != 0) {
    z = mod(a * z);
  }
  return z;
}

bool prime_(int n) {
  if (n == 1) {
    return false;
  } else if (n == 2) {
    return true;
  } else {
    for (int i = 2; i <= std::sqrt(n); i++) {
      if (n % i == 0) {
        return false;
      }
    }
    return true;
  }
}

long long gcd_(long long a, long long b) {
  if (a < b) {
    std::swap(a, b);
  }
  if (a % b == 0) {
    return b;
  } else {
    return gcd_(b, a % b);
  }
}

long long lcm_(long long x, long long y) { return (x / gcd_(x, y)) * y; }

using namespace std;
// using namespace boost::multiprecision;
// using namespace __gnu_pbds;

template <typename T> class SegmentTree {
  using F = function<T(T, T)>;
  int n;
  F f;
  T t;
  vector<T> dat;

public:
  SegmentTree(F f, T t) : f(f), t(t) {}
  void init(int n_) {
    n = 1;
    while (n < n_)
      n <<= 1;
    dat.assign(n << 1, t);
  }
  void build(const vector<T> &v) {
    auto n_ = (int)v.size();
    init(n_);
    for (int i = 0; i < n_; i++)
      dat[n + i] = v[i];
    for (int i = n - 1; i > 0; i--)
      dat[i] = f(dat[(i << 1) | 0], dat[(i << 1) | 1]);
  }
  // k番目の値をxに変更する。
  void set_val(int k, T x) {
    dat[k += n] = x;
    while (k >>= 1)
      dat[k] = f(dat[(k << 1) | 0], dat[(k << 1) | 1]);
  }
  //[a,b)に対してfを適用する。
  T query(int a, int b) {
    T vl = t, vr = t;
    for (int l = a + n, r = b + n; l < r; l >>= 1, r >>= 1) {
      if (l & 1)
        vl = f(vl, dat[l++]);
      if (r & 1)
        vr = f(dat[--r], vr);
    }
    return f(vl, vr);
  }
};

int n, k;
int l[10], r[10];
long long dp[200'010];

int main() {
  using namespace std;
  cin >> n >> k;
  REP(i, k) cin >> l[i] >> r[i];
  typedef pair<int, int> P;
  vector<P> vec;
  REP(i, k) vec.push_back(P(l[i], r[i]));
  sort(ALL(vec));
  REP(i, k) {
    l[i] = vec[i].first;
    r[i] = vec[i].second;
  }
  REP(i, n) dp[i] = -1;

  auto f = [](long long a, long long b) { return mod(a + b); };
  SegmentTree<long long> seg(f, 0LL);
  seg.build(vector<long long>(n, 0LL));

  seg.set_val(0, 1);
  for (int pos = 1; pos < n; pos++) {
    long long res = 0;
    for (int i = 0; i < k; i++) {
      if (l[i] > pos)
        break;
      int st = pos - min(pos, r[i]);
      int ed = pos - l[i];
      res += seg.query(st, ed + 1);
      res = mod(res);
    }
    seg.set_val(pos, res);
  }
  long long ans = seg.query(n - 1, n);
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
// #include <boost/multiprecision/cpp_int.hpp>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
#pragma GCC optimize("O3")
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPP(i, n) for (int i = 1; i <= n; i++)
#define ALL(obj) (obj).begin(), (obj).end()
#define EPS (1e-9)
#define INF (1e17)
#define PI (acos(-1))
// const double PI = acos(-1);
// const double EPS = 1e-15;
// long long INF=(long long)1E17;
// #define i_7 (long long)(1e9+7)
#define i_7 998'244'353
long mod(long a) {
  long long c = a % i_7;
  if (c >= 0)
    return c;
  return c + i_7;
}
long long po(long a, long b) {
  if (b == 0) {
    return 1;
  }

  long long z = po(a, b / 2);
  z = mod(z * z);
  if (b % 2 != 0) {
    z = mod(a * z);
  }
  return z;
}

bool prime_(int n) {
  if (n == 1) {
    return false;
  } else if (n == 2) {
    return true;
  } else {
    for (int i = 2; i <= std::sqrt(n); i++) {
      if (n % i == 0) {
        return false;
      }
    }
    return true;
  }
}

long long gcd_(long long a, long long b) {
  if (a < b) {
    std::swap(a, b);
  }
  if (a % b == 0) {
    return b;
  } else {
    return gcd_(b, a % b);
  }
}

long long lcm_(long long x, long long y) { return (x / gcd_(x, y)) * y; }

using namespace std;
// using namespace boost::multiprecision;
// using namespace __gnu_pbds;

int n, k;
int l[10], r[10];
long long dp[200'010];
typedef pair<int, int> P;
map<P, long long> mp;

long long dfs(int pos) {
  if (pos == 0)
    return 1;
  if (dp[pos] >= 0)
    return dp[pos];
  long long res = 0;
  for (int i = 0; i < k; i++) {
    if (l[i] > pos)
      break;
    int st = pos - min(pos, r[i]);
    int ed = pos - l[i];
    P p = P(st, ed);
    if (mp.find(p) != mp.end()) {
      res += mp[p];
      res = mod(res);
      continue;
    }
    long long temp = 0;
    for (int prev = ed; prev >= st; prev--) {
      P q = P(st, prev);
      if (mp.find(q) != mp.end()) {
        temp += mp[q];
        temp = mod(temp);
        break;
      }
      temp += dfs(prev);
      temp = mod(temp);
    }
    mp[p] = temp;
    res += temp;
    res = mod(res);
  }
  return dp[pos] = res;
}

int main() {
  using namespace std;
  cin >> n >> k;
  REP(i, k) cin >> l[i] >> r[i];

  vector<P> vec;
  REP(i, k) vec.push_back(P(l[i], r[i]));
  sort(ALL(vec));
  REP(i, k) {
    l[i] = vec[i].first;
    r[i] = vec[i].second;
  }
  REP(i, n) dp[i] = -1;
  long long ans = dfs(n - 1);
  cout << ans << endl;
  return 0;
}