QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#744756 | #9631. Median Replacement | cpchenpi | TL | 294ms | 7588kb | C++20 | 4.5kb | 2024-11-13 23:11:28 | 2024-11-13 23:11:46 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
constexpr int MOD = 1e9 + 7, N = 180;
// fact[k][n]: [x^n](1^k + 2^k + 3^k + ... x^k)
i64 comb[N + 5][N + 5], fact[N + 5][N + 5];
i64 modPow(i64 a, i64 p) {
i64 ans = 1;
while (p) {
if (p & 1) ans = ans * a % MOD;
a = a * a % MOD;
p >>= 1;
}
return ans;
}
i64 inv(i64 x) { return modPow(x, MOD - 2); }
void preCalc() {
comb[0][0] = 1;
for (int i = 1; i <= N; i++) {
comb[i][0] = 1;
for (int j = 1; j <= i; j++) {
comb[i][j] = comb[i - 1][j] + comb[i - 1][j - 1];
comb[i][j] %= MOD;
}
}
fact[0][1] = 1;
for (int k = 1; k <= N; k++) {
for (int i = 0; i <= k + 1; i++) {
(fact[k][i] += comb[k + 1][i]) %= MOD;
}
for (int p = 0; p < k; p++) {
for (int i = 0; i <= p + 1; i++) {
fact[k][i] -= comb[k + 1][p] * fact[p][i] % MOD;
fact[k][i] = (fact[k][i] + MOD) % MOD;
}
}
for (int i = 0; i <= k + 1; i++) {
(fact[k][i] *= inv(k + 1)) %= MOD;
}
}
}
using P = array<int, N + 1>;
i64 preSum(const P& t, int n) {
i64 res = 0;
for (int p = 0; p <= N; p++) {
i64 tmp = 0, pw = 1;
for (int i = 0; i <= p + 1; i++) {
(tmp += t[p] * fact[p][i] % MOD * pw % MOD) %= MOD;
pw = (pw * n % MOD) % MOD;
}
(res += tmp) %= MOD;
}
return res;
}
i64 rangeSum(const P& t, int l, int r) {
return (preSum(t, r) + MOD - preSum(t, l - 1)) % MOD;
}
P polyMul(P x, P y, int l) {
P ans{};
for (int i = 0; i <= N; i++) {
for (int j = 0; i + j <= min(l, N); j++) {
(ans[i + j] += (i64)x[i] * y[j] % MOD) %= MOD;
}
}
return ans;
}
using T = array<array<P, 3>, 3>;
T matMul(T a, T b, int l) {
T res{};
for (int i = 0; i < 3; i++) {
for (int k = 0; k < 3; k++) {
for (int j = 0; j < 3; j++) {
P tmp = polyMul(a[i][k], b[k][j], l);
for (int p = 0; p <= l && p <= N; p++) {
(res[i][j][p] += tmp[p]) %= MOD;
}
}
}
}
return res;
}
P ep() {
return {1};
}
T et() {
T res{};
res[0][0] = res[1][1] = res[2][2] = ep();
return res;
}
constexpr int w = 256;
struct Segtree {
T tr[2 * w]{};
Segtree() {
fill(tr, tr + 2 * w, et());
}
void set(int p, T x) {
int i = p + w, l = 2;
for (tr[i] = x, i >>= 1; i; i >>= 1) {
tr[i] = matMul(tr[2 * i], tr[2 * i + 1], l);
l <<= 1;
}
}
T allProd() { return tr[1]; }
};
T from01(P p0, P p1) {
T res{};
res[0][1] = res[1][2] = res[2][2] = p0;
res[2][0] = p1;
return res;
}
void solve() {
int n; cin >> n;
i64 allMul = 1;
vector<int> l(n), r(n);
for (int i = 0; i < n; i++) cin >> l[i];
for (int i = 0; i < n; i++) cin >> r[i];
Segtree tr{};
struct Event {
int time;
int pos;
array<array<int, 2>, 2> change;
bool operator<(const Event &o) {
return time < o.time;
}
};
vector<Event> events;
events.push_back({1, -1, {}});
for (int i = 0; i < n; i++) {
int s = r[i] - l[i] + 1;
(allMul *= s) %= MOD;
P p0 = {0}, p1 = {s};
tr.set(i, from01(p0, p1));
events.push_back({l[i], i, {{{(MOD - l[i]) % MOD, 1}, {(s + l[i]) % MOD, MOD - 1}}}});
events.push_back({r[i] + 1, i, {{{s}, {0}}}});
}
sort(events.begin(), events.end());
i64 ans = 0;
int m = events.size();
for (int i = 0; i < m; i++) {
auto [t, p, pr] = events[i];
if (p != -1) tr.set(p, from01({pr[0][0], pr[0][1]}, {pr[1][0], pr[1][1]}));
if (i + 1 < m && events[i].time != events[i + 1].time) {
int tml = events[i].time, tmr = events[i + 1].time - 1;
ans += (tmr - tml + 1) * allMul % MOD;
ans -= rangeSum(tr.allProd()[2][0], tml, tmr);
ans -= rangeSum(tr.allProd()[2][1], tml, tmr);
ans -= rangeSum(tr.allProd()[2][2], tml, tmr);
// cout << tml << " " << tmr << ": " << ans << "\n";
((ans %= MOD) += MOD) %= MOD;
}
}
cout << ans << "\n";
}
int main() {
preCalc();
int t; cin >> t;
while (t--) {
solve();
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 270ms
memory: 7588kb
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: 276ms
memory: 7428kb
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: 272ms
memory: 7432kb
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: 0
Accepted
time: 294ms
memory: 7584kb
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:
157838571 539867046 711272106 123881231 497944943 9791579 539012259 963879245 315607794 495624077
result:
ok 10 lines
Test #5:
score: 0
Accepted
time: 289ms
memory: 7584kb
input:
10 5 462008700 417744555 925098328 70231697 547596413 462008735 417744555 925098328 70231697 547596413 5 294230630 403894618 294230635 403894620 403894617 294230638 403894620 294230635 403894620 403894617 5 757647830 757647826 757647828 184694646 161891480 757647839 757647827 757647830 184694646 161...
output:
713470735 905154643 458869425 477055327 633375786 349097981 980046476 478461437 573246310 810688575
result:
ok 10 lines
Test #6:
score: -100
Time Limit Exceeded
input:
10 150 7 1 8 8 2 3 8 2 1 3 2 10 9 7 3 9 4 5 4 5 10 7 9 9 9 3 4 7 10 8 5 3 5 1 8 4 1 2 7 9 10 9 1 7 4 7 6 8 7 6 6 7 4 5 10 8 7 10 2 8 1 4 9 2 9 3 9 6 2 2 7 7 10 8 4 10 4 1 7 3 3 5 4 3 9 7 4 1 8 1 4 4 2 7 5 4 9 6 5 8 6 4 8 7 4 6 8 8 2 9 8 3 10 9 2 4 6 10 2 8 9 1 6 6 7 8 8 7 8 8 8 3 4 6 3 8 10 10 10 3 ...
output:
8640 8000