QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#611956#5143. Quick SortHoochWA 48ms5724kbC++233.1kb2024-10-05 00:38:312024-10-05 00:38:32

Judging History

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

  • [2024-10-05 00:38:32]
  • 评测
  • 测评结果:WA
  • 用时:48ms
  • 内存:5724kb
  • [2024-10-05 00:38:31]
  • 提交

answer

#include <bits/stdc++.h>

using u32 = unsigned;
using i64 = long long;
using u64 = unsigned long long;

constexpr int N = 5E5 + 5;

int n, a[N];

struct Data {
    int min, max;
} ;
Data operator+(const Data &lhs, const Data &rhs) {
    return (Data) {
        std::min(lhs.min, rhs.min),
        std::max(lhs.max, rhs.max)
    } ;
}

struct SegmentTree {
    Data t[N * 4];

    void pull(int x) {
        t[x] = t[x * 2] + t[x * 2 + 1];
    }
    void build(int x, int l, int r) {
        if (l == r) return void(t[x] = {a[l], a[l]});
        int mid = (l + r) / 2;
        build(x * 2, l, mid);
        build(x * 2 + 1, mid + 1, r);
        pull(x);
    }
    void modify(int x, int l, int r, int pos, int val) {
        if (l == r) return void(t[x] = {val, val});
        int mid = (l + r) / 2;
        if (pos <= mid) modify(x * 2, l, mid, pos, val);
        else modify(x * 2 + 1, mid + 1, r, pos, val);
        pull(x);
    }
    void modify(int pos, int val) {modify(1, 1, n, pos, val);}
    int fft(int x, int l, int r, int lim) {
        if (l == r) return l;
        int mid = (l + r) / 2;
        if (t[x * 2].max >= lim) return fft(x * 2, l, mid, lim);
        return fft(x * 2 + 1, mid + 1, r, lim);
    }
    int flt(int x, int l, int r, int lim) {
        if (l == r) return l;
        int mid = (l + r) / 2;
        if (t[x * 2 + 1].min <= lim) return flt(x * 2 + 1, mid + 1, r, lim);
        return flt(x * 2, l, mid, lim);
    }
    int ff(int x, int l, int r, int L, int R, int lim) {
        if (r < L || l > R) return -1;
        if (l >= L && r <= R) {
            if (t[x].max >= lim) return fft(x, l, r, lim);
            else return -1;
        }
        int mid = (l + r) / 2, res = ff(x * 2, l, mid, L, R, lim);
        if (~res) return res;
        return ff(x * 2 + 1, mid + 1, r, L, R, lim);
    }
    int ff(int l, int r, int lim) {return ff(1, 1, n, l, r, lim);}
    int fl(int x, int l, int r, int L, int R, int lim) {
        if (r < L || l > R) return -1;
        if (l >= L && r <= R) {
            if (t[x].min <= lim) return flt(x, l, r, lim);
            else return -1;
        }
        int mid = (l + r) / 2, res = fl(x * 2 + 1, mid + 1, r, L, R, lim);
        if (~res) return res;
        return fl(x * 2, l, mid, L, R, lim);
    }
    int fl(int l, int r, int lim) {return fl(1, 1, n, l, r, lim);}
} seg;

int ans;
void solve(int l, int r) {
    if (l >= r) return ;
    int val = a[(l + r) / 2];
    while (true) {
        int lp = seg.ff(l, r, val);
        int rp = seg.fl(l, r, val);
        if (lp >= rp) {
            solve(l, rp);
            solve(rp + 1, r);
            break;
        }
        std::swap(a[lp], a[rp]);
        ++ans;
        seg.modify(lp, a[lp]);
        seg.modify(rp, a[rp]);
    }
}

void solve() {
    std::cin >> n;
    for (int i = 1; i <= n; ++i) std::cin >> a[i];
    ans = 0;
    seg.build(1, 1, n);
    solve(1, n);
    std::cout << ans << "\n";
}

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);

    int t;
    std::cin >> t;
    while (t--) solve();    

    return 0;
}

詳細信息

Test #1:

score: 100
Accepted
time: 1ms
memory: 5620kb

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: 33ms
memory: 5724kb

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: 48ms
memory: 5708kb

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'