답안 #595969

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
595969 2022-07-14T08:24:04 Z 이동현(#8445) Sushi (JOI16_sushi) C++17
20 / 100
12000 ms 39164 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;
multiset<int> num[BS];
multiset<int> que[BS];
int ask[25004][3], lst[BS];

signed main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    int B = 700;
    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].insert(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;
        if(ask[i][0] > ask[i][1]) lst[0] = lst[(n - 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);
                if((int)que[bp].size() == 1){
                    int val = *que[bp].begin();
                    for(int i = l; i < r; ++i) if(val < a[i]) swap(val, a[i]);
                }
                else if((int)que[bp].size() == 2){
                    int v1 = *que[bp].begin(), v2 = *(--que[bp].end());
                    for(int i = l; i < r; ++i){
                        if(v1 < a[i]) swap(v1, a[i]);
                        if(v2 < a[i]) swap(v2, a[i]);
                    }
                }
                else if((int)que[bp].size() == 3){
                    int v1 = *que[bp].begin(), v2 = *(--(--que[bp].end())), v3 = *(--que[bp].end());
                    for(int i = l; i < r; ++i){
                        if(v1 < a[i]) swap(v1, a[i]);
                        if(v2 < a[i]) swap(v2, a[i]);
                        if(v3 < a[i]) swap(v3, a[i]);
                    }
                }
                else if((int)que[bp].size() == 4){
                    int v1 = *que[bp].begin(), v2 = *(++que[bp].begin()), v3 = *(--(--que[bp].end())), v4 = *(--que[bp].end());
                    for(int i = l; i < r; ++i){
                        if(v1 < a[i]) swap(v1, a[i]);
                        if(v2 < a[i]) swap(v2, a[i]);
                        if(v3 < a[i]) swap(v3, a[i]);
                        if(v4 < a[i]) swap(v4, a[i]);
                    }
                }
                else{
                    for(int i = l; i < r; ++i){
                        que[bp].insert(a[i]);
                        a[i] = *(que[bp].begin());
                        que[bp].erase(que[bp].begin());
                    }
                }
                que[bp].clear();
            };
            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){
                        auto p = num[xb].lower_bound(a[i]);
                        num[xb].erase(p);
                        swap(a[i], k);
                        num[xb].insert(a[i]);
                    }
                }
                for(int i = xb + 1; i < yb; ++i){
                    if(lst[i] > qq) que[i].insert(k);
                    if(k < *(--num[i].end())){
                        num[i].insert(k);
                        k = *(--num[i].end());
                        num[i].erase(--num[i].end());
                    }
                }
                for(int i = yl; i <= y; ++i){
                    if(a[i] > k){
                        auto p = num[yb].lower_bound(a[i]);
                        num[yb].erase(p);
                        swap(a[i], k);
                        num[yb].insert(a[i]);
                    }
                }
            }
            else{
                for(int i = x; i <= y; ++i){
                    if(a[i] > k){
                        auto p = num[xb].lower_bound(a[i]);
                        num[xb].erase(p);
                        swap(a[i], k);
                        num[xb].insert(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;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 596 KB Output is correct
2 Correct 8 ms 596 KB Output is correct
3 Correct 8 ms 596 KB Output is correct
4 Correct 8 ms 596 KB Output is correct
5 Correct 7 ms 596 KB Output is correct
6 Correct 9 ms 548 KB Output is correct
7 Correct 3 ms 468 KB Output is correct
8 Correct 5 ms 468 KB Output is correct
9 Correct 9 ms 564 KB Output is correct
10 Correct 8 ms 596 KB Output is correct
11 Correct 59 ms 608 KB Output is correct
12 Correct 58 ms 596 KB Output is correct
13 Correct 50 ms 612 KB Output is correct
14 Correct 16 ms 596 KB Output is correct
15 Correct 13 ms 564 KB Output is correct
16 Correct 2 ms 596 KB Output is correct
17 Correct 0 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 420 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5469 ms 25476 KB Output is correct
2 Correct 5339 ms 24796 KB Output is correct
3 Correct 135 ms 24056 KB Output is correct
4 Correct 5718 ms 25540 KB Output is correct
5 Correct 4572 ms 25480 KB Output is correct
6 Correct 5330 ms 25576 KB Output is correct
7 Correct 4973 ms 25620 KB Output is correct
8 Correct 4902 ms 25608 KB Output is correct
9 Correct 166 ms 24012 KB Output is correct
10 Correct 4354 ms 24676 KB Output is correct
11 Correct 147 ms 23432 KB Output is correct
12 Correct 4435 ms 23712 KB Output is correct
13 Correct 5561 ms 23636 KB Output is correct
14 Correct 5420 ms 23684 KB Output is correct
15 Correct 130 ms 23444 KB Output is correct
16 Correct 5475 ms 23652 KB Output is correct
17 Correct 4713 ms 23752 KB Output is correct
18 Correct 5054 ms 23824 KB Output is correct
19 Correct 5000 ms 23816 KB Output is correct
20 Correct 4972 ms 23716 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 596 KB Output is correct
2 Correct 8 ms 596 KB Output is correct
3 Correct 8 ms 596 KB Output is correct
4 Correct 8 ms 596 KB Output is correct
5 Correct 7 ms 596 KB Output is correct
6 Correct 9 ms 548 KB Output is correct
7 Correct 3 ms 468 KB Output is correct
8 Correct 5 ms 468 KB Output is correct
9 Correct 9 ms 564 KB Output is correct
10 Correct 8 ms 596 KB Output is correct
11 Correct 59 ms 608 KB Output is correct
12 Correct 58 ms 596 KB Output is correct
13 Correct 50 ms 612 KB Output is correct
14 Correct 16 ms 596 KB Output is correct
15 Correct 13 ms 564 KB Output is correct
16 Correct 2 ms 596 KB Output is correct
17 Correct 0 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 420 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 5469 ms 25476 KB Output is correct
24 Correct 5339 ms 24796 KB Output is correct
25 Correct 135 ms 24056 KB Output is correct
26 Correct 5718 ms 25540 KB Output is correct
27 Correct 4572 ms 25480 KB Output is correct
28 Correct 5330 ms 25576 KB Output is correct
29 Correct 4973 ms 25620 KB Output is correct
30 Correct 4902 ms 25608 KB Output is correct
31 Correct 166 ms 24012 KB Output is correct
32 Correct 4354 ms 24676 KB Output is correct
33 Correct 147 ms 23432 KB Output is correct
34 Correct 4435 ms 23712 KB Output is correct
35 Correct 5561 ms 23636 KB Output is correct
36 Correct 5420 ms 23684 KB Output is correct
37 Correct 130 ms 23444 KB Output is correct
38 Correct 5475 ms 23652 KB Output is correct
39 Correct 4713 ms 23752 KB Output is correct
40 Correct 5054 ms 23824 KB Output is correct
41 Correct 5000 ms 23816 KB Output is correct
42 Correct 4972 ms 23716 KB Output is correct
43 Correct 4396 ms 25724 KB Output is correct
44 Correct 4791 ms 25556 KB Output is correct
45 Correct 4478 ms 25372 KB Output is correct
46 Correct 4474 ms 25556 KB Output is correct
47 Correct 4157 ms 25824 KB Output is correct
48 Correct 11016 ms 27288 KB Output is correct
49 Correct 10620 ms 27292 KB Output is correct
50 Correct 10405 ms 26992 KB Output is correct
51 Correct 10532 ms 27012 KB Output is correct
52 Correct 9679 ms 39164 KB Output is correct
53 Correct 9192 ms 38220 KB Output is correct
54 Execution timed out 12041 ms 38112 KB Time limit exceeded
55 Halted 0 ms 0 KB -