QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#339725 | #7899. Say Hello to the Future | goodier | WA | 2ms | 9988kb | C++14 | 4.6kb | 2024-02-27 20:30:30 | 2024-02-27 20:30:30 |
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;
if(!x) exit(0);
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;
}
詳細信息
Test #1:
score: 0
Wrong Answer
time: 2ms
memory: 9988kb
input:
5 1 3 2 1 2
output:
3 3 3 3 3
result:
wrong answer 2nd words differ - expected: '6', found: '3'