fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define fast_io ios::sync_with_stdio(0); cin.tie(0); cout.tie(0)
  5.  
  6. #define int long long
  7. #define pb push_back
  8. #define ff first
  9. #define ss second
  10. #define all(x) (x).begin(), (x).end()
  11. #define rall(x) (x).rbegin(), (x).rend()
  12. #define sz(x) ((int)(x).size())
  13. #define endl '\n'
  14. #define yes cout << "yes\n"
  15. #define no cout << "no\n"
  16.  
  17. #define rep(i,a,b) for(int i=a;i<b;++i)
  18. #define per(i,a,b) for(int i=b-1;i>=a;--i)
  19. #define each(x, a) for (auto& x : a)
  20.  
  21. const int INF = 1e18;
  22. const int MOD = 998244353;
  23. const int N = 3e5 + 5;
  24.  
  25. int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }
  26. int lcm(int a, int b) { return (a / gcd(a, b)) * b; }
  27.  
  28. int power(int a, int b, int m = MOD) {
  29. int res = 1;
  30. while (b > 0) {
  31. if (b & 1) res = res * a % m;
  32. a = a * a % m;
  33. b >>= 1;
  34. }
  35. return res;
  36. }
  37.  
  38. int modinv(int a, int m = MOD) {
  39. return power(a, m - 2, m);
  40. }
  41.  
  42. void solve() {
  43. int n, m;
  44. cin >> n >> m;
  45. vector<int> limit(n + 1, 0);
  46. rep(i, 0, m) {
  47. int l, r;
  48. cin >> l >> r;
  49. limit[r] = max(limit[r], l);
  50. }
  51. rep(i, 1, n + 1) {
  52. limit[i] = max(limit[i], limit[i - 1]);
  53. }
  54. vector<int> dp(n + 1, 0);
  55. vector<int> sdp(n + 1, 0);
  56. dp[0] = 1;
  57. sdp[0] = 1;
  58. rep(i, 1, n + 1) {
  59. int l = limit[i];
  60. int r = i - 1;
  61. if (l <= r) {
  62. int current_sum = sdp[r];
  63. int prev_sum = (l > 0) ? sdp[l - 1] : 0;
  64. dp[i] = (current_sum - prev_sum + MOD) % MOD;
  65. } else {
  66. dp[i] = 0;
  67. }
  68. sdp[i] = (sdp[i - 1] + dp[i]) % MOD;
  69. }
  70. cout << (2 * dp[n]) % MOD << endl;
  71. }
  72.  
  73. int32_t main() {
  74. fast_io;
  75.  
  76. int t;
  77. cin >> t;
  78. while (t--) {
  79. solve();
  80. }
  81.  
  82. return 0;
  83. }
Success #stdin #stdout 0.01s 5284KB
stdin
3
4 3
1 2
2 3
3 4
4 2
1 2
3 4
200 1
13 37
stdout
2
4
570529459