QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#198764#5143. Quick SortbigJTL 3821ms27528kbC++205.4kb2023-10-03 17:08:062023-10-03 17:08:06

Judging History

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

  • [2023-10-03 17:08:06]
  • 评测
  • 测评结果:TL
  • 用时:3821ms
  • 内存:27528kb
  • [2023-10-03 17:08:06]
  • 提交

answer

#include <bits/stdc++.h>

template<typename T, std::size_t N> std::istream& operator>>(std::istream& is, std::array<T, N>& v) { for (auto& i : v) is >> i; return is; }
template<typename T, std::size_t N> std::ostream& operator<<(std::ostream& os, std::array<T, N>& v) { for (auto& i : v) os << i << ' '; return os; }
template<typename T> std::istream& operator>>(std::istream& is, std::vector<T>& v) { for (auto& i : v) is >> i; return is; }
template<typename T> std::ostream& operator<<(std::ostream& os, std::vector<T>& v) { for (auto& i : v) os << i << ' '; return os; }
template<typename...Args> void debug(Args...args) { ((std::cerr << args << ' '), ...); std::cerr << '\n'; }
template<typename...Args> void println(Args...args) { ((std::cout << args << ' '), ...); std::cout << '\n'; }

using i64 = long long;

template <class Info>
struct SegmentTree {
#define m (l + r) / 2
    int n;
    std::vector<Info> tr;
    SegmentTree() : n(0) {}
    SegmentTree(int n_, Info v_ = Info()) {
        init(n_, v_);
    }
    template<class T>
    SegmentTree(std::vector<T> init_) {
        init(init_);
    }
    void init(int n_, Info v_ = Info()) {
        init(std::vector(n_, v_));
    }
    template<class T>
    void init(std::vector<T> init_) {
        n = init_.size();
        tr.assign(4 << std::__lg(n), Info());
        std::function<void(int, int, int)> build = [&](int p, int l, int r) {
            if (r - l == 1) {
                tr[p] = { init_[l] };
                return;
            }
            build(2 * p, l, m);
            build(2 * p + 1, m, r);
            pull(p);
        };
        build(1, 0, n);
    }

    void pull(int p) {
        tr[p] = tr[2 * p] + tr[2 * p + 1];
    }

    void modify(int p, int l, int r, int x, const Info& v) {
        if (r - l == 1) {
            tr[p] = v;
            return;
        }
        if (x < m) {
            modify(2 * p, l, m, x, v);
        }
        else {
            modify(2 * p + 1, m, r, x, v);
        }
        pull(p);
    }
    void modify(int x, const Info& v) {
        modify(1, 0, n, x, v);
    }

    Info rangeQuery(int p, int l, int r, int x, int y) {
        if (l >= y || r <= x) {
            return Info();
        }
        if (l >= x && r <= y) {
            return tr[p];
        }
        return rangeQuery(2 * p, l, m, x, y) + rangeQuery(2 * p + 1, m, r, x, y);
    }
    Info rangeQuery(int l, int r) {
        return rangeQuery(1, 0, n, l, r);
    }

    int findFirstUpper(int p, int l, int r, int x, int y, int v) {
        if (r - l == 1) {
            return l;
        }
        int mid = m - 1;
        if (x > mid) {
            return findFirstUpper(2 * p + 1, m, r, x, y, v);
        }
        else if (y < mid) {
            return findFirstUpper(2 * p, l, m, x, y, v);
        }
        if (rangeQuery(x, mid + 1).mx < v) {
            return findFirstUpper(2 * p + 1, m, r, mid, y, v);
        }
        return findFirstUpper(2 * p, l, m, x, mid + 1, v);
    }

    int findFirstLower(int p, int l, int r, int x, int y, int v) {
        if (r - l == 1) {
            return l;
        }
        int mid = m - 1;
        if (x > mid) {
            return findFirstLower(2 * p + 1, m, r, x, y, v);
        }
        else if (y < mid) {
            return findFirstLower(2 * p, l, m, x, y, v);
        }
        if (rangeQuery(x, mid + 1).mn > v) {
            return findFirstLower(2 * p + 1, m, r, mid, y, v);
        }
        return findFirstLower(2 * p, l, m, x, mid + 1, v);
    }

    int findFirstUpper(int l, int r, int v) {
        return findFirstUpper(1, 0, n, l, r, v);
    }
    int findFirstLower(int l, int r, int v) {
        return findFirstLower(1, 0, n, l, r, v);
    }
#undef m
};

struct Info {
    int mx, mn;
    Info(int x) :mx(x), mn(x) {}
    Info() :mx(-1e9), mn(1e9) {}
    Info(int mx, int mn) :mx(mx), mn(mn) {}
    friend Info operator+(const Info& a, const Info& b) {
        return Info(std::max(a.mx, b.mx), std::min(a.mn, b.mn));
    }
};

SegmentTree<Info> seg, seg1;

int res = 0;
int n;

int trans(int i) {
    return n + 1 - i;
}

int work(std::vector<int>& a, int l, int r) {
    int m = a[(l + r) / 2];
    int i = l - 1, j = r + 1;
    while (true) {
        i++, j--;

        std::tie(i, j) = std::pair(seg.findFirstUpper(i, j + 1, m), seg1.findFirstLower(trans(j), trans(i) + 1, m));
        j = trans(j);

        if (i >= j) {
            return j;
        }
        else {
            seg.modify(i, a[j]);
            seg.modify(j, a[i]);
            seg1.modify(trans(i), a[j]);
            seg1.modify(trans(j), a[i]);
            std::swap(a[i], a[j]);
            res++;
        }
    }
    return -1;
}

void quickSort(std::vector<int>& a, int l, int r) {
    if (l >= 0 && r >= 0 && l < r) {
        int m = work(a, l, r);
        quickSort(a, l, m);
        quickSort(a, m + 1, r);
    }
}

auto solve() {
    std::cin >> n;

    std::vector<int> a(n + 1), b(n + 1);
    for (int i = 1; i <= n; i++) {
        std::cin >> a[i];
    }
    for (int i = 1; i <= n; i++) {
        b[i] = a[trans(i)];
    }

    seg.init(a);
    seg1.init(b);

    quickSort(a, 1, n);
    std::cout << res << "\n";
}

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

    int c = 1;
    std::cin >> c;

    for (int i = 1; i <= c; i++) {
        res = 0;
        solve();
    }

    return 0;
}

詳細信息

Test #1:

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

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: 78ms
memory: 3892kb

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: 0
Accepted
time: 104ms
memory: 3572kb

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
9
8
7
9
8
9
6
8
8
7
7
11
7
6
9
8
3
6
8
8
6
7
8
4
8
6
6
8
5
6
7
6
7
5
5
6
9
5
9
10
6
7
8
9
4
9
6
7
8
8
9
9
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
11
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
7
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
7
...

result:

ok 50000 numbers

Test #4:

score: 0
Accepted
time: 410ms
memory: 3652kb

input:

5000
94
69 86 59 9 67 89 24 63 14 18 16 11 19 46 23 40 4 55 53 61 30 3 78 29 15 74 32 41 51 13 77 47 66 92 57 45 42 21 62 43 26 1 84 75 71 54 73 36 39 48 88 8 80 64 58 10 60 76 17 70 25 37 38 6 72 91 7 20 68 2 35 44 90 79 50 93 81 94 27 33 5 52 28 82 56 87 31 22 83 34 65 85 49 12
97
44 97 28 56 95 6...

output:

141
146
142
150
149
145
135
144
135
145
141
140
140
134
138
137
156
142
148
146
159
149
148
154
158
152
144
154
140
136
143
152
143
155
149
148
160
153
125
155
152
149
133
130
144
138
136
154
150
141
141
142
139
140
154
152
138
161
150
138
140
143
143
153
133
137
129
145
154
144
159
150
148
132
145
...

result:

ok 5000 numbers

Test #5:

score: 0
Accepted
time: 922ms
memory: 3688kb

input:

500
959
670 618 579 212 780 557 380 412 672 951 777 921 684 768 99 952 140 122 139 919 623 17 911 18 880 790 625 505 307 747 801 754 783 146 757 263 285 228 719 640 199 193 105 234 847 842 348 159 823 577 466 954 850 851 643 802 819 317 826 55 617 690 604 229 570 254 759 575 498 240 397 736 864 415 ...

output:

2204
2106
2121
2073
2255
2250
2181
2117
2156
2066
2217
2307
2248
2155
2272
2219
2213
2180
2379
2168
2319
2014
2193
2256
2111
2223
2044
2058
2172
2168
2350
2216
2079
2242
2087
2198
2318
2312
2294
2132
2073
2102
2267
2157
2058
2082
2244
2213
2185
2216
2029
2076
2204
2157
2028
2266
2330
2236
2232
2051
...

result:

ok 500 numbers

Test #6:

score: 0
Accepted
time: 1495ms
memory: 3924kb

input:

50
9597
2421 5801 7761 5556 4158 3033 4751 9284 3326 1858 2849 8472 5917 6077 4438 1948 5294 3028 4716 8042 2671 5305 5076 6924 5569 8173 6362 2160 3095 7385 1374 3167 8128 551 2363 1371 5799 3273 1366 5050 7680 198 5577 1236 2843 1127 5381 3029 6977 4823 702 8077 528 526 7027 4278 7947 6058 5005 90...

output:

29310
28096
27809
27929
30189
28312
30370
28918
28971
29145
30375
28996
29862
29780
30176
27658
30518
29051
28532
27876
28081
27765
29274
27475
28265
29613
28306
27747
29273
30631
29036
29645
28861
27797
29968
30333
28032
29242
30500
28291
30177
29925
29233
27507
28648
28030
27846
27893
28592
30041

result:

ok 50 numbers

Test #7:

score: 0
Accepted
time: 2349ms
memory: 8644kb

input:

5
92316
4486 51971 40435 31486 22840 51804 19355 35116 71427 50525 34461 46690 44101 15605 33166 25846 90319 50846 8819 36285 58519 23478 20717 14434 37378 37454 60063 17182 70164 59883 45000 84942 58799 11505 13371 52739 66680 30438 67677 41266 53940 34428 79533 55092 76616 54423 21642 25614 48002 ...

output:

351318
384237
375828
355759
369341

result:

ok 5 number(s): "351318 384237 375828 355759 369341"

Test #8:

score: 0
Accepted
time: 3048ms
memory: 25128kb

input:

1
471631
424496 112701 456051 347801 218724 312785 85999 325031 220919 219326 327801 239646 431816 121964 216653 223784 147176 29672 466026 412872 269415 238525 365823 442104 346534 297299 298496 242174 296754 297691 105566 80641 204310 21696 170588 199258 59123 336907 57422 387873 209433 272911 261...

output:

2073273

result:

ok 1 number(s): "2073273"

Test #9:

score: 0
Accepted
time: 1349ms
memory: 6044kb

input:

10
49043
3 62 7894 13204 32089 2725 16715 49017 3214 20600 35246 33149 18958 8682 30558 49023 43207 38952 34838 44882 30 15653 41343 6038 9041 670 41370 9137 15978 38363 48352 18170 6201 4242 48102 25665 44628 28845 22071 29581 48319 25193 11916 26009 13138 34355 22758 912 5471 35494 48963 44705 159...

output:

71626
72451
68777
71719
71151
73242
72260
70569
65059
66275

result:

ok 10 numbers

Test #10:

score: 0
Accepted
time: 2881ms
memory: 24852kb

input:

1
454463
224975 135113 385761 248982 414871 419495 29440 415241 230944 365106 17983 404591 315719 79807 95530 423985 25214 245278 286000 288449 93676 309142 318800 187355 292491 250725 361750 196767 230248 38684 411252 190800 290659 74661 271459 100218 34044 258477 424776 244258 13327 108635 419500 ...

output:

1950324

result:

ok 1 number(s): "1950324"

Test #11:

score: 0
Accepted
time: 2904ms
memory: 25072kb

input:

1
477555
464643 323142 58642 301685 465842 109123 160536 126115 319568 153387 310382 268933 30462 463340 454386 289077 337839 10749 315329 409965 395306 235946 293184 460738 284494 111278 335634 280731 68437 373241 171156 206880 97736 419944 444321 100223 95403 121409 458631 443764 37984 404758 4423...

output:

1887736

result:

ok 1 number(s): "1887736"

Test #12:

score: 0
Accepted
time: 2473ms
memory: 24960kb

input:

1
467119
77037 148833 393361 102694 273351 359766 144336 34262 234746 164404 353529 221766 335113 85514 418396 396952 147432 406949 66214 96470 366749 44253 364388 346829 103379 13028 157764 236234 276906 18955 188027 318489 376516 400456 10596 224553 218042 305417 414051 355400 419241 397664 148706...

output:

1411212

result:

ok 1 number(s): "1411212"

Test #13:

score: 0
Accepted
time: 2570ms
memory: 25540kb

input:

1
499061
130564 145811 448484 372031 79740 210229 86804 480281 9360 317022 491052 91746 62582 233895 483260 312126 473313 230696 131064 309187 488425 230167 178638 93839 308947 214227 279078 465524 234246 321738 46695 438621 400083 5664 190510 117927 127239 35111 236912 320792 431986 350632 481980 2...

output:

1391679

result:

ok 1 number(s): "1391679"

Test #14:

score: 0
Accepted
time: 2131ms
memory: 24828kb

input:

1
454838
42864 199798 180952 21880 78970 364397 188215 226462 175873 95071 255111 375034 89670 236248 443301 219625 73992 125504 351339 208867 22147 449531 129242 353019 428824 123584 333925 266422 40119 109364 165888 234954 22194 86348 423220 65437 79619 207599 256752 242548 139691 251065 241729 43...

output:

1015021

result:

ok 1 number(s): "1015021"

Test #15:

score: 0
Accepted
time: 2127ms
memory: 24944kb

input:

1
457705
166904 55413 374498 195851 233691 279824 5147 345044 13316 447297 413570 271247 264676 286756 435549 374105 371973 374942 330154 175972 344391 184306 194731 28222 433466 301864 405981 224999 418159 223073 288850 343599 60336 354267 32381 239361 306339 73478 292916 184484 385187 54852 89398 ...

output:

1032617

result:

ok 1 number(s): "1032617"

Test #16:

score: 0
Accepted
time: 2204ms
memory: 25416kb

input:

1
491856
9273 17142 457474 36637 14467 464140 296884 130576 52343 74140 431768 469179 249611 13922 157361 117953 321744 131186 83724 216582 315305 480513 372541 421863 330141 323558 246961 284650 27701 179496 478193 119928 382199 17119 297247 464477 13753 38762 330994 396129 491445 42033 111957 3069...

output:

1040041

result:

ok 1 number(s): "1040041"

Test #17:

score: 0
Accepted
time: 2196ms
memory: 25760kb

input:

1
498689
177821 141958 14813 59204 282892 132992 78486 235209 288524 149128 250232 468322 368474 43714 277610 247810 404739 127051 72082 264204 448473 458284 96740 88738 158424 169632 306459 341256 140145 408665 378097 343154 31060 343792 324569 209888 310977 375888 122258 201785 61042 135779 168058...

output:

998791

result:

ok 1 number(s): "998791"

Test #18:

score: 0
Accepted
time: 1950ms
memory: 25888kb

input:

1
484696
225179 198196 407691 153439 413714 235 74667 436653 389592 205082 311014 381881 350306 86329 470 244941 43018 294514 119536 289661 124084 455963 19038 425332 137760 251290 210178 294631 482957 158258 393402 474957 317052 458801 484215 206281 337387 186389 402713 10158 162123 210404 83255 31...

output:

796075

result:

ok 1 number(s): "796075"

Test #19:

score: 0
Accepted
time: 1935ms
memory: 26636kb

input:

1
496303
477481 167206 122422 416094 496290 479121 290861 323982 316949 212889 198534 496232 92178 306999 162322 234182 813 487630 482064 172516 227357 308401 408666 258793 799 38949 395998 36 83833 350768 385 315347 55690 59067 793 41 840 226595 235407 476575 237106 408860 226345 199575 382997 3243...

output:

726480

result:

ok 1 number(s): "726480"

Test #20:

score: 0
Accepted
time: 1851ms
memory: 26872kb

input:

1
492225
136113 32 459642 45 454451 14 250003 26165 16 1591 469741 492201 136266 44 42 454688 492176 342256 492179 451487 254984 487130 27 93718 134597 173063 492181 492189 263244 245219 75060 5794 82 48473 109767 492159 325162 126505 327650 492171 240526 291066 125188 47 50 492160 328835 194347 366...

output:

628477

result:

ok 1 number(s): "628477"

Test #21:

score: 0
Accepted
time: 1696ms
memory: 27528kb

input:

1
488776
10 332014 488768 6 79598 7 488773 488772 488771 488761 13 392996 488762 488763 287307 118689 488757 488759 251821 26 22 488750 360241 488751 37842 488745 488409 263123 361 402268 36 45820 35 27474 488741 37 39 146995 488414 37859 284 44 283 442091 50 488732 80448 488734 175726 56 52 53 4887...

output:

528623

result:

ok 1 number(s): "528623"

Test #22:

score: 0
Accepted
time: 3162ms
memory: 5884kb

input:

10
47692
31796 31797 31800 31798 31795 31799 31790 31792 31794 31793 31801 31791 31808 31810 31809 31802 31807 31803 31805 31804 31806 31774 31772 31789 31771 31773 31775 31770 31766 31765 31767 31769 31768 31784 31776 31786 31788 31785 31787 31783 31779 31777 31778 31780 31782 31781 31842 31811 318...

output:

329452
339133
315984
329186
321367
314931
313181
313411
310399
327240

result:

ok 10 numbers

Test #23:

score: 0
Accepted
time: 3821ms
memory: 8720kb

input:

5
91764
61177 61173 61176 61175 61174 61184 61178 61183 61182 61179 61180 61172 61181 61166 61167 61165 61164 61168 61162 61163 61170 61169 61185 61171 61197 61199 61198 61203 61206 61200 61205 61204 61201 61196 61202 61187 61188 61186 61192 61193 61189 61195 61194 61161 61191 61190 61129 61128 6113...

output:

678551
738167
707172
732397
720733

result:

ok 5 number(s): "678551 738167 707172 732397 720733"

Test #24:

score: -100
Time Limit Exceeded

input:

1
488063
325375 325377 325376 325374 325378 325372 325371 325379 325373 325384 325385 325383 325381 325382 325370 325380 325360 325361 325359 325358 325356 325357 325362 325368 325369 325365 325367 325363 325364 325366 325386 325404 325405 325403 325406 325402 325414 325415 325407 325413 325412 3254...

output:


result: