Language: C++

#include <bits/stdc++.h>
using namespace std;

#define F first
#define FF first.first
#define FS first.second
#define S second
#define SF second.first
#define SS second.second
#define ii pair<int,int>
#define iii pair<ii, int>
#define iiii pair<ii, ii>
#define viiii vector< iiii >
#define viii vector< iii >
#define vii vector< ii >
#define vi vector< int >
#define vvi vector<vector< int > >
#define int int64_t


#define sz(x) ((int)x.size())
#define all(x) x.begin(),x.end()
#define sortv(V) sort(all(V))
#define reversev(V) reverse(all(V))
#define uniquev(V) sortv(V),(V).erase(unique(all(V)),(V).end())

#define gcd(a, b) (__gcd((a), (b)))
#define lcm(a, b) (((a)/__gcd(a, b)) * b)

#define yes cout << "Yes" << endl
#define no cout << "No" << endl

// Trace dinâmico (parameter pack (c++11))
template<typename T>
void println(T a) { cout << a << "\n"; }
template<typename T, typename... Args>
void println(T a, Args... args) { cout << a << " "; println(args...); }

template<typename T>
void print(T a) { cout << a; }
template<typename T, typename... Args>
void print(T a, Args... args) { cout << a << " "; print(args...); }

template<typename T>
void printv(T a, string last="\n") {
    for(auto el: a)
      print(el, "");
    print(last);
}


template<typename T>
void scan(T& a) { cin >> a ; }
template<typename T, typename... Args>
void scan(T& a, Args&&... args) { cin >> a; scan(args...); }

//Functions

int firstGreater(int value, vector<int> &v){
   int l = 0, r = v.size();
   while(l<r){
      int m = (l+r)/2;
      if( v[m]>value ){
         r = m;
      }else{
         l = m+1;
      }
   }

   return l;
}

int firstGreaterOrIgual(int value, vector<int> &v){
   int l = 0, r = v.size();
   while(l<r){
      int m = (l+r)/2;
      if( v[m]>=value ){
         r = m;
      }else{
         l = m+1;
      }
   }
   return l;
}


#define pb push_back
#define FOR(v, i, l) for(int v = i; v < (int)l; ++v)
#define REP(v, l) FOR(v, 0, l)

#define EPS 1e-6
#define oo (1LL<<60)
#define MAX 1000000000000000000LL
#define MOD 998244353LL
#define endl '\n'
#define debug(x) cout << #x << " is " << x << "\n";

const int dx[8] = {1, 1, -1, -1, 2, 2, -2, -2};
const int dy[8] = {2, -2, 2, -2, 1, -1, 1, -2};

//Main
int N, M, K, L, R, J, P, CASES;
int A,B,C;

struct PrefixSumBit{
   int len;
   vector<int> bit;

   /*1 to len*/
   PrefixSumBit(int _len){
      len = _len;
      bit = vector<int>(len+1);
   }
   /*LeastSignificantBit*/
   static int LSB(int value){
      return value & -value;
   }

   void add(int value, int id){
      while( id <= len ){
         bit[id]+=value;
         bit[id]%=MOD;
         id += LSB(id);
      }
   }

   int preSum(int id){
      int ans = 0;
      while(id){
         ans += bit[id];
         ans %= MOD;
         id -= LSB(id);
      }
      return ans;
   }
};

main(){
   std::ios::sync_with_stdio(false);
   cin.tie(0);
   cout << setprecision(9) << fixed;

   cin >> N >> K;

   vector<ii> S(K);
   for(auto &it: S) cin >> it.F >> it.S;

   PrefixSumBit presum(N);

   presum.add(1, 1);
   for(int i = 2; i <= N; i++){
      for(int k = 0; k < K; k++){
         int l = max(1LL, i-S[k].S);
         int r = min(i-1LL, i-S[k].F);
         if(l<=r){
            int ans = (presum.preSum(r) - presum.preSum(l-1) + MOD)%MOD;
            presum.add(ans, i);
            //debug(i);
            //debug(ans);
         }
      }
   }
   println( presum.preSum(N) - presum.preSum(N-1) );
/*
5 1
1 2
*/

}
























Compiler output:
a.cc:138:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
  138 | main(){
      | ^~~~
a.cc: In function 'int main()':
a.cc:153:21: error: no matching function for call to 'max(long long int, int64_t)'
  153 |          int l = max(1LL, i-S[k].S);
      |                  ~~~^~~~~~~~~~~~~~~
In file included from /usr/include/c++/14/algorithm:60,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:51,
                 from a.cc:1:
/usr/include/c++/14/bits/stl_algobase.h:257:5: note: candidate: 'template<class _Tp> constexpr const _Tp& std::max(const _Tp&, const _Tp&)'
  257 |     max(const _Tp& __a, const _Tp& __b)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:257:5: note:   template argument deduction/substitution failed:
a.cc:153:21: note:   deduced conflicting types for parameter 'const _Tp' ('long long int' and 'int64_t' {aka 'long int'})
  153 |          int l = max(1LL, i-S[k].S);
      |                  ~~~^~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:303:5: note: candidate: 'template<class _Tp, class _Compare> constexpr const _Tp& std::max(const _Tp&, const _Tp&, _Compare)'
  303 |     max(const _Tp& __a, const _Tp& __b, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:303:5: note:   candidate expects 3 arguments, 2 provided
In file included from /usr/include/c++/14/algorithm:61:
/usr/include/c++/14/bits/stl_algo.h:5706:5: note: candidate: 'template<class _Tp> constexpr _Tp std::max(initializer_list<_Tp>)'
 5706 |     max(initializer_list<_Tp> __l)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5706:5: note:   candidate expects 1 argument, 2 provided
/usr/include/c++/14/bits/stl_algo.h:5716:5: note: candidate: 'template<class _Tp, class _Compare> constexpr _Tp std::max(initializer_list<_Tp>, _Compare)'
 5716 |     max(initializer_list<_Tp> __l, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5716:5: note:   template argument deduction/substitution failed:
a.cc:153:21: note:   mismatched types 'std::initializer_list<_Tp>' and 'long long int'
  153 |          int l = max(1LL, i-S[k].S);
      |                  ~~~^~~~~~~~~~~~~~~
a.cc:154:21: error: no matching function for call to 'min(long long int, int64_t)'
  154 |          int r = min(i-1LL, i-S[k].F);
      |                  ~~~^~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:233:5: note: candidate: 'template<class _Tp> constexpr const _Tp& std::min(const _Tp&, const _Tp&)'
  233 |     min(const _Tp& __a, const _Tp& __b)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:233:5: note:   template argument deduction/substitution failed:
a.cc:154:21: note:   deduced conflicting types for parameter 'const _Tp' ('long long int' and 'int64_t' {aka 'long int'})
  154 |          int r = min(i-1LL, i-S[k].F);
      |                  ~~~^~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:281:5: note: candidate: 'template<class _Tp, class _Compare> constexpr const _Tp& std::min(const _Tp&, const _Tp&, _Compare)'
  281 |     min(const _Tp& __a, const _Tp& __b, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:281:5: note:   candidate expects 3 arguments, 2 provided
/usr/include/c++/14/bits/stl_algo.h:5686:5: note: candidate: 'template<class _Tp> constexpr _Tp std::min(initializer_list<_Tp>)'
 5686 |     min(initializer_list<_Tp> __l)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5686:5: note:   candidate expects 1 argument, 2 provided
/usr/include/c++/14/bits/stl_algo.h:5696:5: note: candidate: 'template<class _Tp, class _Compare> constexpr _Tp std::min(initializer_list<_Tp>, _Compare)'
 5696 |     min(initializer_list<_Tp> __l, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5696:5: note:   template argument deduction/substitution failed:
a.cc:154:21: note:   mismatched types 'std::initializer_list<_Tp>' and 'long long int'
  154 |          int r = min(i-1LL, i-S[k].F);
      |                  ~~~^~~~~~~~~~~~~~~~~