Submission details
Task:Sort
Sender:Sebastian
Submission time:2026-04-17 10:41:23 +0300
Language:C++ (C++20)
Status:READY
Result:0
Feedback
subtaskverdictscore
#10
#20
#30
#40
#50
#60
#70
Test results
testverdicttimesubtask
#10.00 s2, 6, 7details
#20.42 s1, 2, 3, 6, 7details
#30.42 s2, 6, 7details
#40.42 s1, 2, 3, 6, 7details
#50.42 s1, 2, 3, 6, 7details
#60.42 s1, 2, 3, 6, 7details
#70.42 s1, 2, 3, 6, 7details
#80.42 s1, 2, 3, 6, 7details
#90.42 s1, 2, 3, 6, 7details
#100.41 s1, 2, 3, 6, 7details
#110.41 s2, 6, 7details
#120.42 s2, 6, 7details
#130.42 s2, 6, 7details
#140.42 s2, 6, 7details
#150.41 s2, 6, 7details
#160.77 s3, 7details
#17--3, 7details
#18--3, 7details
#19--3, 7details
#20--3, 7details
#21--3, 7details
#22--3, 7details
#23--3, 7details
#24--4, 7details
#25--4, 7details
#26--4, 7details
#27--4, 7details
#28--4, 7details
#29--4, 7details
#30--5, 6, 7details
#31--5, 6, 7details
#32--5, 6, 7details
#33--5, 6, 7details
#34--5, 6, 7details
#35--5, 6, 7details
#36--6, 7details
#37--6, 7details
#380.42 s6, 7details
#390.42 s6, 7details
#40--6, 7details
#410.42 s6, 7details
#42--7details
#43--7details
#44--7details
#450.48 s7details
#460.50 s7details
#470.51 s7details
#480.50 s7details
#49--7details
#500.47 s7details
#510.47 s7details
#520.00 s1, 2, 3, 5, 6, 7details
#530.00 s1, 2, 3, 4, 5, 6, 7details
#540.00 s2, 5, 6, 7details
#550.00 s1, 2, 3, 4, 6, 7details
#560.41 s1, 2, 3, 6, 7details
#570.42 s6, 7details
#580.07 s3, 4, 7details
#590.45 s3, 7details
#600.42 s2, 6, 7details
#610.41 s2, 6, 7details
#620.41 s2, 6, 7details
#630.41 s2, 6, 7details
#640.41 s2, 6, 7details

Code

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;
typedef __int128_t i128;
#define all(x) begin(x), end(x)
#define sz(x) ((int)(x).size())
const ll inf = 4e18;

struct event {
    ll t, l, r;
    ll *p;
    
    event(ll t, ll l, ll r, ll *p) : t(t), l(l), r(r), p(p) {}

    bool operator<(const event &o) const {
        return t < o.t;
    }
};

struct segtree {
    ll nt = 0;
    vector<ll> tree;

    segtree(ll n) {
        nt = 1;
        while (nt < n) nt *= 2;
        tree = vector<ll>(2*nt);
    }

    void point_set(ll i, ll v) { return point_set(1, 0, nt-1, i, v); }
    void point_set(ll k, ll tl, ll tr, ll i, ll v) {
        if (tl == tr) return tree[k] = v, void();
        ll mid = tl + (tr - tl) / 2;
        if (i <= mid) point_set(2*k, tl, mid, i, v);
        else point_set(2*k+1, mid+1, tr, i, v);
        tree[k] = tree[2*k] + tree[2*k+1];
    }

    ll range_sum(ll l, ll r) { return range_sum(1, 0, nt-1, l, r); }
    ll range_sum(ll k, ll tl, ll tr, ll l, ll r) {
        if (r < tl || l > tr) return 0;
        if (l <= tl && r >= tr) return tree[k];
        ll mid = tl + (tr - tl) / 2;
        return range_sum(2*k, tl, mid, l, r) + range_sum(2*k+1, mid+1, tr, l, r);
    }
};

void solve() {
    ll n, q; cin >> n >> q;
    vector<ll> aa(n);
    vector<pll> srt(n);
    for (ll i = 0; i < n; i++) {
        cin >> aa[i];
        srt[i] = pll(aa[i], i);
    }
    sort(all(srt));
    for (ll i = 0; i < n; i++) {
        aa[i] = lower_bound(all(srt), pll(aa[i], i)) - srt.begin();
    }

    vector<ll> qa(q), qb(q), bluel(q), bluer(q), purplel(q), purpler(q);
    vector<event> events;
    for (ll i = 0; i < q; i++) {
        cin >> qa[i] >> qb[i];
        ll fp = qa[i];
        ll fr = n - qb[i];
        if (fp > fr) swap(fp, fr);
        events.emplace_back(fp, 0, fp-1, &bluel[i]);
        events.emplace_back(fr, 0, fp-1, &purplel[i]);
        events.emplace_back(fp, fr, n-1, &bluer[i]);
        events.emplace_back(fr, fr, n-1, &purpler[i]);
    }
    sort(all(events));

    segtree is_sorted(n);
    for (ll i = 0; i < n; i++) {
        is_sorted.point_set(i, aa[i] == i);
    }

    ll curt = 0;
    segtree tree(n);
    for (auto &[t, l, r, p] : events) {
        while (curt < t) {
            tree.point_set(srt[curt].second, 1);
            curt++;
        }
        *p = tree.range_sum(l, r);
    }

    // --------

    for (ll i = 0; i < q; i++) {
        ll a = qa[i], b = qb[i];
        ll lb = bluel[i], lp = purplel[i] - lb;
        ll rb = bluer[i], rp = purpler[i] - rb;
        ll fp = qa[i];
        ll fr = n - qb[i];
        if (fp > fr) swap(fp, fr);
        ll lr = fp - lb - lp, rr = (n - fr) - rb - rp;
        ll mb = fp - lb - rb, mr = (n - fr) - lr - rr;
        ll mp = n - lb - lp - lr - mb - mr - rb - rp - rr;
        if (a + b <= n) {
            bool bad = lb < a || rr < b || is_sorted.range_sum(fp, fr-1) < fr - fp;
            if (bad) cout << "-1\n";
            else {
                ll cnt = (is_sorted.range_sum(0, fp-1) < fp) + (is_sorted.range_sum(fr, n-1) < n - fr);
                cout << cnt << '\n';
            }
            continue;
        }
        if (is_sorted.range_sum(0, n-1) == n) {
            cout << "0\n";
            continue;
        }
        if (is_sorted.range_sum(0, fp-1) == fp || is_sorted.range_sum(fr, n-1) == n - fr) {
            cout << "1\n";
            continue;
        }

        // a + b > n

        ll res = inf;
        {
            ll non_blue = lp + lr + mp + mr - (fr - fp);
            ll non_red = rb + rp;
            if (non_blue < 0) non_blue = 0;
            if (non_red < 0) non_red = 0;
            ll cand = 1 + (non_blue + (fr - fp) - 1) / (fr - fp) + max(1ll, (non_red + (fr - fp) - 1) / (fr - fp));
            res = min(res, cand);
        }
        {
            ll non_blue = lp + lr;
            ll non_red = rb + rp + mb + mp - (fr - fp);
            if (non_blue < 0) non_blue = 0;
            if (non_red < 0) non_red = 0;
            ll cand = 1 + max(1ll, (non_blue + (fr - fp) - 1) / (fr - fp)) + (non_red + (fr - fp) - 1) / (fr - fp);
            res = min(res, cand);
        }
        cout << res << '\n';
    }
}

int main() {
    cin.tie(0)->sync_with_stdio(0);
    ll t; cin >> t; while (t--) solve(); // TODO
}

Test details

Test 1

Subtask: 2, 6, 7

Verdict:

input
6 3
3 1 4 1 5 9
4 1
3 3
2 5

correct output
1
-1
2

user output
2
2
1
0
0
...

Feedback: Output is longer than expected

Test 2

Subtask: 1, 2, 3, 6, 7

Verdict:

input
2 1
548813503 548813503
1 1

correct output
0

user output
(empty)

Test 3

Subtask: 2, 6, 7

Verdict:

input
1 1
417021999
1 1

correct output
0

user output
(empty)

Test 4

Subtask: 1, 2, 3, 6, 7

Verdict:

input
10 10
20751947 20751947 20751947 494...

correct output
-1
-1
1
1
-1
...

user output
(empty)

Test 5

Subtask: 1, 2, 3, 6, 7

Verdict:

input
10 10
12780811 19475241 19475241 683...

correct output
0
0
0
0
0
...

user output
(empty)

Test 6

Subtask: 1, 2, 3, 6, 7

Verdict:

input
10 10
14574963 14574963 14574963 864...

correct output
0
0
0
0
0
...

user output
(empty)

Test 7

Subtask: 1, 2, 3, 6, 7

Verdict:

input
10 10
237541216 237541216 35036522 6...

correct output
-1
-1
-1
2
-1
...

user output
(empty)

Test 8

Subtask: 1, 2, 3, 6, 7

Verdict:

input
10 10
319425549 513116712 539199939 ...

correct output
-1
-1
-1
-1
-1
...

user output
(empty)

Test 9

Subtask: 1, 2, 3, 6, 7

Verdict:

input
10 10
54363219 54363219 110986323 11...

correct output
0
0
0
0
0
...

user output
(empty)

Test 10

Subtask: 1, 2, 3, 6, 7

Verdict:

input
10 5
55687086 550701455 326656159 5...

correct output
-1
-1
-1
-1
-1

user output
(empty)

Test 11

Subtask: 2, 6, 7

Verdict:

input
10 10
35889584 588130796 815837475 8...

correct output
-1
-1
1
1
1
...

user output
(empty)

Test 12

Subtask: 2, 6, 7

Verdict:

input
10 10
679842175 48724877 720966351 6...

correct output
2
3
2
1
1
...

user output
(empty)

Test 13

Subtask: 2, 6, 7

Verdict:

input
10 10
893310183 811950921 338863962 ...

correct output
2
1
1
2
5
...

user output
(empty)

Test 14

Subtask: 2, 6, 7

Verdict:

input
10 10
221045363 282395847 441913686 ...

correct output
0
0
0
0
0
...

user output
(empty)

Test 15

Subtask: 2, 6, 7

Verdict:

input
10 10
509019662 983949268 960017302 ...

correct output
3
-1
3
3
3
...

user output
(empty)

Test 16

Subtask: 3, 7

Verdict:

input
200000 200000
2277053 2277053 2277053 227705...

correct output
-1
-1
-1
-1
-1
...

user output
(empty)

Test 17

Subtask: 3, 7

Verdict:

input
200000 200000
6767 16596 16596 27202 37272 4...

correct output
-1
-1
-1
-1
-1
...

user output
(empty)

Test 18

Subtask: 3, 7

Verdict:

input
200000 200000
5393 5910 9099 15755 15755 164...

correct output
-1
-1
-1
-1
-1
...

user output
(empty)

Test 19

Subtask: 3, 7

Verdict:

input
200000 200000
3779 4629 8999 10468 22605 227...

correct output
-1
1
2
-1
-1
...

user output
(empty)

Test 20

Subtask: 3, 7

Verdict:

input
200000 200000
878 2791 10849 11861 13405 239...

correct output
-1
-1
-1
-1
-1
...

user output
(empty)

Test 21

Subtask: 3, 7

Verdict:

input
200000 200000
430 1479 1992 2829 7152 14093 ...

correct output
-1
-1
-1
-1
-1
...

user output
(empty)

Test 22

Subtask: 3, 7

Verdict:

input
199997 200000
2733 10526 13882 14035 14689 3...

correct output
-1
-1
-1
-1
-1
...

user output
(empty)

Test 23

Subtask: 3, 7

Verdict:

input
200000 200000
12345 13538 15407 18490 18984 ...

correct output
0
0
0
0
0
...

user output
(empty)

Test 24

Subtask: 4, 7

Verdict:

input
200000 200000
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ...

correct output
-1
1
-1
-1
-1
...

user output
(empty)

Test 25

Subtask: 4, 7

Verdict:

input
200000 200000
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ...

correct output
1
-1
1
-1
1
...

user output
(empty)

Test 26

Subtask: 4, 7

Verdict:

input
200000 200000
2 2 1 1 2 1 2 1 2 2 2 1 1 1 2 ...

correct output
4
3
2
2
6
...

user output
(empty)

Test 27

Subtask: 4, 7

Verdict:

input
200000 200000
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ...

correct output
0
0
0
0
0
...

user output
(empty)

Test 28

Subtask: 4, 7

Verdict:

input
200000 200000
2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 ...

correct output
8513
1099
35939
9299
19597
...

user output
(empty)

Test 29

Subtask: 4, 7

Verdict:

input
200000 200000
2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 ...

correct output
178345
169257
62115
96143
64796
...

user output
(empty)

Test 30

Subtask: 5, 6, 7

Verdict:

input
5000 5000
1 2 3 4 5 6 7 8 9 10 11 12 13 ...

correct output
1
1
-1
1
-1
...

user output
(empty)

Test 31

Subtask: 5, 6, 7

Verdict:

input
5000 5000
1 2 3 4 5 6 7 8 9 72 145 47 19...

correct output
-1
2
-1
2
-1
...

user output
(empty)

Test 32

Subtask: 5, 6, 7

Verdict:

input
4999 5000
1 2 3 4 5 6 7 8 9 10 11 12 13 ...

correct output
-1
2
-1
-1
-1
...

user output
(empty)

Test 33

Subtask: 5, 6, 7

Verdict:

input
5000 5000
4033 4368 3086 3208 4313 388 8...

correct output
3
3
3
3
11
...

user output
(empty)

Test 34

Subtask: 5, 6, 7

Verdict:

input
5000 5000
1 2 3 4 5 6 7 8 9 10 11 12 13 ...

correct output
0
0
0
0
0
...

user output
(empty)

Test 35

Subtask: 5, 6, 7

Verdict:

input
5000 5000
5000 4999 4998 4997 4996 4995 ...

correct output
2039
1910
-1
687
673
...

user output
(empty)

Test 36

Subtask: 6, 7

Verdict:

input
5000 5000
202010 457852 826471 926337 10...

correct output
-1
-1
2
-1
-1
...

user output
(empty)

Test 37

Subtask: 6, 7

Verdict:

input
5000 5000
190583 326486 431922 462939 72...

correct output
-1
-1
-1
-1
2
...

user output
(empty)

Test 38

Subtask: 6, 7

Verdict:

input
5000 5000
821998255 400550008 71790232 5...

correct output
5
3
3
2
5
...

user output
(empty)

Test 39

Subtask: 6, 7

Verdict:

input
5000 5000
266174928 446601941 191252234 ...

correct output
3
3
414
4
3
...

user output
(empty)

Test 40

Subtask: 6, 7

Verdict:

input
5000 5000
11621 243915 243915 949123 137...

correct output
0
0
0
0
0
...

user output
(empty)

Test 41

Subtask: 6, 7

Verdict:

input
5000 5000
999767052 998555066 997822810 ...

correct output
919
459
505
833
809
...

user output
(empty)

Test 42

Subtask: 7

Verdict:

input
200000 200000
478025 478025 478025 478025 47...

correct output
-1
-1
2
2
2
...

user output
(empty)

Test 43

Subtask: 7

Verdict:

input
200000 200000
1810 2088 3022 3097 7459 7943 ...

correct output
2
-1
-1
2
-1
...

user output
(empty)

Test 44

Subtask: 7

Verdict:

input
199531 200000
11328 26391 30353 37063 44412 ...

correct output
-1
2
-1
2
2
...

user output
(empty)

Test 45

Subtask: 7

Verdict:

input
200000 200000
106738201 369187074 412614650 ...

correct output
2
12
3
19
3
...

user output
(empty)

Test 46

Subtask: 7

Verdict:

input
200000 200000
670611290 43427363 8475380 309...

correct output
3
5
3
3
3
...

user output
(empty)

Test 47

Subtask: 7

Verdict:

input
200000 200000
907542569 504758282 948727805 ...

correct output
3
33
3
3
3
...

user output
(empty)

Test 48

Subtask: 7

Verdict:

input
200000 200000
487056731 460461648 142698485 ...

correct output
3
3
3
3
3
...

user output
(empty)

Test 49

Subtask: 7

Verdict:

input
200000 200000
12772 23236 23236 23236 41149 ...

correct output
0
0
0
0
0
...

user output
(empty)

Test 50

Subtask: 7

Verdict:

input
200000 200000
999993539 999993361 999993361 ...

correct output
125375
16687
-1
84781
46147
...

user output
(empty)

Test 51

Subtask: 7

Verdict:

input
200000 200000
999993539 999993361 999993361 ...

correct output
94788
177608
95881
56377
179957
...

user output
(empty)

Test 52

Subtask: 1, 2, 3, 5, 6, 7

Verdict:

input
5 1
2 1 3 5 4
2 2

correct output
2

user output
0
0
0
0
0
...

Feedback: Output is longer than expected

Test 53

Subtask: 1, 2, 3, 4, 5, 6, 7

Verdict:

input
2 1
1 2
1 1

correct output
0

user output
0
0

Feedback: Output is longer than expected

Test 54

Subtask: 2, 5, 6, 7

Verdict:

input
4 1
2 1 4 3
3 2

correct output
2

user output
0
0
0
0
0
...

Feedback: Output is longer than expected

Test 55

Subtask: 1, 2, 3, 4, 6, 7

Verdict:

input
10 10
1 1 1 1 2 2 1 1 2 2
7 1
2 6
4 5
...

correct output
-1
1
-1
-1
-1
...

user output
-1
1
-1
2
-1
...

Feedback: Output is longer than expected

Test 56

Subtask: 1, 2, 3, 6, 7

Verdict:

input
10 10
181777772 181777772 181777772 ...

correct output
-1
-1
-1
-1
-1
...

user output
(empty)

Test 57

Subtask: 6, 7

Verdict:

input
100 10
92800811 524548163 939127795 9...

correct output
-1
-1
3
-1
3
...

user output
(empty)

Test 58

Subtask: 3, 4, 7

Verdict:

input
100 100000
2 2 2 1 2 2 2 2 1 2 1 1 1 2 2 ...

correct output
-1
-1
-1
-1
-1
...

user output
-1
-1
3
2
2
...

Feedback: Output is shorter than expected

Test 59

Subtask: 3, 7

Verdict:

input
100 100000
172695325 172695325 172695325 ...

correct output
-1
-1
-1
-1
-1
...

user output
(empty)

Test 60

Subtask: 2, 6, 7

Verdict:

input
4 10
869194539 239439572 968540665 ...

correct output
-1
2
-1
-1
-1
...

user output
(empty)

Test 61

Subtask: 2, 6, 7

Verdict:

input
10 10
55366041 112170735 112170735 5...

correct output
-1
-1
2
1
1
...

user output
(empty)

Test 62

Subtask: 2, 6, 7

Verdict:

input
10 10
156018639 156018639 445832758 ...

correct output
-1
1
-1
-1
2
...

user output
(empty)

Test 63

Subtask: 2, 6, 7

Verdict:

input
10 10
702507512 702507512 892090406 ...

correct output
-1
2
-1
2
2
...

user output
(empty)

Test 64

Subtask: 2, 6, 7

Verdict:

input
10 10
720324490 720324490 720324490 ...

correct output
-1
2
2
1
-1
...

user output
(empty)