QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#411023 | #7899. Say Hello to the Future | lgvc | TL | 621ms | 10120kb | C++14 | 4.9kb | 2024-05-14 20:15:20 | 2024-05-14 20:15:21 |
Judging History
answer
#include <bits/stdc++.h>
#define MOD 998244353
int N,mr,a[200009],dp[200009],s[200009],s2[200009],ix[200009],aq[200009],
tp[200009],su[200009];
void sv(int l,int r) {
if(l==r) return;
int md=(l+r)>>1;
sv(l,md);
int at=0,aq=0;
for(int i=md+1;i<=r;i++) {
if(a[i]>at) {
aq=at;
at=a[i];
} else aq=std::max(aq,a[i]);
s[i]=at;
s2[i]=aq;
}
at=0;aq=0;
for(int i=md;i>=l;i--) {
if(a[i]>at) {
aq=at;
at=a[i];
} else aq=std::max(aq,a[i]);
s[i]=at;
s2[i]=aq;
}
at=0;
if(l) su[l-1]=0;
su[l]=dp[l];
for(int k=l+1;k<=md;k++) su[k]=(su[k-1]+dp[k])%MOD;
for(int k=md+1;k<=r;k++) su[k]=0;
for(int i=md;i>l;i--) {
at=std::max(at,a[i]);
if(at<a[md+1]) continue;
int ll=md+1,rr=r;
while(ll<rr) {
mr=(ll+rr+1)>>1;
if(s[mr]>at) rr=mr-1;else ll=mr;
}
int lx=std::max(at+i-1,md+1),rx=ll;
if(lx>rx) continue;
su[lx]=(su[lx]+dp[i-1])%MOD;
if(rx!=r) su[rx+1]=(su[rx+1]+MOD-dp[i-1])%MOD;
}
for(int i=md+2;i<=r;i++) su[i]=(su[i]+su[i-1])%MOD;
for(int i=md+1;i<=r;i++) dp[i]=(dp[i]+su[i])%MOD;
at=0;s[md+1]=0;
for(int i=md+1;i<=r;i++) {
at=std::max(at,a[i]);
int ll=l+1,rr=md+1;
while(ll<rr) {
mr=(ll+rr)>>1;
if(s[mr]>=at) ll=mr+1;else rr=mr;
}
int lx=ll,rx=std::min(i-at+1,md+1);
if(lx>rx) continue;
dp[i]=(dp[i]+su[rx-1])%MOD;
if(lx>1) dp[i]=(dp[i]+MOD-su[lx-2])%MOD;
// for(int k=lx;k<=rx;k++) dp[i]=(dp[i]+dp[k-1])%MOD;
}
sv(md+1,r);
}
void cl(int l,int r) {
if(l==r) return;
int md=(l+r)/2;
cl(l,md);cl(md+1,r);
int at=0,aqq=0,id;
for(int i=md+1;i<=r;i++) {
if(a[i]>at) {
aqq=at;
at=a[i];id=i;
} else aqq=std::max(aqq,a[i]);
s[i]=at;
ix[i]=id;
s2[i]=aqq;
}
at=0;aqq=0;
for(int i=md;i>=l;i--) {
if(a[i]>=at) {
aqq=at;
at=a[i];
id=i;
} else aqq=std::max(aqq,a[i]);
s[i]=at;
ix[i]=id;
s2[i]=aqq;
}
for(int i=md+2;i<=r;i++) {
if(s[md]>=s2[i]) continue;
int x=s2[i];
int ll=l,rr=md;
while(ll<rr) {
mr=(ll+rr)/2;
if(s[mr]>=x) ll=mr+1;else rr=mr;
}
ll=std::max(ll,i-s[i]+2);
rr=md;
rr=std::min(rr,i-s2[i]+1);
for(int j=ll;j<=rr;j++) {
aq[ix[i]]=(aq[ix[i]]+1ll*dp[N-i]*tp[j-1])%MOD;
}
}
for(int i=md-1;i>=l;i--) {
if(s[md+1]>s2[i]) continue;
int x=s2[i];
int ll=md+1,rr=r;
while(ll<rr) {
mr=(ll+rr+1)/2;
if(s[mr]>x) rr=mr-1;else ll=mr;
}
ll=md+1;
ll=std::max(ll,i+s2[i]-1);
rr=std::min(rr,i+s[i]-2);
for(int j=ll;j<=rr;j++) {
aq[ix[i]]=(aq[ix[i]]+1ll*tp[i-1]*dp[N-j])%MOD;
}
}
for(int i=md+1;i<=r;i++) {
if(s[l]<s[i]) continue;
int ll=l,rr=md,mr;
while(ll<rr) {
mr=(ll+rr+1)/2;
if(s[mr]>=s[i]) {
ll=mr;
} else {
rr=mr-1;
}
}
int tpp=ll;
ll=l,rr=md;
while(ll<rr) {
mr=(ll+rr)/2;
if(s2[mr]>=s[i]) {
ll=mr+1;
} else {
rr=mr;
}
}
std::swap(tpp,rr);
rr=std::min(rr,i-s[i]+1);
if(tpp>rr) continue;
ll=tpp;
for(int j=ll;j<=rr;j++) {
if(s[j]>i-j+1) {
aq[ix[j]]=(aq[ix[j]]+1ll*tp[j-1]*dp[N-i])%MOD;
}
}
}
for(int i=md;i>=l;i--) {
if(s[i]>=s[r]) continue;
int ll=md+1,rr=r;
while(ll<rr) {
mr=(ll+rr)/2;
if(s[i]<s[mr]) rr=mr;else ll=mr+1;
}
int tpp=ll;
ll=md+1,rr=r;
while(ll<rr) {
mr=(ll+rr+1)/2;
if(s[i]<s2[mr]) rr=mr-1;else ll=mr;
}
tpp=std::max(tpp,i+s[i]-1);
if(tpp>rr) continue;
ll=tpp;
for(int j=ll;j<=rr;j++) {
if(s[j]>j-i+1) {
aq[ix[j]]=(aq[ix[j]]+1ll*tp[i-1]*dp[N-j])%MOD;
}
}
}
}
signed main(void) {
scanf("%d",&N);
for(int i=1;i<=N;i++) {
scanf("%d",&a[i]);
}
dp[0]=1;
sv(0,N);
std::reverse(a+1,a+N+1);
for(int i=0;i<=N;i++) tp[i]=dp[i];
memset(dp,0,sizeof(dp));dp[0]=1;
sv(0,N);
std::reverse(a+1,a+N+1);
cl(0,N);
for(int i=1;i<=N;i++) {
printf("%lld ",(tp[N]+1ll*aq[i]+1ll*(a[i]==1?0:1)*tp[i-1]*dp[N-i])%MOD);
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 8616kb
input:
5 1 3 2 1 2
output:
3 6 3 3 6
result:
ok 5 tokens
Test #2:
score: 0
Accepted
time: 269ms
memory: 10104kb
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:
ok 200000 tokens
Test #3:
score: 0
Accepted
time: 247ms
memory: 10120kb
input:
200000 4065 9196 2588 18803 10818 17671 10483 13134 12147 19916 19382 19338 8864 7637 19542 14938 16362 7115 9159 9711 17907 17653 10175 3279 7471 3465 14016 13951 8676 2856 16709 5372 12237 18083 11052 16398 7140 9730 8800 18999 16808 8729 7608 6668 7049 6024 7892 18039 7278 12417 2440 13112 4039 5...
output:
47583147 370164779 370164779 370164779 370164779 370164779 370164779 370164779 370164779 345009231 370164779 370164779 370164779 370164779 370164779 370164779 370164779 370164779 370164779 370164779 370164779 370164779 370164779 370164779 370164779 370164779 370164779 370164779 370164779 370164779 3...
result:
ok 200000 tokens
Test #4:
score: 0
Accepted
time: 330ms
memory: 9936kb
input:
200000 1 1 1 1 4547 1 1 1 1 1 1 1 6329 1 1 19778 1 1 12258 1 1 1 1 1 18104 1 8123 1 329 1 1 1 1 1 1 1 1 4438 1 1 1 1 1 208 1 1 1 1 1 1 1 1 1 15603 1 1 1 1 1 1 1 1 1 1 5513 1 1 15427 1 1 1 1 1 1 1 18309 1 1 6333 1 1 1 1 1 1 1 1 1 13938 1 1 1 1 1 1 9229 1 1 1 1 1 1 1 1 1791 1 1 1 11747 1 1 1 1 8992 1 ...
output:
225508548 225508548 225508548 225508548 748768610 225508548 225508548 225508548 225508548 225508548 225508548 225508548 225508548 225508548 225508548 225508548 225508548 225508548 225508548 225508548 225508548 225508548 225508548 225508548 225508548 225508548 225508548 225508548 225508548 225508548 ...
result:
ok 200000 tokens
Test #5:
score: 0
Accepted
time: 299ms
memory: 10056kb
input:
200000 1 1 1 1 1 1 1 10166 1 1 9410 1 1 1 1 1 1 1 1 1 296 1 1 1 1 1 1 1 1 1 1 7392 4057 17616 1 1 1 1 3084 14799 1 1 13598 1 1 9848 1 1 1 1 1 8084 1 1 1 1 1 1 1 1 10519 1 1 1 1 1 14 1 1 1 1 1 1 1 1 1 1 14981 1 1 1 1 1 1 1 1 1 5144 7784 1 1 1 1 1 1 1 19661 1 14894 1 1 1 1 1 1 1 1 10449 1 1 1 16473 1 ...
output:
735167914 735167914 735167914 735167914 735167914 735167914 735167914 211149010 735167914 735167914 735167914 735167914 735167914 735167914 735167914 735167914 735167914 735167914 735167914 735167914 735167914 735167914 735167914 735167914 735167914 735167914 735167914 735167914 735167914 735167914 ...
result:
ok 200000 tokens
Test #6:
score: 0
Accepted
time: 621ms
memory: 10068kb
input:
200000 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 5 1 6 1 1 1 1 1 1 1 1 1 1 1 2 1 1 1 1 1 1 1 1 3 1 1 1 1 1 1 1 3 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 6 1 1 1 1 1 1 1 1 6 1 1 1 2397 1 1 3 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 4 1 1 1 1 1 1 1 1 1 1 1 1 1 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ...
output:
450552969 450552969 450552969 450552969 450552969 450552969 450552969 450552969 450552969 450552969 450552969 450552969 450552969 450552969 450552969 450552969 415221870 450552969 425969980 450552969 450552969 450552969 450552969 450552969 450552969 450552969 450552969 450552969 450552969 450552969 ...
result:
ok 200000 tokens
Test #7:
score: 0
Accepted
time: 571ms
memory: 10036kb
input:
200000 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 9861 1 1 1 1 1 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1739 1 1 1 1 1 1 1 1 1 1 1 1 1 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 1 1 1 1 1 1 1 1 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 11012 1 1 1 1 1 1 1 1 1 2 1 1 1 1 1 1...
output:
723674232 723674232 723674232 723674232 723674232 723674232 723674232 723674232 723674232 723674232 723674232 723674232 723674232 723674232 723674232 723674232 723674232 723674232 723674232 723674232 723674232 723674232 723674232 76256586 723674232 723674232 723674232 723674232 723674232 723674232 7...
result:
ok 200000 tokens
Test #8:
score: -100
Time Limit Exceeded
input:
200000 1 1 1 1 1 1 1 1 1 1 29 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 5 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 12 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 5 1 1 1 1 1 1 1 1 1 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 1 1 1 1 1 1 1 1 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 8 1 1 1...