QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#590090#5143. Quick SortSoestxWA 96ms5688kbC++232.2kb2024-09-25 21:29:462024-09-25 21:29:46

Judging History

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

  • [2024-09-25 21:29:46]
  • 评测
  • 测评结果:WA
  • 用时:96ms
  • 内存:5688kb
  • [2024-09-25 21:29:46]
  • 提交

answer

#include<bits/stdc++.h>
#pragma GCC optimize(1)
#pragma GCC optimize(2)
#pragma GCC optimize(3,"Ofast","inline")
using namespace std;

const int N = 5e5 + 10;
 
int n, a[N], res;
struct segtree
{
    int w1, w2;
}seg[N * 4];
 
void update(int id)
{
    seg[id].w1 = max(seg[id * 2].w1, seg[id * 2 + 1].w1);
    seg[id].w2 = min(seg[id * 2].w2, seg[id * 2 + 1].w2);
}
 
void build(int id, int l, int r)
{
    seg[id] = {0, 0};
    if(l == r)
    {
        seg[id] = {a[l], a[l]};
        return;
    }
    int mid = (l + r) >> 1;
    build(id * 2, l, mid);
    build(id * 2 + 1, mid + 1, r);
    update(id);
}
 
void change(int id, int l, int r, int pos)
{
    if(l == r)
    {
        seg[id] = {a[l], a[l]};
        return;
    }
    int mid = (l + r) >> 1;
    if(pos <= mid)
        change(id * 2, l, mid, pos);
    else
        change(id * 2 + 1, mid + 1, r, pos);
    update(id);
}
 
int query1(int id, int l, int r, int val)
{
    if(l == r)
        return l;
    int mid = (l + r) >> 1;
    if(seg[id * 2].w1 >= val)
        return query1(id * 2, l, mid, val);
    else
        return query1(id * 2 + 1, mid + 1, r, val);
}
 
int query2(int id, int l, int r, int val)
{
    if(l == r)
        return l;
    int mid = (l + r) >> 1;
    if(seg[id * 2 + 1].w2 <= val)
        return query2(id * 2 + 1, mid + 1, r, val);
    else
        return query2(id * 2, l, mid, val);
}
 
int par(int l, int r)
{
    int tl = l - 1, tr = r + 1;
    int val = a[(l + r) / 2];
    while(1)
    {
        int p1 = query1(1, 1, n, val);

        int p2 = query2(1, 1, n, val);

        if(p1 >= p2)
            return p2;
        swap(a[p1], a[p2]);
        change(1, 1, n, p1);        change(1, 1, n, p2);
        res++;
        tl = p1, tr = p2;
    }   
}
void quicksort(int l, int r)
{
    if(l >= r || l < 0 || r < 0)  return;
    int p = par(l, r);
    quicksort(l, p);
    quicksort(p + 1, r);
}
 
void solve()
{       
    cin>>n;
    for(int i = 1; i <= n; i++)
        cin>>a[i];
    res = 0;
    build(1, 1, n);
    quicksort(1, n);
    cout<<res<<'\n';
    return;
}

int main()
{
    int t;
    cin>>t;
    while(t--) solve();
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 5636kb

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: 96ms
memory: 5656kb

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: 72ms
memory: 5688kb

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'