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


#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
#include <ctime>
#include <deque>
using namespace std;

#define fr first
#define se second
#define int long long
#define pb push_back
#define mp make_pair
#define pii pair<int, int>
#define vi vector<int>
#define vvi vector<vector<int>>
#define mi map<int, int>
#define pq priority_queue<int>
#define set_bits(x) __builtin_popcountll(x)
#define mod 1000000007
#define inf 1e18 // Defining infinity the overflow of int
#define ps(x, y)                                                               \\
  fixed << setprecision(y)                                                     \\
        << x // for defining the precision of the decimal upto y places.
#define zrosetbits(x)                                                          \\
  __builtin_ctzll(x) // it will give no of 0's before the first 1 from the
                     // right.
#define mk(a, n, type) type *a = new type[n];
#define test(t)                                                                \\
  int t;                                                                       \\
  cin >> t;                                                                    \\
  while (t--)
#define no cout << "No\\n";
#define yes cout << "Yes\\n";
#define NO cout << "NO\\n";
#define YES cout << "YES\\n";
// #define for(i,n)          for(int i=0;i<n;i++)
// #define for(j,n)          for(int j=0;j<n;j++)
//  #define forn(i,n)               for(int i=0;i<n;i--)

//**********************************************************************************************************
struct custom_hash {
  static uint64_t splitmix64(uint64_t x) {
    // http://xorshift.di.unimi.it/splitmix64.c
    x += 0x9e3779b97f4a7c15;
    x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
    x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
    return x ^ (x >> 31);
  }

  size_t operator()(uint64_t x) const {
    static const uint64_t FIXED_RANDOM =
        chrono::steady_clock::now().time_since_epoch().count();
    return splitmix64(x + FIXED_RANDOM);
  }
};

#define safemap unordered_map<int, int, custom_hash>

//*********************************************************************************************************

void io() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin.tie(0);
}

int32_t main() {
  int n, k, m, p, q, x, y;
  int small = INT_MIN;

  cin >> n >> k;

  int l[k] = {0};
  int r[k] = {0};

  for (int i = 0; i < k; i++)
    cin >> l[i] >> r[i];

  // Now creating DP
  //  Now creating prefix array for storing the sum of all possible combinations
  //  to reach the cell ith

  vi dp(n + 1, 0);
  vi psum(n + 1, 0);

  dp[1] = 1;
  psum[1] = 1;

  int mood = 998244353;

  for (int i = 2; i <= n; i++) {
    for (int j = 0; j < k; j++) {
      if (i <= l[j])
        continue;

      int start = l[j];
      int end = min(i - 1, r[j]);

      int val = (psum[i - start] - psum[i - end - 1] + mood) % mood;
      dp[i] = (val + dp[i]) % mood;
    }
    psum[i] = (dp[i] % mood + psum[i - 1] % mood) % mood;
  }

  cout << dp[n] << "\\n";

  return 0;
}


Error: TLE

Fixed submission:


#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
#include <ctime>
#include <deque>
using namespace std;

#define fr first
#define se second
#define int long long
#define pb push_back
#define mp make_pair
#define pii pair<int, int>
#define vi vector<int>
#define vvi vector<vector<int>>
#define mi map<int, int>
#define pq priority_queue<int>
#define set_bits(x) __builtin_popcountll(x)
#define mod 1000000007
#define inf 1e18 // Defining infinity the overflow of int
#define ps(x, y)                                                               \\
  fixed << setprecision(y)                                                     \\
        << x // for defining the precision of the decimal upto y places.
#define zrosetbits(x)                                                          \\
  __builtin_ctzll(x) // it will give no of 0's before the first 1 from the
                     // right.
#define mk(a, n, type) type *a = new type[n];
#define test(t)                                                                \\
  int t;                                                                       \\
  cin >> t;                                                                    \\
  while (t--)
#define no cout << "No\\n";
#define yes cout << "Yes\\n";
#define NO cout << "NO\\n";
#define YES cout << "YES\\n";
// #define for(i,n)          for(int i=0;i<n;i++)
// #define for(j,n)          for(int j=0;j<n;j++)
//  #define forn(i,n)               for(int i=0;i<n;i--)

//**********************************************************************************************************
struct custom_hash {
  static uint64_t splitmix64(uint64_t x) {
    // http://xorshift.di.unimi.it/splitmix64.c
    x += 0x9e3779b97f4a7c15;
    x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
    x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
    return x ^ (x >> 31);
  }

  size_t operator()(uint64_t x) const {
    static const uint64_t FIXED_RANDOM =
        chrono::steady_clock::now().time_since_epoch().count();
    return splitmix64(x + FIXED_RANDOM);
  }
};

#define safemap unordered_map<int, int, custom_hash>

//*********************************************************************************************************

void io() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin.tie(0);
}

int helper(int cell, vector<int> &v, vi &dp, int n) {
  // Base Case
  if (cell == n)
    return 1;

  if (dp[cell] != -1)
    return dp[cell];

  // recursive Case
  int res = 0;

  for (int i = 0; i < v.size(); i++) {
    if (cell + v[i] <= n)
      res = (res % 998244353 + helper(cell + v[i], v, dp, n) % 998244353) %
            998244353;

    else
      break;
  }

  dp[cell] = res;

  return res;
}

int32_t main() {

  io();
  clock_t begin = clock();

  int te = 1;

  int n, k, m, p, q, x, y;
  int small = INT_MIN;

  cin >> n >> k;

  map<int, int> mp;

  for (int i = 0; i < k; i++) {
    cin >> p >> q;

    for (int j = p; j <= q; j++)
      mp[j]++;
  }

  vi v;

  for (auto x : mp)
    v.pb(x.first);

  sort(v.begin(), v.end());

  vi dp(n + 1, -1);

  cout << helper(1, v, dp, n) % 998244353 << "\\n";

  return 0;
}