QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#138314#6303. InversionyzhangTL 0ms0kbC++141.8kb2023-08-11 14:09:542023-08-11 14:09:57

Judging History

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

  • [2023-08-11 14:09:57]
  • 评测
  • 测评结果:TL
  • 用时:0ms
  • 内存:0kb
  • [2023-08-11 14:09:54]
  • 提交

answer

//μ's forever
#include <bits/stdc++.h>
#define N 1005
//#define getchar nc
using namespace std;
inline char nc(){
    static char buf[100000],*p1=buf,*p2=buf;
    return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;
}
inline int read()
{
    register int x=0,f=1;register char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9')x=(x<<3)+(x<<1)+ch-'0',ch=getchar();
    return x*f;
}
inline void write(register int x)
{
    if(!x)putchar('0');if(x<0)x=-x,putchar('-');
    static int sta[20];register int tot=0;
    while(x)sta[tot++]=x%10,x/=10;
    while(tot)putchar(sta[--tot]+48);
}
int n;
int p[N],ip[N],f[N][N];
int qry1(int l,int r){
    if(l>=r) return 0;
    printf("? %d %d",l,r);
    fflush(stdout);
    int x=read();
    return x;
}
int qry2(int l,int r){
    return f[l][r];
}
int chk(int l,int r){
    return ((qry1(l,r)-qry2(l,r-1)-qry1(l+1,r)+qry2(l+1,r-1))%2+2)%2;
}
int tr[N];
void modify(int x){
    for(int i=x;i<=n;i+=i&(-i)) ++tr[i];
}
int qry(int x){
    int res=0;
    for(int i=x;i;i-=i&(-i)) res+=tr[i];
    return res;
}
void solve(int pos){
    int l=1,r=pos-1,res=pos;
    while(l<=r){
        int mid=l+r>>1;
        if(chk(ip[mid],pos)){
            res=mid,r=mid-1;
        }else{
            l=mid+1;
        }
    }
    p[pos]=res;
    for(int i=1;i<pos;++i) if(p[i]>=res) ++p[i];
    for(int i=1;i<=pos;++i) ip[p[i]]=i;
    memset(tr,0,sizeof(tr));
    for(int i=pos;i>=1;--i){
        f[i][pos]=f[i+1][pos]+qry(p[i]);
        modify(p[i]);
    }
}
int main()
{
    n=read();
    p[1]=1,ip[1]=1;
    for(int i=2;i<=n;++i){
        solve(i);
    }
    putchar('!');
    for(int i=1;i<=n;++i) printf(" %d",p[i]);
    fflush(stdout);
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Time Limit Exceeded

input:

3

output:

? 1 2

result: