Submission details
Task:Sort
Sender:MRiekasius
Submission time:2026-04-17 13:37:40 +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.00 s1, 2, 3, 6, 7details
#30.00 s2, 6, 7details
#40.00 s1, 2, 3, 6, 7details
#50.00 s1, 2, 3, 6, 7details
#60.00 s1, 2, 3, 6, 7details
#70.00 s1, 2, 3, 6, 7details
#80.00 s1, 2, 3, 6, 7details
#90.00 s1, 2, 3, 6, 7details
#10ACCEPTED0.00 s1, 2, 3, 6, 7details
#110.00 s2, 6, 7details
#120.00 s2, 6, 7details
#130.00 s2, 6, 7details
#140.00 s2, 6, 7details
#150.00 s2, 6, 7details
#160.54 s3, 7details
#170.54 s3, 7details
#180.54 s3, 7details
#190.54 s3, 7details
#200.54 s3, 7details
#210.54 s3, 7details
#220.54 s3, 7details
#230.54 s3, 7details
#24--4, 7details
#25--4, 7details
#26--4, 7details
#27--4, 7details
#28--4, 7details
#29--4, 7details
#300.04 s5, 6, 7details
#310.04 s5, 6, 7details
#320.03 s5, 6, 7details
#330.18 s5, 6, 7details
#34ACCEPTED0.04 s5, 6, 7details
#350.06 s5, 6, 7details
#360.04 s6, 7details
#370.04 s6, 7details
#380.06 s6, 7details
#390.06 s6, 7details
#400.05 s6, 7details
#410.06 s6, 7details
#42--7details
#43--7details
#44--7details
#45--7details
#46--7details
#47--7details
#48--7details
#49--7details
#50--7details
#51--7details
#52ACCEPTED0.00 s1, 2, 3, 5, 6, 7details
#53ACCEPTED0.00 s1, 2, 3, 4, 5, 6, 7details
#54ACCEPTED0.00 s2, 5, 6, 7details
#550.00 s1, 2, 3, 4, 6, 7details
#560.00 s1, 2, 3, 6, 7details
#570.00 s6, 7details
#580.19 s3, 4, 7details
#590.19 s3, 7details
#600.00 s2, 6, 7details
#610.00 s2, 6, 7details
#620.00 s2, 6, 7details
#630.00 s2, 6, 7details
#640.00 s2, 6, 7details

Code

#include <bits/stdc++.h>
using namespace std;

int main()
{
    int n, q;
    cin >> n >> q;
    vector<int> skaiciai(n);
    for(int& i: skaiciai) cin >> i;
    vector<int> didziausiasSkaiciusIki(n, -1), maziausiasSkaiciusNuo(n, -1);
    vector<int> skaiciaiInPlaceIki(n, 0);
    didziausiasSkaiciusIki[0] = skaiciai[0];
    skaiciaiInPlaceIki[0] = (skaiciai[0] == 1);
    maziausiasSkaiciusNuo[n - 1] = skaiciai[n-1];
    for(int i = 1; i < n; i++){
        skaiciaiInPlaceIki[i] = skaiciaiInPlaceIki[i - 1] + (int)(skaiciai[i] == i + 1);
        didziausiasSkaiciusIki[i] = max(didziausiasSkaiciusIki[i - 1], skaiciai[i]);
    }
    for(int i = n - 2; i >= 0; i--){
        maziausiasSkaiciusNuo[i] = min(skaiciai[i], maziausiasSkaiciusNuo[i+1]);
    }
    /*
    for(int i : skaiciaiInPlaceIki) cout << i << " ";
    cout << "\n";
    for(int i : didziausiasSkaiciusIki) cout << i << " ";
    cout << "\n";
    for(int i : maziausiasSkaiciusNuo) cout << i << " ";
    cout << "\n";
    //*/

    while(q--){
        int a, b;
        cin >> a >> b;
        if(a + b <= n){
            if(a == n || b == n){
                cout << (skaiciaiInPlaceIki[n-1] == n) << "\n";
            }
            bool reikiaRikiuotiKaire = false;
            if(a > 0) reikiaRikiuotiKaire = (skaiciaiInPlaceIki[a - 1] != a);
            bool reikiaRikiuotiDesine = false;
            if(b > 0 && b < n)reikiaRikiuotiDesine = (skaiciaiInPlaceIki[n - 1] - skaiciaiInPlaceIki[n - b - 1] != b);
            if(b == n) reikiaRikiuotiDesine = (skaiciaiInPlaceIki[n - 1] != n);
            bool imanoma = ((skaiciaiInPlaceIki[n - b - 1] - skaiciaiInPlaceIki[a - 1]) == n - a - b) && (maziausiasSkaiciusNuo[n - b] == n - b + 1) && (didziausiasSkaiciusIki[a - 1] == a);
            //cout << reikiaRikiuotiKaire << reikiaRikiuotiDesine << imanoma << "\n";
            int ans = (int)reikiaRikiuotiDesine + (int)reikiaRikiuotiKaire;
            if(!imanoma) ans = -1;
            cout << ans << "\n";
            continue;
        }
        int keitimoGreitis = a + b - n;
        const int lStarts = 0;
        const int lEnds = n - b - 1;
        const int rStarts = a;
        const int rEnds = n - 1;
        const int midStarts = n - b;
        const int midEnds = a - 1;
        int ltor = 0, ltom = 0, mtol = 0, mtor = 0, rtol = 0, rtom = 0;
        bool lNeedsSort = (skaiciaiInPlaceIki[n - b - 1] != n - b);
        bool rNeedsSort = (skaiciaiInPlaceIki[n - 1] - skaiciaiInPlaceIki[a-1] != n - a);

        for(int i = lStarts; i <= lEnds; i++){
            int j = skaiciai[i] - 1;
            if(j >= midStarts && j <= midEnds) ltom++;
            if(j >= rStarts) ltor++;
        }
        for(int i = midStarts; i <= midEnds; i++){
            int j = skaiciai[i] - 1;
            if(j <= lEnds) mtol++;
            if(j >= rStarts) mtor++;
        }
        for(int i = rStarts; i <= rEnds; i++){
            int j = skaiciai[i] - 1;
            if(j >= midStarts && j <= midEnds) rtom++;
            if(j <= lEnds) rtol++;
        }
        //cout << lStarts << " " << lEnds << " " << midStarts << " " << midEnds << " " << rStarts << " " << rEnds << "\n";
        //cout << ltom << " " << ltor << " " << mtol << " " << mtor << " " << rtol << " " << rtom << "\n";
        //cout << lNeedsSort << " " << rNeedsSort << "\n";
        int lIsstumtiTotal = ltor + ltom;
        int rIsstumtiTotal = rtol + rtom;
        int LejimuDelL = max((int) lNeedsSort, (lIsstumtiTotal)/keitimoGreitis + (int)(lIsstumtiTotal % keitimoGreitis != 0));
        int RejimuDelL = ltor/keitimoGreitis + (int)(ltor % keitimoGreitis != 0);
        int LejimuDelM = mtol/keitimoGreitis + (int) (mtol % keitimoGreitis != 0);
        int RejimuDelM = mtor/keitimoGreitis + (int) (mtor % keitimoGreitis != 0);
        int RejimuDelR = max((int) rNeedsSort, (rIsstumtiTotal)/keitimoGreitis + (int)(rIsstumtiTotal % keitimoGreitis != 0) );
        int LejimuDelR = rtol/keitimoGreitis + (int)(rtol % keitimoGreitis != 0);
        //cout << LejimuDelL << " " << LejimuDelM << " " << LejimuDelR << " " << RejimuDelL << " " << RejimuDelM << " " << RejimuDelR << "\n";
        int Ejimai = max(LejimuDelL, max(LejimuDelM, LejimuDelR)) + max(RejimuDelL, max(RejimuDelM, RejimuDelR));
        if(RejimuDelL != 0 && LejimuDelR != 0) Ejimai++;
        if(Ejimai == 0){
            bool mNeedsSort = (skaiciaiInPlaceIki[a-1] - skaiciaiInPlaceIki[n - b - 1] != keitimoGreitis);
            Ejimai += (int)mNeedsSort;
        }
        cout << Ejimai << "\n";
        //cout << keitimoGreitis << " " << reikiaKeistiKaireje << " " << reikiaKeistiDesineje << "\n";

    }
}

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
-1
-1
3

Feedback: Incorrect character on line 1 col 1: expected "1", got "-1"

Test 2

Subtask: 1, 2, 3, 6, 7

Verdict:

input
2 1
548813503 548813503
1 1

correct output
0

user output
-1

Feedback: Incorrect character on line 1 col 1: expected "0", got "-1"

Test 3

Subtask: 2, 6, 7

Verdict:

input
1 1
417021999
1 1

correct output
0

user output
1

Feedback: Incorrect character on line 1 col 1: expected "0", got "1"

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
-1
-1
-1
-1
-1
...

Feedback: Incorrect character on line 3 col 1: expected "1", got "-1"

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
-1
-1
-1
-1
-1
...

Feedback: Incorrect character on line 1 col 1: expected "0", got "-1"

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
-1
-1
-1
-1
-1
...

Feedback: Incorrect character on line 1 col 1: expected "0", got "-1"

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
-1
-1
-1
-1
-1
...

Feedback: Incorrect character on line 4 col 1: expected "2", got "-1"

Test 8

Subtask: 1, 2, 3, 6, 7

Verdict:

input
10 10
319425549 513116712 539199939 ...

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

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

Feedback: Incorrect character on line 6 col 1: expected "1", got "-1"

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
-1
-1
-1
-1
-1
...

Feedback: Incorrect character on line 1 col 1: expected "0", got "-1"

Test 10

Subtask: 1, 2, 3, 6, 7

Verdict: ACCEPTED

input
10 5
55687086 550701455 326656159 5...

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

user output
-1
-1
-1
-1
-1

Test 11

Subtask: 2, 6, 7

Verdict:

input
10 10
35889584 588130796 815837475 8...

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

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

Feedback: Incorrect character on line 3 col 1: expected "1", got "-1"

Test 12

Subtask: 2, 6, 7

Verdict:

input
10 10
679842175 48724877 720966351 6...

correct output
2
3
2
1
1
...

user output
2
16
6
1
1
...

Feedback: Incorrect character on line 2 col 1: expected "3", got "16"

Test 13

Subtask: 2, 6, 7

Verdict:

input
10 10
893310183 811950921 338863962 ...

correct output
2
1
1
2
5
...

user output
2
1
4
2
6
...

Feedback: Incorrect character on line 3 col 1: expected "1", got "4"

Test 14

Subtask: 2, 6, 7

Verdict:

input
10 10
221045363 282395847 441913686 ...

correct output
0
0
0
0
0
...

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

Feedback: Incorrect character on line 1 col 1: expected "0", got "2"

Test 15

Subtask: 2, 6, 7

Verdict:

input
10 10
509019662 983949268 960017302 ...

correct output
3
-1
3
3
3
...

user output
2
-1
2
2
8
...

Feedback: Incorrect character on line 1 col 1: expected "3", got "2"

Test 16

Subtask: 3, 7

Verdict:

input
200000 200000
2277053 2277053 2277053 227705...

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

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

Feedback: Incorrect character on line 46 col 1: expected "2", got "-1"

Test 17

Subtask: 3, 7

Verdict:

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

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

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

Feedback: Incorrect character on line 6 col 1: expected "1", got "-1"

Test 18

Subtask: 3, 7

Verdict:

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

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

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

Feedback: Incorrect character on line 11 col 1: expected "2", got "-1"

Test 19

Subtask: 3, 7

Verdict:

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

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

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

Feedback: Incorrect character on line 2 col 1: expected "1", got "-1"

Test 20

Subtask: 3, 7

Verdict:

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

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

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

Feedback: Incorrect character on line 13 col 1: expected "2", got "-1"

Test 21

Subtask: 3, 7

Verdict:

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

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

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

Feedback: Incorrect character on line 98 col 1: expected "2", got "-1"

Test 22

Subtask: 3, 7

Verdict:

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

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

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

Feedback: Incorrect character on line 20 col 1: expected "1", got "-1"

Test 23

Subtask: 3, 7

Verdict:

input
200000 200000
12345 13538 15407 18490 18984 ...

correct output
0
0
0
0
0
...

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

Feedback: Incorrect character on line 1 col 1: expected "0", got "-1"

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
1
1
-1
1
-1
...

Feedback: Incorrect character on line 11 col 1: expected "3", got "4"

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
-1
2
-1
2
-1
...

Feedback: Incorrect character on line 757 col 1: expected "4", got "5"

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
-1
2
-1
-1
-1
...

Feedback: Incorrect character on line 1036 col 1: expected "3", got "4"

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
4
3
3
3
11
...

Feedback: Incorrect character on line 1 col 1: expected "3", got "4"

Test 34

Subtask: 5, 6, 7

Verdict: ACCEPTED

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
0
0
0
0
0
...

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
2040
1911
-1
688
674
...

Feedback: Incorrect character on line 1 col 3: expected "2039", got "2040"

Test 36

Subtask: 6, 7

Verdict:

input
5000 5000
202010 457852 826471 926337 10...

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

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

Feedback: Incorrect character on line 3 col 1: expected "2", got "-1"

Test 37

Subtask: 6, 7

Verdict:

input
5000 5000
190583 326486 431922 462939 72...

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

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

Feedback: Incorrect character on line 7 col 1: expected "2", got "-1"

Test 38

Subtask: 6, 7

Verdict:

input
5000 5000
821998255 400550008 71790232 5...

correct output
5
3
3
2
5
...

user output
4
10
2
2
6
...

Feedback: Incorrect character on line 1 col 1: expected "5", got "4"

Test 39

Subtask: 6, 7

Verdict:

input
5000 5000
266174928 446601941 191252234 ...

correct output
3
3
414
4
3
...

user output
2
2
904
12
2
...

Feedback: Incorrect character on line 1 col 1: expected "3", got "2"

Test 40

Subtask: 6, 7

Verdict:

input
5000 5000
11621 243915 243915 949123 137...

correct output
0
0
0
0
0
...

user output
12
1732
144
-1
2
...

Feedback: Incorrect character on line 1 col 1: expected "0", got "12"

Test 41

Subtask: 6, 7

Verdict:

input
5000 5000
999767052 998555066 997822810 ...

correct output
919
459
505
833
809
...

user output
1544
2032
514
862
9184
...

Feedback: Incorrect character on line 1 col 1: expected "919", got "1544"

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: ACCEPTED

input
5 1
2 1 3 5 4
2 2

correct output
2

user output
2

Test 53

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

Verdict: ACCEPTED

input
2 1
1 2
1 1

correct output
0

user output
0

Test 54

Subtask: 2, 5, 6, 7

Verdict: ACCEPTED

input
4 1
2 1 4 3
3 2

correct output
2

user output
2

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
-1
-1
...

Feedback: Incorrect character on line 2 col 1: expected "1", got "-1"

Test 56

Subtask: 1, 2, 3, 6, 7

Verdict:

input
10 10
181777772 181777772 181777772 ...

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

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

Feedback: Incorrect character on line 10 col 1: expected "1", got "-1"

Test 57

Subtask: 6, 7

Verdict:

input
100 10
92800811 524548163 939127795 9...

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

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

Feedback: Incorrect character on line 3 col 1: expected "3", got "2"

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
-1
-1
-1
...

Feedback: Incorrect character on line 1401 col 1: expected "1", got "-1"

Test 59

Subtask: 3, 7

Verdict:

input
100 100000
172695325 172695325 172695325 ...

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

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

Feedback: Incorrect character on line 417 col 1: expected "1", got "-1"

Test 60

Subtask: 2, 6, 7

Verdict:

input
4 10
869194539 239439572 968540665 ...

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

user output
-1
4
-1
-1
-1
...

Feedback: Incorrect character on line 2 col 1: expected "2", got "4"

Test 61

Subtask: 2, 6, 7

Verdict:

input
10 10
55366041 112170735 112170735 5...

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

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

Feedback: Incorrect character on line 3 col 1: expected "2", got "14"

Test 62

Subtask: 2, 6, 7

Verdict:

input
10 10
156018639 156018639 445832758 ...

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

user output
-1
8
-1
-1
4
...

Feedback: Incorrect character on line 2 col 1: expected "1", got "8"

Test 63

Subtask: 2, 6, 7

Verdict:

input
10 10
702507512 702507512 892090406 ...

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

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

Feedback: Incorrect character on line 4 col 1: expected "2", got "4"

Test 64

Subtask: 2, 6, 7

Verdict:

input
10 10
720324490 720324490 720324490 ...

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

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

Feedback: Incorrect character on line 2 col 1: expected "2", got "6"