Submission #967948

#TimeUsernameProblemLanguageResultExecution timeMemory
967948duckindogBoat (APIO16_boat)C++17
31 / 100
1525 ms524288 KiB
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#include <bits/stdc++.h>
 
using namespace std;
 
const int N = 500 + 10,
          M = 1'000'000'007;
int n;
int a[N], b[N];

template <int MOD=998'244'353>
struct Modular {
  int value;
  static const int MOD_value = MOD;

  Modular(long long v = 0) { value = v % MOD; if (value < 0) value += MOD;}
  Modular(long long a, long long b) : value(0){ *this += a; *this /= b;}

  Modular& operator+=(Modular const& b) {value += b.value; if (value >= MOD) value -= MOD; return *this;}
  Modular& operator-=(Modular const& b) {value -= b.value; if (value < 0) value += MOD;return *this;}
  Modular& operator*=(Modular const& b) {value = (long long)value * b.value % MOD;return *this;}

  friend Modular mexp(Modular a, long long e) {
    Modular res = 1; while (e) { if (e&1) res *= a; a *= a; e >>= 1; }
    return res;
  }
  friend Modular inverse(Modular a) { return mexp(a, MOD - 2); }

  Modular& operator/=(Modular const& b) { return *this *= inverse(b); }
  friend Modular operator+(Modular a, Modular const b) { return a += b; }
  friend Modular operator-(Modular a, Modular const b) { return a -= b; }
  friend Modular operator-(Modular const a) { return 0 - a; }
  friend Modular operator*(Modular a, Modular const b) { return a *= b; }
  friend Modular operator/(Modular a, Modular const b) { return a /= b; }
  friend std::ostream& operator<<(std::ostream& os, Modular const& a) {return os << a.value;}
  friend bool operator==(Modular const& a, Modular const& b) {return a.value == b.value;}
  friend bool operator!=(Modular const& a, Modular const& b) {return a.value != b.value;}
};

using modint = Modular<M>;
modint f[2][1'000'010];
 
int32_t main() { 
  cin.tie(0)->sync_with_stdio(0);
 
  cin >> n;
  for (int i = 1; i <= n; ++i) cin >> a[i] >> b[i];
 
  vector<int> rrh;
  for (int i = 1; i <= n; ++i) { 
    for (int j = a[i]; j <= b[i]; ++j) rrh.push_back(j);
  }
  sort(rrh.begin(), rrh.end());
  rrh.resize(unique(rrh.begin(), rrh.end()) - rrh.begin());

  f[0][0] = 1;
  for (int j = 1; j <= rrh.size(); ++j) f[0][j] = 1;
 
  for (int i = 1; i <= n; ++i) { 
    bool id = i & 1, p = id ^ 1;
    
    int it = lower_bound(rrh.begin(), rrh.end(), a[i]) - rrh.begin() + 1;
    
    f[id][0] = f[p][0];
    for (int j = 1; j <= rrh.size(); ++j) {
      if (j < it || j > b[i] - a[i] + it) {
        f[id][j] = f[id][j - 1] - f[p][j - 1] + f[p][j];
      } else {
        f[id][j] = f[id][j - 1] - f[p][j - 1] + f[p][j] + f[p][j - 1];
      }
    }
  }
  
  cout << f[n & 1][rrh.size()] - 1 << "\n";
  cerr << 1.0 * clock() / CLOCKS_PER_SEC << "\n";
}

Compilation message (stderr)

boat.cpp: In function 'int32_t main()':
boat.cpp:58:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   58 |   for (int j = 1; j <= rrh.size(); ++j) f[0][j] = 1;
      |                   ~~^~~~~~~~~~~~~
boat.cpp:66:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   66 |     for (int j = 1; j <= rrh.size(); ++j) {
      |                     ~~^~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...