QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#339714 | #7899. Say Hello to the Future | goodier | TL | 0ms | 7912kb | C++14 | 4.6kb | 2024-02-27 20:27:35 | 2024-02-27 20:27:36 |
Judging History
answer
#include <bits/stdc++.h>
#define x first
#define y second
#define mp make_pair
using namespace std;
typedef pair<int,int> PII;
typedef long long ll;
const int N = 2e5 + 10;
const ll MOD = 998244353;
struct Query{
int id;
ll t;
PII lim;
};
ll f[N], g[N], c[3 * N], ans[N];
int a[N], len[N], n;
PII st[N][20], p[N];
void init()
{
for(int i = 2; i <= n; i++) len[i] = len[i >> 1] + 1;
for(int i = 1; i <= n; i++) st[i][0] = mp(a[i], i);
for(int j = 1; j <= len[n]; j++)
{
for(int i = 1; i + (1 << j) - 1 <= n; i++)
{
st[i][j] = max(st[i][j - 1], st[i + (1 << (j - 1))][j - 1]);
}
}
}
bool cmpx(PII a, PII b)
{
return a.x < b.x;
}
bool cmpy(PII a, PII b)
{
return a.y < b.y;
}
bool cmpxq(Query a, Query b)
{
return a.lim.x < b.lim.x;
}
bool cmpxq2(Query a, Query b)
{
return a.lim.x > b.lim.x;
}
void add(int x, ll y)
{
x += n;
for(; x <= n + n + n; x += x & -x) c[x] = (c[x] + y + MOD) % MOD;
}
ll ask(int x)
{
x += n;
ll res = 0;
for(; x; x -= x & -x) res = (res + c[x] + MOD) % MOD;
return res;
}
PII querymx(int l, int r)
{
int t = len[r - l + 1];
return max(st[l][t], st[r - (1 << t) + 1][t]);
}
void solve1(int l, int r, ll f[N])
{
if(l == r)
{
if(a[l] == 1)
{
f[l] = (f[l - 1] + f[l]) % MOD;
}
return;
}
int mid = l + r >> 1;
solve1(l, mid, f);
for(int i = l; i <= mid; i++)
{
p[i] = mp(i, querymx(i, mid).x + i - 1);
}
for(int i = mid + 1; i <= r; i++)
{
p[i] = mp(i, i - querymx(mid + 1, i).x + 1);
}
sort(p + l, p + mid + 1, cmpy), sort(p + mid + 1, p + r + 1, cmpx);
int i, j;
for(i = l, j = mid + 1; i <= mid && j <= r; )
{
if(p[i].y <= p[j].x)
{
add(p[i].x, f[p[i].x - 1]);
i++;
}
else
{
f[p[j].x] = (f[p[j].x] + ask(p[j].y)) % MOD;
j++;
}
}
while(j <= r)
{
f[p[j].x] = (f[p[j].x] + ask(p[j].y)) % MOD;
j++;
}
while(i > l)
{
i--;
add(p[i].x, -f[p[i].x - 1]);
}
solve1(mid + 1, r, f);
}
void solve2(int l, int r)
{
if(l == r)
{
if(a[l] > 1) ans[l] = (ans[l] + g[l + 1] * f[l - 1] % MOD) % MOD;
return;
}
vector <Query> ql, qr;
int mid = l + r >> 1;
for(int i = l; i <= mid; i++)
{
p[i] = mp(i, querymx(i, mid).x + i - 1);
}
for(int i = mid + 1; i <= r; i++)
{
p[i] = mp(i, i - querymx(mid + 1, i).x + 1);
}
for(int i = l; i <= mid; i++)
{
int pos = querymx(i, mid).y;
int nowmx = 1; if(i < pos) nowmx = max(nowmx, querymx(i, pos - 1).x); if(pos < mid) nowmx = max(nowmx, querymx(pos + 1, mid).x);
qr.push_back(Query({pos, -f[i - 1], mp(p[i].y, p[i].x)}));
qr.push_back(Query({pos, f[i - 1], mp(nowmx + i - 1, i)}));
}
for(int i = mid + 1; i <= r; i++)
{
int pos = querymx(mid + 1, i).y;
int nowmx = 1; if(mid + 1 < pos) nowmx = max(nowmx, querymx(mid + 1, pos - 1).x); if(pos < i) nowmx = max(nowmx, querymx(pos + 1, i).x);
ql.push_back(Query({pos, -g[i + 1], mp(p[i].y, p[i].x)}));
ql.push_back(Query({pos, g[i + 1], mp(i - nowmx + 1, i)}));
}
sort(ql.begin(), ql.end(), cmpxq), sort(qr.begin(), qr.end(), cmpxq2);
int i = l;
for(auto& q : ql)
{
while(i <= mid && i <= q.lim.x)
{
add(p[i].y, f[i - 1]);
i++;
}
ans[q.id] = (ans[q.id] + (ll)q.t * ask(q.lim.y) % MOD + MOD) % MOD;
}
while(i > l)
{
i--;
add(p[i].y, -f[i - 1]);
}
i = r;
for(auto& q : qr)
{
while(i >= q.lim.x && i > mid)
{
add(p[i].y, g[i + 1]);
i--;
}
ans[q.id] = (ans[q.id] + (ll)q.t * (ask(n) - ask(q.lim.y - 1) + MOD) % MOD + MOD) % MOD;
}
while(i < r)
{
i++;
add(p[i].y, -g[i + 1]);
}
solve2(l, mid), solve2(mid + 1, r);
}
int main()
{
//freopen("data.in", "r", stdin);
f[0] = g[0] = 1;
scanf("%d", &n);
for(int i = 1; i <= n; i++) scanf("%d", &a[i]);
reverse(a + 1, a + n + 1);
init();
solve1(1, n, g); for(int i = 0; i < n + 1 - i; i++) swap(g[i], g[n + 1 - i]);
reverse(a + 1, a + n + 1);
init();
solve1(1, n, f);
solve2(1, n);
for(int i = 1; i <= n; i++) printf("%lld ", (ans[i] + f[n]) % MOD);
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 7912kb
input:
5 1 3 2 1 2
output:
3 6 3 3 6
result:
ok 5 tokens
Test #2:
score: -100
Time Limit Exceeded
input:
200000 15922 15391 11782 4758 1973 19909 16800 6438 3821 986 18599 2011 338 19886 12401 4169 11143 12665 3230 12565 15065 15056 5090 16908 13677 12634 11435 1425 8647 3876 10694 12256 3853 19901 5723 11065 6147 13613 13768 15794 14993 5819 8117 13871 14708 15099 7152 9810 14438 10557 3209 1265 13915...
output:
157122482 826457499 826457499 826457499 826457499 826457499 826457499 826457499 826457499 826457499 826457499 826457499 826457499 826457499 826457499 826457499 826457499 826457499 826457499 826457499 826457499 826457499 826457499 826457499 826457499 826457499 826457499 826457499 826457499 826457499 ...