QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#333386#7899. Say Hello to the Futurec20230201TL 968ms132304kbC++145.3kb2024-02-19 21:15:542024-02-19 21:15:55

Judging History

你现在查看的是最新测评结果

  • [2024-02-19 21:15:55]
  • 评测
  • 测评结果:TL
  • 用时:968ms
  • 内存:132304kb
  • [2024-02-19 21:15:54]
  • 提交

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;

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 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 ;
}

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 ;
    }

    for(ll j=l;j<=r;j++) lmx2[j]=rmx2[j]=0;

    ll mid=l+r>>1;
    lmx[mid]=a[mid], lid[mid]=mid;
    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(j-rmx[j]+1<l) continue;
        op[min(j-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){j-rmx[j]+1,j,0,1});
        op[j].push_back((node2){j-rmx[j]+1,j,rid[j],mo-1});
        op[j].push_back((node2){j-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;
}

详细

Test #1:

score: 100
Accepted
time: 5ms
memory: 21216kb

input:

5
1 3 2 1 2

output:

3 6 3 3 6 

result:

ok 5 tokens

Test #2:

score: 0
Accepted
time: 892ms
memory: 90820kb

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: 898ms
memory: 91004kb

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: 923ms
memory: 110304kb

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: 935ms
memory: 108412kb

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: 968ms
memory: 132120kb

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: 950ms
memory: 132304kb

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...

output:

529842888 529842888 529842888 529842888 529842888 529842888 529842888 529842888 529842888 529842888 773664190 529842888 529842888 529842888 529842888 529842888 529842888 529842888 529842888 529842888 529842888 529842888 529842888 529842888 529842888 529842888 529842888 529842888 529842888 529842888 ...

result: