QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#229561 | #5143. Quick Sort | whsyhyyh# | WA | 27ms | 7932kb | C++14 | 1.9kb | 2023-10-28 16:26:16 | 2023-10-28 16:26:17 |
Judging History
answer
#include<bits/stdc++.h>
#define N 500010
#define rep(i,l,r) for(int i=l;i<=r;i++)
using namespace std;
int rd() {
int res=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9') {if(ch=='-') f*=-1;ch=getchar();}
while(ch>='0'&&ch<='9') res=(res<<1)+(res<<3)+(ch^48),ch=getchar();
return res*f;
}
int Tim,n,a[N];
struct Seg {
int mn[N<<2],mx[N<<2];
void update(int p) {
mn[p]=min(mn[p<<1],mn[p<<1|1]);
mx[p]=max(mx[p<<1],mx[p<<1|1]);
}
void build(int p,int l,int r) {
if(l==r) {mn[p]=mx[p]=a[l];return ;}
int mid=l+r>>1;
build(p<<1,l,mid),build(p<<1|1,mid+1,r);
update(p);
}
void modify(int p,int l,int r,int x) {
if(l==r) {mn[p]=mx[p]=a[x];return ;}
int mid=l+r>>1;
if(x<=mid) modify(p<<1,l,mid,x);
else modify(p<<1|1,mid+1,r,x);
update(p);
}
int findl(int p,int l,int r,int L,int R,int v) {
if(mx[p]<v) return -1;
if(l==r) return l;
int mid=l+r>>1;
if(R<=mid) return findl(p<<1,l,mid,L,R,v);
if(L>mid) return findl(p<<1|1,mid+1,r,L,R,v);
int tmp=findl(p<<1,l,mid,L,R,v);
if(tmp!=-1) return tmp;
return findl(p<<1|1,mid+1,r,L,R,v);
}
int findr(int p,int l,int r,int L,int R,int v) {
if(mn[p]>v) return -1;
if(l==r) return l;
int mid=l+r>>1;
if(R<=mid) return findr(p<<1,l,mid,L,R,v);
if(L>mid) return findr(p<<1|1,mid+1,r,L,R,v);
int tmp=findr(p<<1|1,mid+1,r,L,R,v);
if(tmp!=-1) return tmp;
return findr(p<<1,l,mid,L,R,v);
}
}T;
int ans;
void solve(int l,int r) {
if(l==r) return ;
int p=l+r>>1;
int L=l,R=r,A,B;
while(L!=p||R!=p) {
A=L!=p?T.findl(1,1,n,L,p,a[p]):p;
B=R!=p?T.findr(1,1,n,p,R,a[p]):p;
L=A,R=B;
if(L==R) break;
if(p==L) p=R;
else if(p==R) p=L;
swap(a[L],a[R]),T.modify(1,1,n,L),T.modify(1,1,n,R),ans++;
}
solve(l,p);
if(p<r) solve(p+1,r);
}
int main() {
Tim=rd();
while(Tim--) {
n=rd();
rep(i,1,n) a[i]=rd();
T.build(1,1,n);
ans=0,solve(1,n);
printf("%d\n",ans);
}
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 1ms
memory: 7932kb
input:
3 3 3 2 1 5 2 4 5 3 1 10 7 2 4 6 1 9 10 8 5 3
output:
1 4 7
result:
ok 3 number(s): "1 4 7"
Test #2:
score: 0
Accepted
time: 17ms
memory: 7916kb
input:
100000 4 3 4 2 1 5 5 4 1 3 2 4 3 1 4 2 4 4 2 1 3 4 1 3 2 4 4 4 2 3 1 4 3 2 1 4 5 1 2 3 4 5 5 5 2 3 1 4 5 1 3 5 4 2 5 4 3 2 1 5 5 3 4 2 1 5 5 5 4 3 2 1 4 3 4 2 1 5 4 2 5 1 3 5 4 1 5 2 3 4 3 4 1 2 4 2 1 3 4 5 4 3 2 5 1 4 4 2 1 3 5 3 1 5 2 4 4 4 1 2 3 5 1 5 2 4 3 5 4 1 3 5 2 5 4 2 3 5 1 5 1 2 3 4 5 5 4...
output:
3 4 3 2 1 1 1 0 2 2 2 3 2 3 2 3 2 1 3 2 4 3 2 3 2 0 4 2 4 1 3 2 3 2 2 1 3 1 1 2 4 3 2 3 1 1 1 3 3 3 4 4 2 3 3 2 3 3 3 2 1 1 2 3 1 3 1 1 3 4 2 2 4 1 1 3 2 2 2 2 1 3 4 4 3 4 2 2 1 3 2 3 2 3 3 2 1 4 2 3 4 1 2 2 3 2 2 3 2 2 2 2 4 1 2 3 2 2 2 2 3 2 3 4 3 2 3 4 2 4 1 3 2 3 4 3 3 4 1 2 4 3 2 4 2 3 3 1 2 2 ...
result:
ok 100000 numbers
Test #3:
score: -100
Wrong Answer
time: 27ms
memory: 7852kb
input:
50000 10 3 1 2 10 6 8 5 4 7 9 10 8 3 9 2 10 4 5 1 7 6 9 6 8 4 9 5 7 1 3 2 9 6 7 9 3 8 5 2 1 4 10 7 10 1 2 6 5 3 9 4 8 10 1 10 4 3 2 9 7 8 5 6 9 1 5 3 4 9 6 7 2 8 10 4 7 2 8 3 6 9 5 10 1 9 6 4 9 1 8 5 2 3 7 10 5 1 7 8 10 3 9 6 2 4 9 4 8 6 3 9 7 5 2 1 9 9 1 7 6 2 3 8 5 4 10 5 7 2 1 4 3 6 8 9 10 10 9 7...
output:
8 8 8 8 9 5 3 8 8 9 7 8 7 9 8 11 6 8 8 7 7 11 7 6 11 8 3 6 8 6 6 7 10 4 8 6 6 8 5 6 7 6 7 5 5 8 9 5 9 10 6 7 8 9 4 9 6 7 8 8 9 7 5 7 6 6 10 9 8 6 4 8 6 10 4 9 9 6 9 6 7 7 7 9 6 6 9 8 7 9 9 5 6 8 8 8 8 8 6 4 8 8 5 8 8 8 9 8 8 7 8 7 6 10 9 5 7 7 6 5 5 6 8 9 5 6 7 8 6 7 9 6 7 9 7 7 6 8 9 9 7 7 9 7 8 6 ...
result:
wrong answer 11th numbers differ - expected: '9', found: '7'