Submission #74339002
Source Code Expand
/*
author: Maksim1744
created: 22.03.2026 15:46:39
*/
#include "bits/stdc++.h"
using namespace std;
using ll = long long;
using ld = long double;
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define sum(a) ( accumulate ((a).begin(), (a).end(), 0ll))
#define mine(a) (*min_element((a).begin(), (a).end()))
#define maxe(a) (*max_element((a).begin(), (a).end()))
#define mini(a) ( min_element((a).begin(), (a).end()) - (a).begin())
#define maxi(a) ( max_element((a).begin(), (a).end()) - (a).begin())
#define lowb(a, x) ( lower_bound((a).begin(), (a).end(), (x)) - (a).begin())
#define uppb(a, x) ( upper_bound((a).begin(), (a).end(), (x)) - (a).begin())
template<typename T, typename U> pair<T,U>& operator-- (pair<T, U> &p){--p.first; --p.second; return p;}
template<typename T, typename U> pair<T,U>& operator++ (pair<T, U> &p){++p.first; ++p.second; return p;}
template<typename T, typename U> istream& operator>>(istream& is, pair<T, U> &p){is >> p.first >> p.second; return is;}
template<typename T, typename U> ostream& operator<<(ostream& os, pair<T, U> p){os << p.first << ' ' << p.second; return os;}
template<typename T> vector<T>& operator-- (vector<T> &v){for (auto& i : v) --i; return v;}
template<typename T> vector<T>& operator++ (vector<T> &v){for (auto& i : v) ++i; return v;}
template<typename T> istream& operator>>(istream& is, vector<T> &v){for (auto& i : v) is >> i; return is;}
template<typename T> ostream& operator<<(ostream& os, vector<T> v){for (auto& i : v) os << i << ' '; return os;}
template<typename T, typename U> pair<T,U> operator-(pair<T,U> a, pair<T,U> b){return mp(a.first-b.first, a.second-b.second);}
template<typename T, typename U> pair<T,U> operator+(pair<T,U> a, pair<T,U> b){return mp(a.first+b.first, a.second+b.second);}
template<typename T, typename U> void umin(T& a, U b){if (a > b) a = b;}
template<typename T, typename U> void umax(T& a, U b){if (a < b) a = b;}
#ifdef HOME
#define SHOW_COLORS
#include "/home/maksim/tools/programming-library/tools/print.cpp"
#else
#define show(...) void(0)
#define debugf(fun) fun
#define debugv(var) var
#define mclock void(0)
#define shows void(0)
#define debug if (false)
#define OSTREAM(...) ;
#define OSTREAM0(...) ;
#endif
namespace mint_ns {
template<auto P>
struct Modular {
using value_type = decltype(P);
value_type value;
Modular(long long k = 0) : value(norm(k)) {}
friend Modular<P>& operator += ( Modular<P>& n, const Modular<P>& m) { n.value += m.value; if (n.value >= P) n.value -= P; return n; }
friend Modular<P> operator + (const Modular<P>& n, const Modular<P>& m) { Modular<P> r = n; return r += m; }
friend Modular<P>& operator -= ( Modular<P>& n, const Modular<P>& m) { n.value -= m.value; if (n.value < 0) n.value += P; return n; }
friend Modular<P> operator - (const Modular<P>& n, const Modular<P>& m) { Modular<P> r = n; return r -= m; }
friend Modular<P> operator - (const Modular<P>& n) { return Modular<P>(-n.value); }
friend Modular<P>& operator *= ( Modular<P>& n, const Modular<P>& m) { n.value = n.value * 1ll * m.value % P; return n; }
friend Modular<P> operator * (const Modular<P>& n, const Modular<P>& m) { Modular<P> r = n; return r *= m; }
friend Modular<P>& operator /= ( Modular<P>& n, const Modular<P>& m) { return n *= m.inv(); }
friend Modular<P> operator / (const Modular<P>& n, const Modular<P>& m) { Modular<P> r = n; return r /= m; }
Modular<P>& operator ++ ( ) { return *this += 1; }
Modular<P>& operator -- ( ) { return *this -= 1; }
Modular<P> operator ++ (int) { Modular<P> r = *this; *this += 1; return r; }
Modular<P> operator -- (int) { Modular<P> r = *this; *this -= 1; return r; }
friend bool operator == (const Modular<P>& n, const Modular<P>& m) { return n.value == m.value; }
friend bool operator != (const Modular<P>& n, const Modular<P>& m) { return n.value != m.value; }
explicit operator int() const { return value; }
explicit operator bool() const { return value; }
explicit operator long long() const { return value; }
constexpr static value_type mod() { return P; }
value_type norm(long long k) {
if (!(-P <= k && k < P)) k %= P;
if (k < 0) k += P;
return k;
}
Modular<P> inv() const {
value_type a = value, b = P, x = 0, y = 1;
while (a != 0) { value_type k = b / a; b -= k * a; x -= k * y; swap(a, b); swap(x, y); }
return Modular<P>(x);
}
};
template<auto P> Modular<P> pow(Modular<P> m, long long p) {
Modular<P> r(1);
while (p) {
if (p & 1) r *= m;
m *= m;
p >>= 1;
}
return r;
}
template<auto P> ostream& operator << (ostream& o, const Modular<P>& m) { return o << m.value; }
template<auto P> istream& operator >> (istream& i, Modular<P>& m) { long long k; i >> k; m.value = m.norm(k); return i; }
template<auto P> string to_string(const Modular<P>& m) { return to_string(m.value); }
// using Mint = Modular<1000000007>;
using Mint = Modular<998244353>;
// using Mint = long double;
vector<Mint> f, fi;
void init_C(int n) {
f.assign(n, 1); fi.assign(n, 1);
for (int i = 2; i < n; ++i) f[i] = f[i - 1] * i;
fi.back() = Mint(1) / f.back();
for (int i = n - 2; i >= 0; --i) fi[i] = fi[i + 1] * (i + 1);
}
Mint C(int n, int k) {
if (k < 0 || k > n) return 0;
else return f[n] * fi[k] * fi[n - k];
}
}
using namespace mint_ns;
int main() {
ios_base::sync_with_stdio(false); cin.tie(NULL);
int n, q;
cin >> n >> q;
init_C(n * 2 + 10);
vector<int> a(n);
cin >> a;
vector<char> u(n, false);
for (int i : a)
if (i >= 0)
u[i] = true;
vector<int> s(n);
s[0] = !u[0];
for (int i = 1; i < n; ++i)
s[i] = s[i - 1] + !u[i];
int B = count(a.begin(), a.end(), -1);
const auto& fact = f;
const auto& facti = fi;
vector<vector<Mint>> f(n + 1, vector<Mint>(n));
for (int A = 0; A <= B; ++A) {
for (int k = 0; k < n; ++k) {
if (A >= s[k])
f[A][k] = fact[A] * facti[A - s[k]] * fact[B - s[k]];
if (k)
f[A][k] += f[A][k - 1];
}
}
show(f);
vector<int> minl(n, 1e9);
for (int i = 0; i < n; ++i) {
if (i) minl[i] = minl[i - 1];
if (a[i] != -1)
minl[i] = min(minl[i], a[i]);
}
vector<int> minr(n, 1e9);
for (int i = n - 1; i >= 0; --i) {
if (i < n - 1) minr[i] = minr[i + 1];
if (a[i] != -1)
minr[i] = min(minr[i], a[i]);
}
vector<int> prefempty(n, 0);
for (int i = 0; i < n; ++i) {
if (i) prefempty[i] = prefempty[i - 1];
prefempty[i] += (a[i] == -1);
}
show(prefempty);
while (q--) {
int l, r;
cin >> l >> r;
--l; --r;
int minout = n;
if (l) minout = min(minout, minl[l - 1]);
if (r < n - 1) minout = min(minout, minr[r + 1]);
int A = prefempty[r];
if (l) A -= prefempty[l - 1];
cout << (minout == 0 ? 0 : f[A][minout - 1]) << '\n';
cout.flush();
}
return 0;
}
Submission Info
| Submission Time |
|
| Task |
B - Range Mex Sum |
| User |
Maksim1744 |
| Language |
C++23 (GCC 15.2.0) |
| Score |
700 |
| Code Size |
7634 Byte |
| Status |
AC |
| Exec Time |
385 ms |
| Memory |
103824 KiB |
Judge Result
| Set Name |
Sample |
All |
| Score / Max Score |
0 / 0 |
700 / 700 |
| Status |
|
|
| Set Name |
Test Cases |
| Sample |
00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt |
| All |
00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt, 01_test_00.txt, 01_test_01.txt, 01_test_02.txt, 01_test_03.txt, 01_test_04.txt, 01_test_05.txt, 01_test_06.txt, 01_test_07.txt, 01_test_08.txt, 01_test_09.txt, 01_test_10.txt, 01_test_11.txt, 01_test_12.txt, 01_test_13.txt, 01_test_14.txt, 01_test_15.txt, 01_test_16.txt, 01_test_17.txt, 01_test_18.txt, 01_test_19.txt, 01_test_20.txt, 01_test_21.txt, 01_test_22.txt, 01_test_23.txt, 01_test_24.txt, 01_test_25.txt, 01_test_26.txt, 01_test_27.txt, 01_test_28.txt, 01_test_29.txt, 01_test_30.txt, 01_test_31.txt, 01_test_32.txt, 01_test_33.txt, 01_test_34.txt, 01_test_35.txt, 01_test_36.txt, 01_test_37.txt, 01_test_38.txt, 01_test_39.txt, 01_test_40.txt, 01_test_41.txt, 01_test_42.txt, 01_test_43.txt, 01_test_44.txt, 01_test_45.txt, 01_test_46.txt, 01_test_47.txt, 01_test_48.txt, 01_test_49.txt, 01_test_50.txt, 01_test_51.txt |
| Case Name |
Status |
Exec Time |
Memory |
| 00_sample_00.txt |
AC |
1 ms |
3592 KiB |
| 00_sample_01.txt |
AC |
1 ms |
3560 KiB |
| 00_sample_02.txt |
AC |
1 ms |
3620 KiB |
| 01_test_00.txt |
AC |
206 ms |
9492 KiB |
| 01_test_01.txt |
AC |
214 ms |
9512 KiB |
| 01_test_02.txt |
AC |
210 ms |
9532 KiB |
| 01_test_03.txt |
AC |
209 ms |
9516 KiB |
| 01_test_04.txt |
AC |
209 ms |
9520 KiB |
| 01_test_05.txt |
AC |
208 ms |
9464 KiB |
| 01_test_06.txt |
AC |
209 ms |
9408 KiB |
| 01_test_07.txt |
AC |
203 ms |
7376 KiB |
| 01_test_08.txt |
AC |
208 ms |
7432 KiB |
| 01_test_09.txt |
AC |
207 ms |
7368 KiB |
| 01_test_10.txt |
AC |
205 ms |
7460 KiB |
| 01_test_11.txt |
AC |
200 ms |
7408 KiB |
| 01_test_12.txt |
AC |
203 ms |
7320 KiB |
| 01_test_13.txt |
AC |
319 ms |
103604 KiB |
| 01_test_14.txt |
AC |
328 ms |
103588 KiB |
| 01_test_15.txt |
AC |
323 ms |
103752 KiB |
| 01_test_16.txt |
AC |
327 ms |
103652 KiB |
| 01_test_17.txt |
AC |
325 ms |
103756 KiB |
| 01_test_18.txt |
AC |
325 ms |
103824 KiB |
| 01_test_19.txt |
AC |
337 ms |
103644 KiB |
| 01_test_20.txt |
AC |
325 ms |
103668 KiB |
| 01_test_21.txt |
AC |
343 ms |
103784 KiB |
| 01_test_22.txt |
AC |
316 ms |
103764 KiB |
| 01_test_23.txt |
AC |
343 ms |
101768 KiB |
| 01_test_24.txt |
AC |
307 ms |
103724 KiB |
| 01_test_25.txt |
AC |
340 ms |
103800 KiB |
| 01_test_26.txt |
AC |
285 ms |
103664 KiB |
| 01_test_27.txt |
AC |
312 ms |
101648 KiB |
| 01_test_28.txt |
AC |
260 ms |
101320 KiB |
| 01_test_29.txt |
AC |
284 ms |
103668 KiB |
| 01_test_30.txt |
AC |
257 ms |
101796 KiB |
| 01_test_31.txt |
AC |
280 ms |
101640 KiB |
| 01_test_32.txt |
AC |
246 ms |
101328 KiB |
| 01_test_33.txt |
AC |
252 ms |
101740 KiB |
| 01_test_34.txt |
AC |
248 ms |
101740 KiB |
| 01_test_35.txt |
AC |
251 ms |
101708 KiB |
| 01_test_36.txt |
AC |
243 ms |
101284 KiB |
| 01_test_37.txt |
AC |
249 ms |
101728 KiB |
| 01_test_38.txt |
AC |
246 ms |
101312 KiB |
| 01_test_39.txt |
AC |
249 ms |
101344 KiB |
| 01_test_40.txt |
AC |
239 ms |
101272 KiB |
| 01_test_41.txt |
AC |
248 ms |
101312 KiB |
| 01_test_42.txt |
AC |
385 ms |
103776 KiB |
| 01_test_43.txt |
AC |
377 ms |
103716 KiB |
| 01_test_44.txt |
AC |
369 ms |
103812 KiB |
| 01_test_45.txt |
AC |
360 ms |
103740 KiB |
| 01_test_46.txt |
AC |
343 ms |
103748 KiB |
| 01_test_47.txt |
AC |
323 ms |
103664 KiB |
| 01_test_48.txt |
AC |
306 ms |
103688 KiB |
| 01_test_49.txt |
AC |
277 ms |
103740 KiB |
| 01_test_50.txt |
AC |
2 ms |
3616 KiB |
| 01_test_51.txt |
AC |
1 ms |
3432 KiB |