QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#333313 | #7899. Say Hello to the Future | c20230201 | TL | 3ms | 8316kb | C++14 | 5.7kb | 2024-02-19 20:02:18 | 2024-02-19 20:02:18 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll maxn=2e5+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];
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-2});
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});
}
ll sum=0;
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=(sum+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;
}
}
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]);
ans[j]=(ans[j]+sum)%mo;
}
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-2});
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});
}
sum=0;
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) continue;
if(p.to==0) sum=(sum+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;
}
}
for(ll j=mid+1;j<=r;j++) {
for(auto p:op[j]) if(p.to==-1) T.add(p.v,mo-f[p.id-1]);
ans[j]=(ans[j]+sum)%mo;
}
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);
// for(ll i=0;i<=n+1;i++) cout<<f[i]<<' '<<g[i]<<'\n';
// cout<<n<<'\n';
// for(int i=1;i<=n;i++) cout<<a[i]<<' '; cout<<'\n';
for(ll i=1;i<=n;i++) {
ll t=a[i], res=0; a[i]=1;
for(ll l=1;l<=i;l++) {
ll mx=0;
for(ll r=l;r<=n;r++) {
mx=max(mx,a[r]);
if(r<i) continue;
res=(res+ f[l-1]*g[r+1]*(mx<=r-l+1))%mo;
}
}
a[i]=t;
cout<<res<<' ';
}
cout<<'\n';
// solve3(1,n);
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 3ms
memory: 8316kb
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...