QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#741574#9631. Median ReplacementDiaoTianhaoWA 16ms43532kbC++147.8kb2024-11-13 14:40:122024-11-13 14:40:19

Judging History

你现在查看的是最新测评结果

  • [2024-11-13 14:40:19]
  • 评测
  • 测评结果:WA
  • 用时:16ms
  • 内存:43532kb
  • [2024-11-13 14:40:12]
  • 提交

answer

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

#define bug(x) << #x << "=" << (x) << " "

namespace IO {
	inline ll rd() {
		char ch = getchar();
		ll s = 0, w = 1;
		while (ch < '0' || ch > '9') {
			if (ch == '-') w = -1;
			ch = getchar();
		}
		while (ch >= '0' && ch <= '9') {
			s = (s << 3) + (s << 1) + (ch ^ 48);
			ch = getchar();
		}
		return (s * w);
	}
	void wr(ll x) {
		if (x < 0) {
			putchar('-');
			x = -x;
		}
		if (x <= 9) {
			putchar(x + 48);
			return;
		}
		wr(x / 10);
		putchar(x % 10 + 48);
	}
}
using namespace IO;

const ll fn = 1000;
const ll N = fn + 10;

inline namespace ModInt {
	constexpr ll MO = 998244353;
	constexpr inline ll moa(ll x) {
		return (x >= MO ? x - MO : x);
	}
	constexpr inline ll moi(ll x) {
		return (x < 0 ? x + MO : x);
	}
	constexpr inline ll mo(ll x) {
		return moi(x % MO);
	}
	constexpr inline ll qpow(ll x, ll y) {
		x = mo(x);
		ll k = 1;
		while (y) {
			if (y & 1) k *= x, k %= MO;
			x *= x, x %= MO;
			y >>= 1;
		}
		return k;
	}
	constexpr inline ll get_inv(ll x) {
		return qpow(x, MO - 2);
	}

	constexpr inline void ckadd(ll &x, ll y) {
		x = moa(x + y);
	}
	constexpr inline void cksub(ll &x, ll y) {
		x = moi(x - y);
	}

	constexpr inline ll nop(ll x, ll k) {
		return (k % 2 == 0 ? x : moi(-x));
	}
} // namespace ModInt

inline namespace ModIntBase {
	ll inv[N], jc[N], ji[N];
	void init_inv() {
		inv[0] = inv[1] = 1;
		for (ll i = 2; i <= fn; ++i) {
			inv[i] = (MO - MO / i) * inv[MO % i] % MO;
		}

		jc[0] = ji[0] = 1;
		for (ll i = 1; i <= fn; ++i) {
			jc[i] = jc[i - 1] * i % MO;
			ji[i] = ji[i - 1] * inv[i] % MO;
		}
	}
} // namespace ModIntBase

namespace Math {
	struct Lagrange {
		ll n, X[N], Y[N];
		inline ll operator () (ll x) {
			ll res = 0;
			for (ll i = 0; i < n; ++i) {
				ll u = Y[i], v = 1;
				for (ll j = 0; j < n; ++j) {
					if (j == i) continue;
					u = u * mo(x - X[j]) % MO;
					v = v * mo(X[i] - X[j]) % MO;
				}
				res = (res + u * get_inv(v)) % MO;
			}
			return res;
		}
	};
	struct ConsecutiveLagrange {
		ll n, Y[N];
		inline ll operator () (ll x) {
			static ll pre[N], suf[N];
			ll rod = 1;
			for (ll i = 0; i < n; ++i) {
				pre[i] = rod;
				rod = rod * mo(x - i) % MO;
			}
			rod = 1;
			for (ll i = n - 1; i >= 0; --i) {
				suf[i] = rod;
				rod = rod * mo(x - i) % MO;
			}

			ll res = 0;
			for (ll i = 0; i < n; ++i) {
				ckadd(res, nop(Y[i] * pre[i] % MO * suf[i] % MO * ji[i] % MO * ji[n - 1 - i] % MO, n - 1 - i));
			}
			return res;
		}
	};
	ConsecutiveLagrange SumOfPower[N];
	void init_SumOfPower() {
		for (ll i = 0; i <= fn; ++i) {
			SumOfPower[i].n = i + 2;
			ll sum = 0;
			for (ll j = 0; j <= i + 1; ++j) {
				ckadd(sum, qpow(j, i));
				SumOfPower[i].Y[j] = sum;
			}
		}
	}
} // namespace Math

namespace Polynomial {
	struct TwoTermPoly {
		ll u, v;
		inline TwoTermPoly() : u(0), v(0) {}
		inline TwoTermPoly(ll u, ll v) : u(u), v(v) {}
	};

	struct poly {
		ll n, f[N];
		inline poly() {
			n = 0;
		}
		static inline poly unit() {
			poly res;
			res.n = 1;
			res.f[0] = 1;
			return res;
		}
		inline bool empty() {
			return n == 0;
		}
		inline void resize(ll _n) {
			for (ll i = n; i < _n; ++i) {
				f[i] = 0;
			}
			n = _n;
		}
		friend inline poly operator + (const poly &A, const poly &B) {
			poly res = A;
			res.resize(max(A.n, B.n));
			for (ll i = 0; i < B.n; ++i) {
				ckadd(res.f[i], B.f[i]);
			}
			return res;
		}
		inline poly& operator += (const poly &A) {
			resize(max(n, A.n));
			for (ll i = 0; i < A.n; ++i) {
				ckadd(f[i], A.f[i]);
			}
			return *this;
		}
		friend inline poly operator - (const poly &A, const poly &B) {
			poly res = A;
			res.resize(max(A.n, B.n));
			for (ll i = 0; i < B.n; ++i) {
				cksub(res.f[i], B.f[i]);
			}
			return res;
		}
		inline poly& operator -= (const poly &A) {
			resize(max(n, A.n));
			for (ll i = 0; i < A.n; ++i) {
				cksub(f[i], A.f[i]);
			}
			return *this;
		}
		inline poly operator * (const TwoTermPoly &k) {
			poly res;
			res.resize(n + 1);
			for (ll i = 0; i < n; ++i) {
				ckadd(res.f[i + 1], f[i] * k.u % MO);
				ckadd(res.f[i], f[i] * k.v % MO);
			}
			return res;
		}
		// inline poly iteg() {
		// 	poly res;
		// 	res.n = n + 1;
		// 	for (ll i = 1; i <= n; ++i) {
		// 		res.f[i] = f[i - 1] * inv[i] % MO;
		// 	}
		// 	return res;
		// }
		inline ll calc_prefix_sum(ll x) {
			ll res = 0;
			for (ll i = 0; i < n; ++i) {
				res = (res + f[i] * Math::SumOfPower[i](x)) % MO;
			}
			return res;
		}
	};
} // namespace Polynomial
using Polynomial::TwoTermPoly;
using Polynomial::poly;

ll n, m, L[N], R[N], b[N];

ll ans;
poly dp[N][2][2]; // whether <, whether <
TwoTermPoly poss[N]; // the number of schemes that a[i] < x
inline TwoTermPoly calc_poss(ll i, bool k) {
	return (k == 1 ? poss[i] : TwoTermPoly(moi(-poss[i].u), mo(R[i] - L[i] - poss[i].v)));
}
inline void proc_dp(ll k) {
	for (ll i = 1; i <= n; ++i) {
		poss[i] = (
			R[i] <= b[k - 1] ? TwoTermPoly(0, mo(R[i] - L[i])) :
			L[i] <= b[k - 1] ? TwoTermPoly(1, mo(-L[i])) :
			TwoTermPoly(0, 0)
		);
	}

	for (ll i = 0; i <= n; ++i) {
		for (ll p = 0; p <= 1; ++p) {
			for (ll q = 0; q <= 1; ++q) {
				dp[i][p][q] = poly();
			}
		}
	}
	for (ll p = 0; p <= 1; ++p) {
		for (ll q = 0; q <= 1; ++q) {
			// if (!p && !q) {
			// 	// dp[2][p][q] = 0;
			// 	continue;
			// }
			dp[2][p][q] = poly::unit() * calc_poss(1, p) * calc_poss(2, q);
		}
	}

	for (ll i = 3; i <= n; ++i) {
		for (ll p = 0; p <= 1; ++p) {
			for (ll q = 0; q <= 1; ++q) {
				// if (!p && !q) {
				// 	continue;
				// }
				if (dp[i - 1][p][q].empty()) {
					continue;
				}
				// cerr bug(i - 1) bug(p) bug(q) bug(dp[i - 1][p][q].n) bug(dp[i - 1][p][q].f[0]) bug(dp[i - 1][p][q].f[1]) bug(dp[i - 1][p][q].f[2]) bug(dp[i - 1][p][q].f[3]) << endl;

				// (a[i] <= x) == 0
				if (!p + !q + 1 <= 1) {
					dp[i][q][0] += dp[i - 1][p][q] * calc_poss(i, 0);
				}

				// (a[i] <= x) == 1
				if (!p + !q <= 1) {
					dp[i][q][1] += dp[i - 1][p][q] * calc_poss(i, 1);
				}
			}
		}
	}

	ll rod = 1;
	for (ll i = 1; i <= n; ++i) {
		rod = rod * mo(R[i] - L[i]) % MO;
	}
	// cerr bug(rod) bug(L[2]) bug(R[2]) << endl;
	poly res;
	res.n = 1, res.f[0] = rod;
	for (ll p = 0; p <= 1; ++p) {
		for (ll q = 0; q <= 1; ++q) {
			// if (!p && !q) {
			// 	continue;
			// }
			if (dp[n][p][q].empty()) {
				continue;
			}
			// cerr bug(n) bug(p) bug(q) bug(dp[n][p][q].n) bug(dp[n][p][q].f[0]) bug(dp[n][p][q].f[1]) bug(dp[n][p][q].f[2]) bug(dp[n][p][q].f[3]) << endl;

			res -= dp[n][p][q];
		}
	}
	ll val = moi(res.calc_prefix_sum(b[k] - 1) - res.calc_prefix_sum(b[k - 1] - 1));
	// cerr bug(res.n) bug(res.f[0]) bug(res.f[1]) bug(res.f[2]) bug(res.f[3]) bug(b[k - 1]) bug(b[k]) bug(val) << endl;
	ckadd(ans, val);
}

void solve_tc() {
	n = rd();
	m = 0;
	b[++m] = 1;
	for (ll i = 1; i <= n; ++i) {
		L[i] = rd();
		b[++m] = L[i];
	}
	for (ll i = 1; i <= n; ++i) {
		R[i] = rd() + 1;
		b[++m] = R[i];
	}
	sort(b + 1, b + m + 1);
	m = unique(b + 1, b + m + 1) - (b + 1);

	ans = 0;
	for (ll i = 2; i <= m; ++i) {
		// if (b[i] - b[i - 1] >= n + 5) {
		// 	Lag.n = n + 5;
		// 	for (ll j = 0; j < n + 5; ++j) {
		// 		Lag.X[j] = b[i] - j;
		// 		Lag.Y[j] = DP::calc_dp(b[i] - j);
		// 	}
		// 	ckadd(ans, Lag(b[i]) - Lag(b[i - 1]));
		// }
		// else {
		// 	for (ll j = b[i]; j > b[i - 1]; --j) {
		// 		ckadd(ans, DP::calc_dp(j));
		// 	}
		// }
		proc_dp(i);
	}

	wr(ans), putchar('\n');
}
int main() {
	init_inv();
	Math::init_SumOfPower();

	// cerr bug(Math::SumOfPower[1].Y[1]) << endl;
	// cerr bug(Math::SumOfPower[2](4)) << endl;

	ll T = rd();
	while (T--) {
		solve_tc();
	}
	return 0;
}

详细

Test #1:

score: 100
Accepted
time: 12ms
memory: 43440kb

input:

10
5
5 1 4 3 2
14 2 5 3 2
5
4 5 1 2 3
13 7 1 2 3
5
5 2 5 3 1
10 2 12 3 2
5
5 5 3 1 5
57 5 3 1 5
5
2 2 3 3 5
4 5 4 4 5
5
4 5 3 5 3
13 7 3 5 3
5
5 1 4 2 3
14 3 4 2 3
5
1 2 5 4 5
2 8 5 7 5
5
1 1 3 5 1
8 2 3 8 1
5
4 4 4 2 3
5 10 5 2 3

output:

180
170
650
265
182
173
120
296
192
131

result:

ok 10 lines

Test #2:

score: 0
Accepted
time: 10ms
memory: 43532kb

input:

10
5
1 2 2 5 3
6 4 2 6 3
5
4 4 1 4 3
6 7 2 5 3
5
5 3 4 2 4
5 7 5 2 6
5
1 5 3 5 2
7 7 3 5 2
5
1 3 3 2 2
10 5 3 2 2
5
4 4 4 5 3
4 11 9 5 3
5
5 3 2 1 3
13 5 2 1 5
5
5 4 1 2 5
10 6 1 2 5
5
3 5 3 4 2
5 9 3 5 2
5
1 1 3 2 1
7 3 3 3 1

output:

120
230
144
110
110
289
324
89
140
122

result:

ok 10 lines

Test #3:

score: 0
Accepted
time: 16ms
memory: 43256kb

input:

10
5
3 1 3 4 4
9 1 3 10 4
5
1 1 3 1 1
9 1 3 3 1
5
5 1 2 3 1
74 1 2 3 1
5
2 5 5 3 4
5 6 8 3 4
5
2 1 3 4 5
2 4 6 4 5
5
2 4 2 1 3
2 11 3 2 3
5
1 5 4 4 2
1 14 6 6 2
5
4 1 3 5 1
9 2 4 5 1
5
4 1 2 4 4
6 1 6 4 4
5
3 2 5 3 5
8 8 5 3 5

output:

196
76
140
172
72
80
486
84
65
224

result:

ok 10 lines

Test #4:

score: -100
Wrong Answer
time: 16ms
memory: 42664kb

input:

10
5
676437428 903889545 700650370 965758082 146716866
676437431 903889567 700650370 965758082 146716866
5
517457740 64600397 388618400 783268973 388618400
517457797 64600397 388618400 783268973 388618400
5
971452763 106948541 259878781 537741075 9504353
971452780 106948544 259878781 537741075 95043...

output:

303557853
578491434
742873878
206396969
643664225
29103773
586414917
27082782
459571422
613252895

result:

wrong answer 1st lines differ - expected: '157838571', found: '303557853'