QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#333359 | #7899. Say Hello to the Future | c20230201 | WA | 864ms | 110912kb | C++14 | 5.3kb | 2024-02-19 20:52:47 | 2024-02-19 20:52:47 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll maxn=4e5+5, mo=998244353;
ll n;
ll a[maxn], f[maxn], g[maxn], lmx[maxn], rmx[maxn], ans[maxn];
struct node {
ll x;
ll y;
ll id;
}b[maxn];
bool cmp(node x,node y) {
if(x.x!=y.x) return x.x<y.x;
if(x.y!=y.y) return x.y<y.y;
return x.id<y.id;
}
struct BIT {
ll bit[maxn];
ll lowbit(ll x) {
return x&(-x);
}
void add(ll x,ll v) {
for(ll i=x;i<=n;i+=lowbit(i)) bit[i]=(bit[i]+v)%mo;
return ;
}
ll ask(ll x) {
ll res=0;
for(ll i=x;i>0;i-=lowbit(i)) res=(res+bit[i])%mo;
return res;
}
void add2(ll x,ll v) {
for(ll i=x;i>0;i-=lowbit(i)) bit[i]=(bit[i]+v)%mo;
return ;
}
ll ask2(ll x) {
ll res=0;
for(ll i=x;i<=n;i+=lowbit(i)) res=(res+bit[i])%mo;
return res;
}
}T;
void solve1(ll l,ll r) {
if(l==r) {
if(a[l]==1) f[l]=(f[l]+f[l-1])%mo;
return ;
}
ll mid=l+r>>1;
solve1(l,mid);
lmx[mid]=a[mid];
for(ll j=mid-1;j>=l;j--) lmx[j]=max(lmx[j+1],a[j]);
rmx[mid+1]=a[mid+1];
for(ll j=mid+2;j<=r;j++) rmx[j]=max(rmx[j-1],a[j]);
for(ll j=l;j<=mid;j++) b[j]=(node){j,lmx[j]+j-1,j};
for(ll j=mid+1;j<=r;j++) b[j]=(node){j-rmx[j]+1,j,j};
sort(b+l,b+r+1,cmp);
for(ll j=l;j<=r;j++) {
if(b[j].id>mid) f[b[j].id]=(f[b[j].id]+T.ask(b[j].y))%mo;
else T.add(b[j].y,f[b[j].id-1]);
}
for(ll j=l;j<=r;j++) if(b[j].id<=mid) T.add(b[j].y,mo-f[b[j].id-1]);
solve1(mid+1,r);
return ;
}
void solve2(ll l,ll r) {
if(l==r) {
if(a[l]==1) g[l]=(g[l]+g[l-1])%mo;
return ;
}
ll mid=l+r>>1;
solve2(l,mid);
lmx[mid]=a[mid];
for(ll j=mid-1;j>=l;j--) lmx[j]=max(lmx[j+1],a[j]);
rmx[mid+1]=a[mid+1];
for(ll j=mid+2;j<=r;j++) rmx[j]=max(rmx[j-1],a[j]);
for(ll j=l;j<=mid;j++) b[j]=(node){j,lmx[j]+j-1,j};
for(ll j=mid+1;j<=r;j++) b[j]=(node){j-rmx[j]+1,j,j};
sort(b+l,b+r+1,cmp);
for(ll j=l;j<=r;j++) {
if(b[j].id>mid) g[b[j].id]=(g[b[j].id]+T.ask(b[j].y))%mo;
else T.add(b[j].y,g[b[j].id-1]);
}
for(ll j=l;j<=r;j++) if(b[j].id<=mid) T.add(b[j].y,mo-g[b[j].id-1]);
solve2(mid+1,r);
return ;
}
ll lid[maxn], rid[maxn], rmx2[maxn], lmx2[maxn], sum[maxn];
struct node2 {
ll v;
ll id;
ll to;
ll k;
};
vector<node2> op[maxn];
void solve3(ll l,ll r) {
if(l==r) {
ans[l]=(ans[l]+f[l-1]*g[l+1]%mo)%mo;
cout<<ans[l]<<' ';
return ;
}
ll mid=l+r>>1;
lmx[mid]=a[mid], lid[mid]=mid;
for(ll j=l;j<=r;j++) lmx2[j]=rmx2[j]=0;
for(ll j=mid-1;j>=l;j--) {
if(lmx[j+1]>a[j]) {
lid[j]=lid[j+1];
lmx[j]=lmx[j+1];
lmx2[j]=max(lmx2[j+1],a[j]);
} else lid[j]=j, lmx[j]=a[j], lmx2[j]=lmx[j+1];
}
rmx[mid+1]=a[mid+1], rid[mid+1]=mid+1;
for(ll j=mid+2;j<=r;j++) {
if(rmx[j-1]>a[j]) {
rid[j]=rid[j-1];
rmx[j]=rmx[j-1];
rmx2[j]=max(rmx2[j-1],a[j]);
}else rid[j]=j, rmx[j]=a[j], rmx2[j]=rmx[j-1];
}
for(ll j=l;j<=mid;j++) {
op[j].push_back((node2){lmx[j]+j-1,j,0,1});
op[j].push_back((node2){lmx[j]+j-1,j,lid[j],mo-1});
op[j].push_back((node2){max(lmx2[j],1ll)+j-1,j,lid[j],1});
}
for(ll j=mid+1;j<=r;j++) {
if(r-rmx[j]+1<l) continue;
op[min(r-rmx[j]+1,mid)].push_back((node2){j,j,-1});
}
for(ll j=mid;j>=l;j--) {
for(auto p:op[j]) if(p.to==-1) T.add2(p.v,g[p.id+1]);
for(auto p:op[j]) {
if(p.to==-1) continue;
if(p.to==0) sum[j]=(sum[j]+T.ask2(p.v)*f[p.id-1]%mo)%mo;
else ans[p.to]=(ans[p.to]+p.k*T.ask2(p.v)%mo*f[p.id-1]%mo)%mo;
}
}
ans[l]=(ans[l]+sum[l])%mo;
for(ll j=l+1;j<=mid;j++) sum[j]=(sum[j]+sum[j-1])%mo, ans[j]=(ans[j]+sum[j])%mo;
for(ll j=l;j<=mid;j++) {
for(auto p:op[j]) if(p.to==-1) T.add2(p.v,mo-g[p.id+1]);
sum[j]=0, op[j].clear();
}
for(ll j=mid+1;j<=r;j++) {
op[j].push_back((node2){r-rmx[j]+1,j,0,1});
op[j].push_back((node2){r-rmx[j]+1,j,rid[j],mo-1});
op[j].push_back((node2){r-max(rmx2[j],1ll)+1,j,rid[j],1});
}
for(ll j=l;j<=mid;j++) {
if(lmx[j]+j-1>r) continue;
op[max(mid+1,lmx[j]+j-1)].push_back((node2){j,j,-1});
}
for(ll j=mid+1;j<=r;j++) {
for(auto p:op[j]) if(p.to==-1) T.add(p.v,f[p.id-1]);
for(auto p:op[j]) {
if(p.to==-1) continue;
if(p.to==0) sum[j]=(sum[j]+T.ask(p.v)*g[p.id+1]%mo)%mo;
else ans[p.to]=(ans[p.to]+p.k*T.ask(p.v)%mo*g[p.id+1]%mo)%mo;
}
}
ans[r]=(ans[r]+sum[r])%mo;
for(ll j=r-1;j>mid;j--) sum[j]=(sum[j]+sum[j+1])%mo, ans[j]=(ans[j]+sum[j])%mo;
for(ll j=r;j>mid;j--) {
for(auto p:op[j]) if(p.to==-1) T.add(p.v,mo-f[p.id-1]);
sum[j]=0, op[j].clear();
}
solve3(l,mid), solve3(mid+1,r);
return ;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
cin>>n;
for(ll i=1;i<=n;i++) cin>>a[i];
f[0]=g[0]=1;
solve1(1,n), reverse(a+1,a+n+1);
solve2(1,n), reverse(a+1,a+n+1), reverse(g,g+n+2);
solve3(1,n);
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 3ms
memory: 34752kb
input:
5 1 3 2 1 2
output:
3 6 3 3 6
result:
ok 5 tokens
Test #2:
score: -100
Wrong Answer
time: 864ms
memory: 110912kb
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 ...
result:
wrong answer 20704th words differ - expected: '526449908', found: '276682868'