QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#851481#9865. DollsGotoHiotoriWA 0ms34696kbC++142.4kb2025-01-10 19:23:112025-01-10 19:23:12

Judging History

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

  • [2025-01-10 19:23:12]
  • 评测
  • 测评结果:WA
  • 用时:0ms
  • 内存:34696kb
  • [2025-01-10 19:23:11]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
namespace soyo{
int st_max[20][1000001],st_min[20][1000001],lg[1000001];
int Max(int l,int r){int k=lg[r-l+1];return max(st_max[k][l],st_max[k][r-(1<<k)+1]);}
int Min(int l,int r){int k=lg[r-l+1];return min(st_min[k][l],st_min[k][r-(1<<k)+1]);}
int stk[1000001],top;
int suf1[1000001],suf2[1000001];
int a[1000001];
int f[20][1000001];
void main(){
    //freopen("doll.in","r",stdin),freopen("doll.out","w",stdout);
    int n,q;
    scanf("%d%d",&n,&q);
    for(int i=1;i<=n;++i)scanf("%d",a+i),st_max[0][i]=st_min[0][i]=a[i];
    int top=0;
    for(int i=n;i>=1;--i){
        for(;top&&a[stk[top-1]]<a[i];--top);
        suf1[i]=top?stk[top-1]:n+1;
        stk[top++]=i;
    }
    top=0;
    for(int i=n;i>=1;--i){
        for(;top&&a[stk[top-1]]>a[i];--top);
        suf2[i]=top?stk[top-1]:n+1;
        stk[top++]=i;
    }
    for(int i=2;i<=n;++i)lg[i]=lg[i>>1]+1;
    for(int i=1;i<=lg[n];++i)
        for(int j=1;j+(1<<i-1)<=n;++j)
            st_max[i][j]=max(st_max[i-1][j],st_max[i-1][j+(1<<i-1)]),
            st_min[i][j]=min(st_min[i-1][j],st_min[i-1][j+(1<<i-1)]);
    f[0][n]=n+1;
    for(int i=n-1;i>=1;--i)
        if(a[i+1]<a[i]){
            if(suf1[i]<f[0][i+1]){
                int l=suf1[i],r=f[0][i+1];
                for(;l<r;){
                    int mid=l+r>>1;
                    if(Min(suf1[i],mid)>a[i])l=mid+1;
                    else r=mid;
                }
                if(l<f[0][i+1]&&a[l]<Min(i,l-1))f[0][i]=f[0][i+1];
                else f[0][i]=l;
            }else f[0][i]=f[0][i+1];
        }else{
            if(suf2[i]<f[0][i+1]){
                int l=suf2[i],r=f[0][i+1];
                for(;l<r;){
                    int mid=l+r>>1;
                    if(Max(suf2[i],mid)<a[i])l=mid+1;
                    else r=mid;
                }
                if(l<f[0][i+1]&&a[l]>Max(i,l-1))f[0][i]=f[0][i+1];
                else f[0][i]=l;
            }else f[0][i]=f[0][i+1];
        }
    for(int i=1;i<=lg[n];++i)
        for(int j=1;j<=n;++j)
            if(f[i-1][j]>n)f[i][j]=n+1;
            else f[i][j]=f[i-1][f[i-1][j]];
    for(int l,r;q--;){
        scanf("%d%d",&l,&r);
        int res=r-l;
        for(int i=lg[n];i--;)
            if(f[i][l]<=r)
                l=f[i][l],res-=(1<<i);
        printf("%d\n",res);
    }
}
}
int main(){
    soyo::main();
    return 0;
}

详细

Test #1:

score: 0
Wrong Answer
time: 0ms
memory: 34696kb

input:

8
4
2 1 4 3
4
1 4 2 3
4
3 1 4 2
5
1 3 5 2 4
5
1 4 2 5 3
5
2 5 3 1 4
6
1 3 6 5 2 4
6
2 5 1 3 6 4

output:

1
-2
-2
-4

result:

wrong answer 1st numbers differ - expected: '3', found: '1'