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

#include <algorithm>
#include <cassert>
#include <cstdio>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string.h>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;

#define SORT(v) sort((v).begin(), (v).end())
#define RSORT(v) sort((v).rbegin(), (v).rend())
#define pb push_back
#define FOR(i, n) for (int i = 0; i < (n); i++)
typedef pair<int, int> pii;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;

const int mod = 1e9 + 7;
const int mod2 = 998244353;

void fact_init(int n);
ll exp(ll taban, ll us, ll md);
ll ebob(ll a, ll b);
ll ekok(ll a, ll b);
ll komb(ll a, ll b);

vector<ll> fact;
vector<ll> inv_fact;
void fact_init(int n) {
  fact.resize(n + 5);
  inv_fact.resize(n + 5);
  fact[0] = inv_fact[0] = 1;
  for (int i = 1; i <= n; i++) {
    fact[i] = (fact[i - 1] * i) % mod;
    inv_fact[i] = exp(fact[i], mod - 2, mod);
  }
}
ll exp(ll taban, ll us, ll md) {
  ll carpan = taban % md;
  if (carpan == 0)
    return 0;
  ll temp = us;
  ll res = 1;
  while (temp) {
    if (temp % 2)
      res = (res * carpan) % md;
    temp /= 2;
    carpan = (carpan * carpan) % md;
  }
  return res;
}

ll ebob(ll a, ll b) {
  if (!a)
    return b;
  return ebob(b % a, a);
}

ll ekok(ll a, ll b) { return (a * b) / ebob(a, b); }

ll komb(ll a, ll b) {
  if (a < b)
    return 0;
  return fact[a] * (inv_fact[a - b] * inv_fact[b] % mod) % mod;
}
const int N = 2e5 + 5;
int seg[4 * N];
void update(int cur, int l, int r, int tar, int val) {
  int mid = (l + r) / 2;
  if (!(l <= tar && tar <= r))
    return;
  if (l == r) {
    seg[cur] = (seg[cur] + val) % mod2;
    return;
  }
  update(2 * cur, l, mid, tar, val);
  update(2 * cur + 1, mid + 1, r, tar, val);
  seg[cur] = (seg[2 * cur] + seg[2 * cur + 1]) % mod2;
}
int get(int cur, int l, int r, int tl, int tr) {
  int mid = (l + r) / 2;
  if (tl > r || l > tr)
    return 0;
  if (tl <= l && r <= tr)
    return seg[cur];
  return (get(2 * cur, l, mid, tl, tr) + get(2 * cur + 1, mid + 1, r, tl, tr)) %
         mod2;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  int n = 5e4, k = 10;
  cin >> n >> k;
  int l[k], r[k];
  for (int i = 0; i < k; i++) {
    l[i] = rand() % n;
    r[i] = min(n, l[i] + rand() % n);
    cin >> l[i] >> r[i];
  }
  update(1, 0, n, 1, 1);
  for (int i = 2; i <= n; i++) {
    int res = 0;
    for (int j = 0; j < k; j++) {
      res = (res + get(1, 0, n, max(0, i - r[j]), max(0, i - l[j]))) % mod2;
    }
    update(1, 0, n, i, res);
  }
  cout << get(1, 0, n, n, n) << "\\n";
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cstdio>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string.h>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;

#define SORT(v) sort((v).begin(), (v).end())
#define RSORT(v) sort((v).rbegin(), (v).rend())
#define pb push_back
#define FOR(i, n) for (int i = 0; i < (n); i++)
typedef pair<int, int> pii;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;

// const int mod = 1e9 + 7;
const int mod = 998244353;

void fact_init(int n);
ll exp(ll taban, ll us, ll md);
ll ebob(ll a, ll b);
ll ekok(ll a, ll b);
ll komb(ll a, ll b);

vector<ll> fact;
vector<ll> inv_fact;
void fact_init(int n) {
  fact.resize(n + 5);
  inv_fact.resize(n + 5);
  fact[0] = inv_fact[0] = 1;
  for (int i = 1; i <= n; i++) {
    fact[i] = (fact[i - 1] * i) % mod;
    inv_fact[i] = exp(fact[i], mod - 2, mod);
  }
}
ll exp(ll taban, ll us, ll md) {
  ll carpan = taban % md;
  if (carpan == 0)
    return 0;
  ll temp = us;
  ll res = 1;
  while (temp) {
    if (temp % 2)
      res = (res * carpan) % md;
    temp /= 2;
    carpan = (carpan * carpan) % md;
  }
  return res;
}

ll ebob(ll a, ll b) {
  if (!a)
    return b;
  return ebob(b % a, a);
}

ll ekok(ll a, ll b) { return (a * b) / ebob(a, b); }

ll komb(ll a, ll b) {
  if (a < b)
    return 0;
  return fact[a] * (inv_fact[a - b] * inv_fact[b] % mod) % mod;
}
const int N = 1e5 + 5;
int seg[4 * N];
void update(int cur, int l, int r, int tar, int val) {
  int mid = (l + r) / 2;
  if (l > tar)
    return;
  if (l == r) {
    if (l == tar)
      seg[cur] = (seg[cur] + val) % mod;
    return;
  }
  update(2 * cur, l, mid, tar, val);
  update(2 * cur + 1, mid + 1, r, tar, val);
  seg[cur] = (seg[2 * cur] + seg[2 * cur + 1]) % mod;
}
int get(int cur, int l, int r, int tl, int tr) {
  int mid = (l + r) / 2;
  if (tl > r || l > tr)
    return 0;
  if (tl <= l && r <= tr)
    return seg[cur];
  return (get(2 * cur, l, mid, tl, tr) + get(2 * cur + 1, mid + 1, r, tl, tr)) %
         mod;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  int n, k;
  cin >> n >> k;
  int l[k], r[k];
  for (int i = 0; i < k; i++)
    cin >> l[i] >> r[i];
  update(1, 0, n, 1, 1);
  for (int i = 2; i <= n; i++) {
    int res = 0;
    for (int j = 0; j < k; j++) {
      res = (res + get(1, 0, n, max(0, i - r[j]), max(0, i - l[j]))) % mod;
      // cout<<i<<" "<<get(1, 0, n, max(0, i - r[j]), max(0, i - l[j]))<<"\\n";
    }
    update(1, 0, n, i, res);
    // cout<<get(1, 0, n, 0, 0)<<"\\n";
  }
  cout << get(1, 0, n, n, n) << "\\n";
}