Submission #595990

# Submission time Handle Problem Language Result Execution time Memory
595990 2022-07-14T08:42:20 Z 이동현(#8445) Sushi (JOI16_sushi) C++17
100 / 100
8000 ms 116708 KB
#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
using namespace std;

const int BS = 1000;
priority_queue<int> num[BS], numd[BS];
priority_queue<int, vector<int>, greater<int>> que[BS];
int ask[25004][3], lst[BS];

signed main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    int B = 800;
    int n, q; cin >> n >> q;
    //n = 400000, q = 25000;
    vector<int> a(n);
    for(int i = 0; i < n; ++i){
        cin >> a[i];
       // a[i] = i + 1;
        num[i / B].push(a[i]);
    }
    for(int i = 0; i < q; ++i){
        cin >> ask[i][0] >> ask[i][1] >> ask[i][2];
        lst[ask[i][0] / B] = lst[ask[i][1] / B] = i;
    }
    for(int qq = 0; qq < q; ++qq){
        int x, y, k;
        x = ask[qq][0] - 1, y = ask[qq][1] - 1, k = ask[qq][2];
       // x = 0, y = n - 1, k = q + 300;
        auto sol = [&](int x, int y, int k)->int{
            auto doque = [&](int pos)->void{
                int bp = pos / B;
                if(!(int)que[bp].size()){
                    return;
                }
                int l = bp * B, r = min(n, l + B);
                for(int i = l; i < r; ++i){
                    que[bp].push(a[i]);
                    a[i] = que[bp].top();
                    que[bp].pop();
                }
                while((int)que[bp].size()) que[bp].pop();
            };
            int xb = x / B, yb = y / B;
            int xl = xb * B, xr = min(n, xl + B);
            int yl = yb * B, yr = min(n, yl + B);
            if(xl < x) doque(x);
            if(y + 1 < yr) doque(y);
            if(x == xl) --xb, xl -= B, xr -= B;
            if(y + 1 == yr) ++yb, yl += B, yr += B;

            if(xb < yb){
                for(int i = x; i < xr; ++i){
                    if(a[i] > k){
                        numd[xb].push(a[i]);
                        swap(a[i], k);
                        num[xb].push(a[i]);
                    }
                }
                for(int i = xb + 1; i < yb; ++i){
                    if(lst[i] > qq) que[i].push(k);
                    num[i].push(k);
                    while((int)numd[i].size() && num[i].top() == numd[i].top()) num[i].pop(), numd[i].pop();
                    k = num[i].top();
                    num[i].pop();
                }
                for(int i = yl; i <= y; ++i){
                    if(a[i] > k){
                        numd[yb].push(a[i]);
                        swap(a[i], k);
                        num[yb].push(a[i]);
                    }
                }
            }
            else{
                for(int i = x; i <= y; ++i){
                    if(a[i] > k){
                        numd[xb].push(a[i]);
                        swap(a[i], k);
                        num[xb].push(a[i]);
                    }
                }
            }
            return k;
        };
        //if(x <= y) sol(x, y, k);
        //else sol(0, y, sol(x, n - 1, k));
        if(x <= y) cout << sol(x, y, k) << '\n';
        else cout << sol(0, y, sol(x, n - 1, k)) << '\n';
    }
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 19 ms 484 KB Output is correct
2 Correct 17 ms 492 KB Output is correct
3 Correct 16 ms 468 KB Output is correct
4 Correct 19 ms 492 KB Output is correct
5 Correct 15 ms 536 KB Output is correct
6 Correct 16 ms 544 KB Output is correct
7 Correct 12 ms 480 KB Output is correct
8 Correct 12 ms 468 KB Output is correct
9 Correct 17 ms 560 KB Output is correct
10 Correct 15 ms 508 KB Output is correct
11 Correct 103 ms 3096 KB Output is correct
12 Correct 102 ms 2996 KB Output is correct
13 Correct 75 ms 2108 KB Output is correct
14 Correct 26 ms 528 KB Output is correct
15 Correct 23 ms 504 KB Output is correct
16 Correct 1 ms 468 KB Output is correct
17 Correct 0 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 0 ms 340 KB Output is correct
20 Correct 0 ms 340 KB Output is correct
21 Correct 0 ms 340 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 856 ms 4836 KB Output is correct
2 Correct 804 ms 4928 KB Output is correct
3 Correct 524 ms 4744 KB Output is correct
4 Correct 921 ms 4896 KB Output is correct
5 Correct 458 ms 4756 KB Output is correct
6 Correct 762 ms 4828 KB Output is correct
7 Correct 755 ms 4904 KB Output is correct
8 Correct 757 ms 4744 KB Output is correct
9 Correct 380 ms 4688 KB Output is correct
10 Correct 387 ms 4664 KB Output is correct
11 Correct 311 ms 4648 KB Output is correct
12 Correct 401 ms 4820 KB Output is correct
13 Correct 888 ms 4992 KB Output is correct
14 Correct 882 ms 4684 KB Output is correct
15 Correct 528 ms 4656 KB Output is correct
16 Correct 882 ms 4756 KB Output is correct
17 Correct 454 ms 4916 KB Output is correct
18 Correct 753 ms 4940 KB Output is correct
19 Correct 758 ms 4768 KB Output is correct
20 Correct 759 ms 4976 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 19 ms 484 KB Output is correct
2 Correct 17 ms 492 KB Output is correct
3 Correct 16 ms 468 KB Output is correct
4 Correct 19 ms 492 KB Output is correct
5 Correct 15 ms 536 KB Output is correct
6 Correct 16 ms 544 KB Output is correct
7 Correct 12 ms 480 KB Output is correct
8 Correct 12 ms 468 KB Output is correct
9 Correct 17 ms 560 KB Output is correct
10 Correct 15 ms 508 KB Output is correct
11 Correct 103 ms 3096 KB Output is correct
12 Correct 102 ms 2996 KB Output is correct
13 Correct 75 ms 2108 KB Output is correct
14 Correct 26 ms 528 KB Output is correct
15 Correct 23 ms 504 KB Output is correct
16 Correct 1 ms 468 KB Output is correct
17 Correct 0 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 0 ms 340 KB Output is correct
20 Correct 0 ms 340 KB Output is correct
21 Correct 0 ms 340 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 856 ms 4836 KB Output is correct
24 Correct 804 ms 4928 KB Output is correct
25 Correct 524 ms 4744 KB Output is correct
26 Correct 921 ms 4896 KB Output is correct
27 Correct 458 ms 4756 KB Output is correct
28 Correct 762 ms 4828 KB Output is correct
29 Correct 755 ms 4904 KB Output is correct
30 Correct 757 ms 4744 KB Output is correct
31 Correct 380 ms 4688 KB Output is correct
32 Correct 387 ms 4664 KB Output is correct
33 Correct 311 ms 4648 KB Output is correct
34 Correct 401 ms 4820 KB Output is correct
35 Correct 888 ms 4992 KB Output is correct
36 Correct 882 ms 4684 KB Output is correct
37 Correct 528 ms 4656 KB Output is correct
38 Correct 882 ms 4756 KB Output is correct
39 Correct 454 ms 4916 KB Output is correct
40 Correct 753 ms 4940 KB Output is correct
41 Correct 758 ms 4768 KB Output is correct
42 Correct 759 ms 4976 KB Output is correct
43 Correct 2102 ms 8304 KB Output is correct
44 Correct 2089 ms 8200 KB Output is correct
45 Correct 1530 ms 6668 KB Output is correct
46 Correct 2140 ms 8096 KB Output is correct
47 Correct 2051 ms 8216 KB Output is correct
48 Correct 5940 ms 27220 KB Output is correct
49 Correct 4988 ms 20788 KB Output is correct
50 Correct 4866 ms 21100 KB Output is correct
51 Correct 4875 ms 21736 KB Output is correct
52 Correct 2756 ms 12452 KB Output is correct
53 Correct 2672 ms 11796 KB Output is correct
54 Correct 5173 ms 25772 KB Output is correct
55 Correct 4536 ms 20788 KB Output is correct
56 Correct 4469 ms 21940 KB Output is correct
57 Correct 4464 ms 22104 KB Output is correct
58 Correct 3869 ms 29744 KB Output is correct
59 Correct 3838 ms 23640 KB Output is correct
60 Correct 8000 ms 35132 KB Output is correct
61 Correct 5535 ms 20968 KB Output is correct
62 Correct 5667 ms 20160 KB Output is correct
63 Correct 5729 ms 25128 KB Output is correct
64 Correct 1092 ms 7288 KB Output is correct
65 Correct 845 ms 6404 KB Output is correct
66 Correct 850 ms 6640 KB Output is correct
67 Correct 1905 ms 116708 KB Output is correct
68 Correct 2249 ms 91648 KB Output is correct
69 Correct 2334 ms 92296 KB Output is correct
70 Correct 2346 ms 94676 KB Output is correct