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

#include <algorithm>
#include <array>
#include <bitset>
#include <cctype>
#include <cfloat>
#include <cinttypes>
#include <climits>
#include <cmath>
#include <complex>
#include <cstddef>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <valarray>
#include <vector>

using namespace std;

const double EPS = 1e-10;
const int64_t INF = 1e9;
// typedef long long ll;
typedef int ll;

const int QMAX = 530000;
const int MOD = 998244353;

int n;
ll dat[QMAX];

void init(int _n) {
  n = 1;
  while (n < _n)
    n *= 2;

  for (int i = 0; i < 2 * n - 1; i++)
    dat[i] = 0;
}

ll val(int k) {
  k += n - 1;
  ll ans = dat[k];
  while (k > 0) {
    k = (k - 1) / 2;
    ans = (ans + dat[k]) % MOD;
  }
  return ans;
}

void add(int a, int b, ll x, int k, int l, int r) {
  if (r <= a || b <= l)
    return;

  if (a <= l && r <= b) {
    dat[k] = (dat[k] + x) % MOD;
  } else {
    add(a, b, x, k * 2 + 1, l, (l + r) / 2);
    add(a, b, x, k * 2 + 2, (l + r) / 2, r);
  }
}

int main() {
  int m, k;
  int l[10], r[10];
  scanf("%d%d", &m, &k);
  for (int i = 0; i < k; ++i) {
    scanf("%d%d", &l[i], &r[i]);
  }

  init(m + 2);

  add(1, 2, 1, 0, 0, n);
  for (int i = 1; i < m; ++i) {
    ll a = val(i);
    for (int j = 0; j < k; ++j) {
      if (i + l[j] > n)
        continue;
      int ed = min(i + r[j], n);
      add(i + l[j], ed + 1, a, 0, 0, n);
    }

    /*
    for (int j = 0; j <= m; ++j) {
            printf("%d: %d\\n", j, val(j));
    }
    printf("\\n");
    */
  }

  printf("%lld\\n", val(m));

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <array>
#include <bitset>
#include <cctype>
#include <cfloat>
#include <cinttypes>
#include <climits>
#include <cmath>
#include <complex>
#include <cstddef>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <valarray>
#include <vector>

using namespace std;

const double EPS = 1e-10;
const int64_t INF = 1e9;
// typedef long long ll;
typedef int ll;

const int QMAX = 530000;
const int MOD = 998244353;

int n;
ll dat[QMAX];

void init(int _n) {
  n = 1;
  while (n < _n)
    n *= 2;

  for (int i = 0; i < 2 * n - 1; i++)
    dat[i] = 0;
}

ll val(int k) {
  k += n - 1;
  ll ans = dat[k];
  while (k > 0) {
    k = (k - 1) / 2;
    ans = (ans + dat[k]) % MOD;
  }
}

void add(int a, int b, ll x, int k, int l, int r) {
  if (r <= a || b <= l)
    return;

  if (a <= l && r <= b) {
    dat[k] = (dat[k] + x) % MOD;
  } else {
    add(a, b, x, k * 2 + 1, l, (l + r) / 2);
    add(a, b, x, k * 2 + 2, (l + r) / 2, r);
  }
}

int main() {
  int m, k;
  int l[10], r[10];
  scanf("%d%d", &m, &k);
  for (int i = 0; i < k; ++i) {
    scanf("%d%d", &l[i], &r[i]);
  }

  init(m + 2);

  add(1, 2, 1, 0, 0, n);
  for (int i = 1; i < m; ++i) {
    ll a = val(i);
    for (int j = 0; j < k; ++j) {
      if (i + l[j] > n)
        continue;
      int ed = min(i + r[j], n);
      add(i + l[j], ed + 1, a, 0, 0, n);
    }

    /*
    for (int j = 0; j <= m; ++j) {
            printf("%d: %d\\n", j, val(j));
    }
    printf("\\n");
    */
  }

  printf("%lld\\n", val(m));

  return 0;
}