QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#758100 | #8813. Records in Chichén Itzá | ucup-team902# | WA | 6ms | 26448kb | C++20 | 4.9kb | 2024-11-17 15:49:32 | 2024-11-17 15:49:33 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 5e5;
const int mod = 998244353;
int add(int x, int y) { return (x += y) >= mod ? x - mod : x; }
int sub(int x, int y) { return (x -= y) < 0 ? x + mod : x; }
int ksm(ll x, int tp, int s = 1)
{
for (; tp; x = x * x % mod, tp >>= 1)
if (tp & 1)
s = x * s % mod;
return s;
}
int kp1[N + 5], kp2[N + 5];
int n;
int p[N + 5], rp[N + 5];
int l[N + 5], r[N + 5];
int fa[N + 5];
int cal[N + 5], ical[N + 5];
int sc[N + 5], isc[N + 5];
int f[N + 5];
int rt;
// int rt, ch[N + 5][2], sz[N + 5];
// vector<int> f[N + 5], ff[N + 5], h[N + 5];
// int g[N + 5];
int kpow(ll x)
{
return 1ll * kp2[x / N] * kp1[x % N] % mod;
}
void prep()
{
kp1[0] = 1;
kp2[0] = 1;
for (int i = 1; i <= N; i++)
kp1[i] = add(kp1[i - 1], kp1[i - 1]);
for (int i = 1; i <= N; i++)
kp2[i] = 1ll * kp2[i - 1] * kp1[N] % mod;
static int st[N + 5];
int ss = 0;
st[0] = 0;
for (int i = 1; i <= n; i++)
{
while (ss && p[st[ss]] < p[i])
{
r[st[ss]] = i;
ss--;
}
l[i] = st[ss];
st[++ss] = i;
}
while (ss)
{
r[st[ss]] = n + 1;
ss--;
}
p[0] = p[n + 1] = n + 1;
rt = st[1];
for (int i = 1; i <= n; i++)
{
if (i == rt)
continue;
int u = p[l[i]] < p[r[i]] ? l[i] : r[i];
fa[i] = u;
}
sc[0] = isc[0] = 1;
for (int i = 1; i <= n; i++)
{
int pos = rp[i];
cal[i] = kpow(1ll * (pos - l[pos]) * (r[pos] - pos)) - 1;
ical[i] = ksm(cal[i], mod - 2);
sc[i] = 1ll * sc[i - 1] * cal[i] % mod;
isc[i] = 1ll * isc[i - 1] * ical[i] % mod;
}
// for (int i = 1; i <= n; i++)
// printf("%d %d %d %d\n", l[i], r[i], ch[i][0], ch[i][1]);
}
// void dp(int u)
// {
// sz[u] = r[u] - l[u] - 1;
// int lc = ch[u][0], rc = ch[u][1];
// if (lc)
// dp(lc);
// if (rc)
// dp(rc);
// f[u].resize(sz[u] + 1);
// ff[u].resize(sz[u] + 1);
// h[u].reserve(sz[u] + 1);
// if (!lc && !rc)
// {
// f[u][1] = 1;
// g[u] = 1;
// return;
// }
// ll ls = u - l[u], rs = r[u] - u;
// g[u] = 1ll * g[lc] * g[rc] % mod * sub(kpow(ls * rs), 1) % mod;
// f[u][ls] = 1ll * g[lc] * g[rc] % mod;
// if (rc)
// {
// for (int i = 1; i <= sz[rc]; i++)
// {
// int c = 1ll * (kpow(i * ls) - 1) * (kpow((sz[rc] - i + 1) * ls) - 1) % mod;
// g[u] = (g[u] + 1ll * g[lc] * f[rc][i] % mod * c) % mod;
// c = kpow(i * ls) - 1;
// f[u][i + ls] = (f[u][i + ls] + 1ll * g[lc] * f[rc][i] % mod * c) % mod;
// }
// }
// if (lc)
// {
// for (int i = 1; i <= sz[lc]; i++)
// {
// int c = 1ll * (kpow((sz[lc] - i + 1) * rs) - 1) * (kpow(i * rs) - 1) % mod;
// g[u] = (g[u] + 1ll * g[rc] * f[lc][i] % mod * c) % mod;
// c = kpow((sz[lc] - i + 1) * rs) - 1;
// f[u][i] = (f[u][i] + 1ll * g[rc] * f[lc][i] % mod * c) % mod;
// }
// }
// // printf("u %d\n", g[u]);
// }
int main()
{
scanf("%d", &n);
for (int i = 1; i <= n; i++)
{
scanf("%d", &p[i]);
rp[p[i]] = i;
}
prep();
int ans = 0;
f[0] = 1;
for (int i = 1; i <= n; i++)
{
f[i] = (f[i] + 1ll * f[i - 1] * cal[i]) % mod;
int pos = rp[i];
int coe = 1ll * f[i - 1] * isc[i] % mod;
for (int j = fa[pos]; j; j = fa[j])
{
int fv = p[j];
coe = 1ll * coe * ical[fv] % mod;
int c;
if (pos < j)
{
ll rs = r[j] - j;
c = 1ll * (kpow((j - pos) * rs) - 1) * (kpow((pos - l[pos]) * rs) - 1) % mod;
}
else
{
ll ls = j - l[j];
c = 1ll * (kpow((pos - j) * ls) - 1) * (kpow((r[pos] - pos) * ls) - 1) % mod;
}
f[fv] = (f[fv] + 1ll * coe * c % mod * sc[fv]) % mod;
if (pos < j)
{
ll rs = r[j] - j;
c = kpow((j - pos) * rs) - 1;
}
else
{
ll ls = j - l[j];
c = kpow((pos - j) * ls) - 1;
}
coe = 1ll * coe * c % mod;
}
ans = (ans + 1ll * coe * sc[n]) % mod;
printf("%d %d\n", i, 1ll * coe * sc[n] % mod);
// printf("i: %d\n", f[i]);
}
// puts("");
ans = add(ans, f[n]);
// g[0] = 1;
// dp(rt);
// int ans = g[rt];
// for (int i = 1; i <= n; i++)
// ans = add(ans, f[rt][i]);
// printf("%d\n", ans);
printf("%d\n", ans);
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Wrong Answer
time: 6ms
memory: 26448kb
input:
3 6 1 1 1 1 3 3 5 1 1 2 2 2 10 1 1 1 1 2 2 2 2 3 3
output:
1 0 2 0 3 0 0
result:
wrong answer 1st words differ - expected: 'No', found: '1'