QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#577731#9327. Permutation and QueriesAfterlife#TL 39ms13244kbC++202.1kb2024-09-20 14:23:262024-09-20 14:23:28

Judging History

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

  • [2024-09-20 14:23:28]
  • 评测
  • 测评结果:TL
  • 用时:39ms
  • 内存:13244kb
  • [2024-09-20 14:23:26]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
int n;
struct mt {
    int p[100005] , B;
    priority_queue<int , vector<int> , greater<int> > pq[320] , dec[320];
    void init() {
        B = sqrt(n);
        for(int i = 1;i <= n;i++) {
            for(int j = i + 1 ; j <= i + B && j <= n;j++) pq[j - i].push(abs(p[i] - p[j]));
        }
    }
    int calc() {
        int ans = 1e9;
        for(int i = 1;i <= B;i++) {
            while(dec[i].size() && pq[i].top() == dec[i].top()) {
                pq[i].pop() ; dec[i].pop() ;
            }
            int mn = pq[i].top() ;
            if(1LL * i* mn < ans) ans = i * mn;
        }
        return ans;
    }
    void swp(int a,int b) {
        if(a > b) swap(a,b) ;
        for(int i = max(1 , a - B) ; i <= a + B && i <= n;i++) {
            if(i == a) continue ;
            dec[abs(i - a)].push(abs(p[i] - p[a]));
        }
        for(int i = max(1 , b - B) ; i <= b + B && i <= n;i++) {
            if(i == b) continue ;
            if(i == a) continue ;
            dec [abs(i - b)].push(abs(p[i] - p[b])) ;
        }

        swap(p[a] , p[b]) ;
        for(int i = max(1 , a - B) ; i <= a + B && i <= n;i++) {
            if(i == a) continue ;
            pq[abs(i - a)].push(abs(p[i] - p[a]));
        }
        for(int i = max(1 , b - B) ; i <= b + B && i <= n;i++) {
            if(i == b) continue ;
            if(i == a) continue ;
            pq [abs(i - b)].push(abs(p[i] - p[b])) ;
        }
        return ;
    }
}p , r;
int q;
int val[100005];
void solv() {
    cin >> n >> q;
    for(int i = 1;i <= n;i++) {
        cin >> p.p[i];
        val[i] = p.p[i];
        r.p[p.p[i]] = i;
    }
    p.init() ; r.init() ;
    cout << min(p.calc() , r.calc()) << '\n' ;
    for(int i = 1;i <= q;i++) {
        int a , b;
        cin >> a >> b;
        p.swp(a,b) ;
        r.swp(val[a] ,val[b]);
        swap(val[a] , val[b]) ;
        cout << min(p.calc() , r.calc()) << '\n' ;
    }
    return ;
}
int main() {
    ios::sync_with_stdio(false) ; cin.tie(0) ;
    int t = 1;
    while(t--) solv() ;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

6 5
2 4 1 6 3 5
1 2
3 5
1 2
5 3
5 6

output:

2
1
1
1
2
1

result:

ok 6 numbers

Test #2:

score: 0
Accepted
time: 0ms
memory: 3668kb

input:

10 20
1 4 7 10 2 5 8 3 6 9
10 1
5 7
1 2
5 3
6 3
9 4
3 4
9 6
8 4
9 6
8 7
3 8
10 7
2 7
3 7
5 9
7 6
4 6
2 10
8 9

output:

3
3
1
1
1
1
1
1
1
2
1
1
1
1
1
1
1
1
1
1
1

result:

ok 21 numbers

Test #3:

score: 0
Accepted
time: 0ms
memory: 3692kb

input:

5 6
1 3 5 2 4
2 4
1 3
2 4
1 4
2 3
4 2

output:

2
1
1
1
1
1
2

result:

ok 7 numbers

Test #4:

score: 0
Accepted
time: 0ms
memory: 3668kb

input:

4 4
1 3 2 4
1 2
3 1
1 3
2 3

output:

1
1
1
1
1

result:

ok 5 number(s): "1 1 1 1 1"

Test #5:

score: 0
Accepted
time: 0ms
memory: 3752kb

input:

3 5
1 2 3
1 3
2 1
3 2
2 1
2 1

output:

1
1
1
1
1
1

result:

ok 6 numbers

Test #6:

score: 0
Accepted
time: 5ms
memory: 4088kb

input:

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

output:

10
10
10
3
3
2
2
2
2
2
2
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
2
2
2
2
2
2
2
1
1
1
1
1
1
1
1
1
1
1
1
2
2
2
2
2
2
2
2
2
2
2
2
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
2
2
2
2
2
2
2
2
2...

result:

ok 1001 numbers

Test #7:

score: 0
Accepted
time: 9ms
memory: 6352kb

input:

1000 1000
1 32 63 94 125 156 187 218 249 280 311 342 373 404 435 466 497 528 559 590 621 652 683 714 745 776 807 838 869 900 931 962 993 2 33 64 95 126 157 188 219 250 281 312 343 374 405 436 467 498 529 560 591 622 653 684 715 746 777 808 839 870 901 932 963 994 3 34 65 96 127 158 189 220 251 282 3...

output:

31
21
21
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
9
9
9
9
9
9
9
9
8
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
1
1
1
1
1
1
1
...

result:

ok 1001 numbers

Test #8:

score: 0
Accepted
time: 13ms
memory: 5724kb

input:

292 1828
1 18 35 52 69 86 103 120 137 154 171 188 205 222 239 256 273 290 2 19 36 53 70 87 104 121 138 155 172 189 206 223 240 257 274 291 3 20 37 54 71 88 105 122 139 156 173 190 207 224 241 258 275 292 4 21 38 55 72 89 106 123 140 157 174 191 208 225 242 259 276 5 22 39 56 73 90 107 124 141 158 17...

output:

17
9
9
6
6
6
6
6
6
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1...

result:

ok 1829 numbers

Test #9:

score: 0
Accepted
time: 39ms
memory: 13244kb

input:

1919 2393
1 44 87 130 173 216 259 302 345 388 431 474 517 560 603 646 689 732 775 818 861 904 947 990 1033 1076 1119 1162 1205 1248 1291 1334 1377 1420 1463 1506 1549 1592 1635 1678 1721 1764 1807 1850 1893 2 45 88 131 174 217 260 303 346 389 432 475 518 561 604 647 690 733 776 819 862 905 948 991 1...

output:

43
43
43
42
42
42
42
42
42
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1...

result:

ok 2394 numbers

Test #10:

score: 0
Accepted
time: 25ms
memory: 8956kb

input:

1000 2000
1 32 63 94 125 156 187 218 249 280 311 342 373 404 435 466 497 528 559 590 621 652 683 714 745 776 807 838 869 900 931 962 993 2 33 64 95 126 157 188 219 250 281 312 343 374 405 436 467 498 529 560 591 622 653 684 715 746 777 808 839 870 901 932 963 994 3 34 65 96 127 158 189 220 251 282 3...

output:

31
16
16
12
12
12
12
12
12
12
12
3
3
3
3
3
3
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2...

result:

ok 2001 numbers

Test #11:

score: 0
Accepted
time: 3ms
memory: 5540kb

input:

500 999
1 23 45 67 89 111 133 155 177 199 221 243 265 287 309 331 353 375 397 419 441 463 485 2 24 46 68 90 112 134 156 178 200 222 244 266 288 310 332 354 376 398 420 442 464 486 3 25 47 69 91 113 135 157 179 201 223 245 267 289 311 333 355 377 399 421 443 465 487 4 26 48 70 92 114 136 158 180 202 ...

output:

22
22
22
19
19
16
16
12
12
12
8
8
8
8
8
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
3
3
3
3
3
3
3
3
3
3
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
...

result:

ok 1000 numbers

Test #12:

score: 0
Accepted
time: 32ms
memory: 10592kb

input:

1600 2000
1 41 81 121 161 201 241 281 321 361 401 441 481 521 561 601 641 681 721 761 801 841 881 921 961 1001 1041 1081 1121 1161 1201 1241 1281 1321 1361 1401 1441 1481 1521 1561 2 42 82 122 162 202 242 282 322 362 402 442 482 522 562 602 642 682 722 762 802 842 882 922 962 1002 1042 1082 1122 116...

output:

40
40
40
40
40
40
40
36
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
...

result:

ok 2001 numbers

Test #13:

score: 0
Accepted
time: 24ms
memory: 10892kb

input:

1600 2000
1 41 81 121 161 201 241 281 321 361 401 441 481 521 561 601 641 681 721 761 801 841 881 921 961 1001 1041 1081 1121 1161 1201 1241 1281 1321 1361 1401 1441 1481 1521 1561 2 42 82 122 162 202 242 282 322 362 402 442 482 522 562 602 642 682 722 762 802 842 882 922 962 1002 1042 1082 1122 116...

output:

40
40
40
40
40
40
40
18
15
15
15
15
15
15
15
15
15
11
11
11
11
11
11
11
11
11
11
11
11
11
11
11
11
11
11
11
11
11
11
11
11
11
11
11
11
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2...

result:

ok 2001 numbers

Test #14:

score: 0
Accepted
time: 11ms
memory: 6136kb

input:

900 900
1 31 61 91 121 151 181 211 241 271 301 331 361 391 421 451 481 511 541 571 601 631 661 691 721 751 781 811 841 871 2 32 62 92 122 152 182 212 242 272 302 332 362 392 422 452 482 512 542 572 602 632 662 692 722 752 782 812 842 872 3 33 63 93 123 153 183 213 243 273 303 333 363 393 423 453 483...

output:

30
30
30
18
18
18
18
18
18
18
8
8
8
8
7
7
7
7
7
7
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
...

result:

ok 901 numbers

Test #15:

score: 0
Accepted
time: 0ms
memory: 4400kb

input:

400 500
1 21 41 61 81 101 121 141 161 181 201 221 241 261 281 301 321 341 361 381 2 22 42 62 82 102 122 142 162 182 202 222 242 262 282 302 322 342 362 382 3 23 43 63 83 103 123 143 163 183 203 223 243 263 283 303 323 343 363 383 4 24 44 64 84 104 124 144 164 184 204 224 244 264 284 304 324 344 364 ...

output:

20
20
16
13
13
13
13
13
13
6
4
4
4
4
4
4
4
4
4
4
4
3
3
3
3
3
3
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
1
1
1
1
1
1
1
1
1
1
1
1
1
1...

result:

ok 501 numbers

Test #16:

score: 0
Accepted
time: 4ms
memory: 4384kb

input:

400 500
1 21 41 61 81 101 121 141 161 181 201 221 241 261 281 301 321 341 361 381 2 22 42 62 82 102 122 142 162 182 202 222 242 262 282 302 322 342 362 382 3 23 43 63 83 103 123 143 163 183 203 223 243 263 283 303 323 343 363 383 4 24 44 64 84 104 124 144 164 184 204 224 244 264 284 304 324 344 364 ...

output:

20
19
4
4
4
4
4
4
4
4
4
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
...

result:

ok 501 numbers

Test #17:

score: -100
Time Limit Exceeded

input:

100000 100000
1 317 633 949 1265 1581 1897 2213 2529 2845 3161 3477 3793 4109 4425 4741 5057 5373 5689 6005 6321 6637 6953 7269 7585 7901 8217 8533 8849 9165 9481 9797 10113 10429 10745 11061 11377 11693 12009 12325 12641 12957 13273 13589 13905 14221 14537 14853 15169 15485 15801 16117 16433 16749 ...

output:

316
316
316
316
316
298
298
298
298
298
298
298
298
298
298
298
298
298
150
150
150
150
150
150
150
150
150
150
150
150
150
150
150
150
150
150
150
150
150
150
150
150
150
150
150
150
150
150
150
150
150
150
150
150
150
150
150
150
150
150
150
150
150
150
150
150
150
150
150
150
150
150
20
20
20
20
...

result: