Submission #791725

# Submission time Handle Problem Language Result Execution time Memory
791725 2023-07-24T09:08:16 Z 이동현(#10049) Sličnost (COI23_slicnost) C++17
100 / 100
1762 ms 318684 KB
#include <bits/stdc++.h>
#define mi(x, y) (x = min(x, y))
#define ma(x, y) (x = max(x, y))
using namespace std;

const int SZ = (int)15000000, NS = (int)1e5 + 4;
struct Data{
    int l, r, mx, lazy, cnt;
    Data(){
        l = r = mx = lazy = cnt = 0;
    }
};
Data tr[SZ];
int tn;
int n, k, q;
int yl[NS], yr[NS];
int a[NS], b[NS];
int rt[NS];
long long cntsum[NS];

void build(int x, int s, int e){
    if(s == e){
        tr[x].cnt = 1;
        return;
    }
    int m = s + e >> 1;
    tr[x].l = ++tn;
    build(tr[x].l, s, m);
    tr[x].r = ++tn;
    build(tr[x].r, m + 1, e);
    tr[x].cnt = e - s + 1;
}

void push(int pre, int now, int s, int e, int ps, int pe, int val){
    // cout << "PUSH " << pre << ' ' << now << ' ' << s << ' ' << e << ' ' << ps << ' ' << pe << ' ' << val << endl;

    if(ps <= s && e <= pe){
        tr[now].mx += val;
        tr[now].lazy += val;
        return;
    }

    int m = s + e >> 1;
    if(tr[now].lazy || ps <= m){
        tr[now].l = ++tn;
        tr[tn] = tr[tr[pre].l];
        tr[tn].mx += tr[now].lazy;
        tr[tn].lazy += tr[now].lazy;
    }
    if(tr[now].lazy || pe > m){
        tr[now].r = ++tn;
        tr[tn] = tr[tr[pre].r];
        tr[tn].mx += tr[now].lazy;
        tr[tn].lazy += tr[now].lazy;
    }
    tr[now].lazy = 0;
    if(ps <= m){
        push(tr[pre].l, tr[now].l, s, m, ps, pe, val);
    }
    if(pe > m){
        push(tr[pre].r, tr[now].r, m + 1, e, ps, pe, val);
    }

    tr[now].mx = max(tr[tr[now].l].mx, tr[tr[now].r].mx);
    tr[now].cnt = (tr[now].mx == tr[tr[now].l].mx ? tr[tr[now].l].cnt : 0) + (tr[now].mx == tr[tr[now].r].mx ? tr[tr[now].r].cnt : 0);
}

signed main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);

    cin >> n >> k >> q;
    for(int i = 0; i < n; ++i){
        cin >> a[i];
        --a[i];
    }
    for(int i = 0; i < n; ++i){
        cin >> b[i];
        --b[i];
    }
    multiset<int> st;
    auto init = [&](){
        for(int i = 0; i < n; ++i){
            yl[b[i]] = max(0, i - k + 1);
            yr[b[i]] = min(i, n - k);
        }
        vector<vector<vector<int>>> add(n - k + 2);
        for(int i = 0; i < n; ++i){
            add[max(0, i - k + 1)].push_back({yl[a[i]], yr[a[i]], 1});
            add[min(i, n - k) + 1].push_back({yl[a[i]], yr[a[i]], -1});
        }

        ++tn;
        build(1, 0, n - k);
        int pre = 1;
        for(int i = 0; i <= n - k; ++i){
            for(auto&j:add[i]){
                // cout << i << ' ' << j[0] << ' ' << j[1] << ' ' << j[2] << endl;
                rt[i] = ++tn;
                tr[tn] = tr[pre];
                push(pre, rt[i], 0, n - k, j[0], j[1], j[2]);
                // cout << tr[rt[i]].mx << ' ' << tr[rt[i]].cnt << endl;
                pre = rt[i];
            }
        }

        st.clear();
        memset(cntsum, 0, sizeof(cntsum));
        for(int i = 0; i <= n - k; ++i){
            st.insert(tr[rt[i]].mx);
            cntsum[tr[rt[i]].mx] += tr[rt[i]].cnt;
        }
    };
    init();

    auto output = [&](){
        int ans1 = *(--st.end());
        cout << ans1 << ' ' << cntsum[ans1] << '\n';
    };
    output();

    for(int i = 0; i < q; ++i){
        int x;
        cin >> x;
        --x;

        int lp = x - k + 1;
        int rp = x + 1;

        if(lp >= 0){
            st.erase(st.find(tr[rt[lp]].mx));
            cntsum[tr[rt[lp]].mx] -= tr[rt[lp]].cnt;

            int lst = rt[lp];
            tr[++tn] = tr[rt[lp]];
            push(lst, rt[lp] = tn, 0, n - k, yl[a[x]], yr[a[x]], -1);
            lst = rt[lp];
            tr[++tn] = tr[rt[lp]];
            push(lst, rt[lp] = tn, 0, n - k, yl[a[x + 1]], yr[a[x + 1]], 1);

            st.insert(tr[rt[lp]].mx);
            cntsum[tr[rt[lp]].mx] += tr[rt[lp]].cnt;
        }

        if(rp <= n - k){
            st.erase(st.find(tr[rt[rp]].mx));
            cntsum[tr[rt[rp]].mx] -= tr[rt[rp]].cnt;

            int lst = rt[rp];
            tr[++tn] = tr[rt[rp]];
            push(lst, rt[rp] = tn, 0, n - k, yl[a[x]], yr[a[x]], 1);
            lst = rt[rp];
            tr[++tn] = tr[rt[rp]];
            push(lst, rt[rp] = tn, 0, n - k, yl[a[x + 1]], yr[a[x + 1]], -1);

            st.insert(tr[rt[rp]].mx);
            cntsum[tr[rt[rp]].mx] += tr[rt[rp]].cnt;
        }

        swap(a[x], a[x + 1]);

        output();
        
        if(tn > SZ - (int)1e5){
            tn = 0;
            init();
        }
    }
    
    return 0;
}

Compilation message

Main.cpp: In function 'void build(int, int, int)':
Main.cpp:26:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   26 |     int m = s + e >> 1;
      |             ~~^~~
Main.cpp: In function 'void push(int, int, int, int, int, int, int)':
Main.cpp:43:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   43 |     int m = s + e >> 1;
      |             ~~^~~
# Verdict Execution time Memory Grader output
1 Correct 122 ms 294604 KB Output is correct
2 Correct 123 ms 294564 KB Output is correct
3 Correct 116 ms 294564 KB Output is correct
4 Correct 119 ms 294648 KB Output is correct
5 Correct 116 ms 294664 KB Output is correct
6 Correct 116 ms 294648 KB Output is correct
7 Correct 119 ms 294648 KB Output is correct
8 Correct 119 ms 294580 KB Output is correct
9 Correct 117 ms 294664 KB Output is correct
10 Correct 117 ms 294604 KB Output is correct
11 Correct 116 ms 294608 KB Output is correct
12 Correct 117 ms 294596 KB Output is correct
13 Correct 118 ms 294604 KB Output is correct
14 Correct 118 ms 294644 KB Output is correct
15 Correct 122 ms 294620 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 122 ms 294604 KB Output is correct
2 Correct 123 ms 294564 KB Output is correct
3 Correct 116 ms 294564 KB Output is correct
4 Correct 119 ms 294648 KB Output is correct
5 Correct 116 ms 294664 KB Output is correct
6 Correct 116 ms 294648 KB Output is correct
7 Correct 119 ms 294648 KB Output is correct
8 Correct 119 ms 294580 KB Output is correct
9 Correct 117 ms 294664 KB Output is correct
10 Correct 117 ms 294604 KB Output is correct
11 Correct 116 ms 294608 KB Output is correct
12 Correct 117 ms 294596 KB Output is correct
13 Correct 118 ms 294604 KB Output is correct
14 Correct 118 ms 294644 KB Output is correct
15 Correct 122 ms 294620 KB Output is correct
16 Correct 124 ms 295648 KB Output is correct
17 Correct 128 ms 295720 KB Output is correct
18 Correct 122 ms 295428 KB Output is correct
19 Correct 123 ms 295500 KB Output is correct
20 Correct 130 ms 295728 KB Output is correct
21 Correct 123 ms 295668 KB Output is correct
22 Correct 118 ms 295340 KB Output is correct
23 Correct 123 ms 295624 KB Output is correct
24 Correct 133 ms 295652 KB Output is correct
25 Correct 122 ms 295452 KB Output is correct
26 Correct 120 ms 295600 KB Output is correct
27 Correct 158 ms 295596 KB Output is correct
28 Correct 125 ms 295424 KB Output is correct
29 Correct 117 ms 295372 KB Output is correct
30 Correct 115 ms 295380 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 122 ms 294604 KB Output is correct
2 Correct 123 ms 294564 KB Output is correct
3 Correct 116 ms 294564 KB Output is correct
4 Correct 119 ms 294648 KB Output is correct
5 Correct 116 ms 294664 KB Output is correct
6 Correct 116 ms 294648 KB Output is correct
7 Correct 119 ms 294648 KB Output is correct
8 Correct 119 ms 294580 KB Output is correct
9 Correct 117 ms 294664 KB Output is correct
10 Correct 117 ms 294604 KB Output is correct
11 Correct 116 ms 294608 KB Output is correct
12 Correct 117 ms 294596 KB Output is correct
13 Correct 118 ms 294604 KB Output is correct
14 Correct 118 ms 294644 KB Output is correct
15 Correct 122 ms 294620 KB Output is correct
16 Correct 124 ms 295648 KB Output is correct
17 Correct 128 ms 295720 KB Output is correct
18 Correct 122 ms 295428 KB Output is correct
19 Correct 123 ms 295500 KB Output is correct
20 Correct 130 ms 295728 KB Output is correct
21 Correct 123 ms 295668 KB Output is correct
22 Correct 118 ms 295340 KB Output is correct
23 Correct 123 ms 295624 KB Output is correct
24 Correct 133 ms 295652 KB Output is correct
25 Correct 122 ms 295452 KB Output is correct
26 Correct 120 ms 295600 KB Output is correct
27 Correct 158 ms 295596 KB Output is correct
28 Correct 125 ms 295424 KB Output is correct
29 Correct 117 ms 295372 KB Output is correct
30 Correct 115 ms 295380 KB Output is correct
31 Correct 245 ms 316652 KB Output is correct
32 Correct 276 ms 316412 KB Output is correct
33 Correct 141 ms 307812 KB Output is correct
34 Correct 234 ms 312316 KB Output is correct
35 Correct 334 ms 316752 KB Output is correct
36 Correct 257 ms 315668 KB Output is correct
37 Correct 146 ms 308592 KB Output is correct
38 Correct 345 ms 316216 KB Output is correct
39 Correct 262 ms 316892 KB Output is correct
40 Correct 250 ms 312712 KB Output is correct
41 Correct 279 ms 314236 KB Output is correct
42 Correct 339 ms 315588 KB Output is correct
43 Correct 227 ms 312492 KB Output is correct
44 Correct 204 ms 311324 KB Output is correct
45 Correct 162 ms 308392 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 122 ms 294604 KB Output is correct
2 Correct 123 ms 294564 KB Output is correct
3 Correct 116 ms 294564 KB Output is correct
4 Correct 119 ms 294648 KB Output is correct
5 Correct 116 ms 294664 KB Output is correct
6 Correct 116 ms 294648 KB Output is correct
7 Correct 119 ms 294648 KB Output is correct
8 Correct 119 ms 294580 KB Output is correct
9 Correct 117 ms 294664 KB Output is correct
10 Correct 117 ms 294604 KB Output is correct
11 Correct 116 ms 294608 KB Output is correct
12 Correct 117 ms 294596 KB Output is correct
13 Correct 118 ms 294604 KB Output is correct
14 Correct 118 ms 294644 KB Output is correct
15 Correct 122 ms 294620 KB Output is correct
16 Correct 123 ms 294564 KB Output is correct
17 Correct 117 ms 294656 KB Output is correct
18 Correct 114 ms 294608 KB Output is correct
19 Correct 109 ms 294672 KB Output is correct
20 Correct 124 ms 294556 KB Output is correct
21 Correct 120 ms 294648 KB Output is correct
22 Correct 120 ms 294548 KB Output is correct
23 Correct 118 ms 294592 KB Output is correct
24 Correct 118 ms 294628 KB Output is correct
25 Correct 122 ms 294696 KB Output is correct
26 Correct 118 ms 294648 KB Output is correct
27 Correct 116 ms 294664 KB Output is correct
28 Correct 118 ms 294640 KB Output is correct
29 Correct 119 ms 294556 KB Output is correct
30 Correct 117 ms 294552 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 122 ms 294604 KB Output is correct
2 Correct 123 ms 294564 KB Output is correct
3 Correct 116 ms 294564 KB Output is correct
4 Correct 119 ms 294648 KB Output is correct
5 Correct 116 ms 294664 KB Output is correct
6 Correct 116 ms 294648 KB Output is correct
7 Correct 119 ms 294648 KB Output is correct
8 Correct 119 ms 294580 KB Output is correct
9 Correct 117 ms 294664 KB Output is correct
10 Correct 117 ms 294604 KB Output is correct
11 Correct 116 ms 294608 KB Output is correct
12 Correct 117 ms 294596 KB Output is correct
13 Correct 118 ms 294604 KB Output is correct
14 Correct 118 ms 294644 KB Output is correct
15 Correct 122 ms 294620 KB Output is correct
16 Correct 124 ms 295648 KB Output is correct
17 Correct 128 ms 295720 KB Output is correct
18 Correct 122 ms 295428 KB Output is correct
19 Correct 123 ms 295500 KB Output is correct
20 Correct 130 ms 295728 KB Output is correct
21 Correct 123 ms 295668 KB Output is correct
22 Correct 118 ms 295340 KB Output is correct
23 Correct 123 ms 295624 KB Output is correct
24 Correct 133 ms 295652 KB Output is correct
25 Correct 122 ms 295452 KB Output is correct
26 Correct 120 ms 295600 KB Output is correct
27 Correct 158 ms 295596 KB Output is correct
28 Correct 125 ms 295424 KB Output is correct
29 Correct 117 ms 295372 KB Output is correct
30 Correct 115 ms 295380 KB Output is correct
31 Correct 123 ms 294564 KB Output is correct
32 Correct 117 ms 294656 KB Output is correct
33 Correct 114 ms 294608 KB Output is correct
34 Correct 109 ms 294672 KB Output is correct
35 Correct 124 ms 294556 KB Output is correct
36 Correct 120 ms 294648 KB Output is correct
37 Correct 120 ms 294548 KB Output is correct
38 Correct 118 ms 294592 KB Output is correct
39 Correct 118 ms 294628 KB Output is correct
40 Correct 122 ms 294696 KB Output is correct
41 Correct 118 ms 294648 KB Output is correct
42 Correct 116 ms 294664 KB Output is correct
43 Correct 118 ms 294640 KB Output is correct
44 Correct 119 ms 294556 KB Output is correct
45 Correct 117 ms 294552 KB Output is correct
46 Correct 130 ms 295708 KB Output is correct
47 Correct 132 ms 295684 KB Output is correct
48 Correct 120 ms 295452 KB Output is correct
49 Correct 127 ms 295504 KB Output is correct
50 Correct 143 ms 295688 KB Output is correct
51 Correct 124 ms 295432 KB Output is correct
52 Correct 130 ms 295672 KB Output is correct
53 Correct 157 ms 295704 KB Output is correct
54 Correct 132 ms 295788 KB Output is correct
55 Correct 127 ms 295512 KB Output is correct
56 Correct 163 ms 295776 KB Output is correct
57 Correct 144 ms 295840 KB Output is correct
58 Correct 123 ms 295452 KB Output is correct
59 Correct 139 ms 295604 KB Output is correct
60 Correct 122 ms 295416 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 122 ms 294604 KB Output is correct
2 Correct 123 ms 294564 KB Output is correct
3 Correct 116 ms 294564 KB Output is correct
4 Correct 119 ms 294648 KB Output is correct
5 Correct 116 ms 294664 KB Output is correct
6 Correct 116 ms 294648 KB Output is correct
7 Correct 119 ms 294648 KB Output is correct
8 Correct 119 ms 294580 KB Output is correct
9 Correct 117 ms 294664 KB Output is correct
10 Correct 117 ms 294604 KB Output is correct
11 Correct 116 ms 294608 KB Output is correct
12 Correct 117 ms 294596 KB Output is correct
13 Correct 118 ms 294604 KB Output is correct
14 Correct 118 ms 294644 KB Output is correct
15 Correct 122 ms 294620 KB Output is correct
16 Correct 124 ms 295648 KB Output is correct
17 Correct 128 ms 295720 KB Output is correct
18 Correct 122 ms 295428 KB Output is correct
19 Correct 123 ms 295500 KB Output is correct
20 Correct 130 ms 295728 KB Output is correct
21 Correct 123 ms 295668 KB Output is correct
22 Correct 118 ms 295340 KB Output is correct
23 Correct 123 ms 295624 KB Output is correct
24 Correct 133 ms 295652 KB Output is correct
25 Correct 122 ms 295452 KB Output is correct
26 Correct 120 ms 295600 KB Output is correct
27 Correct 158 ms 295596 KB Output is correct
28 Correct 125 ms 295424 KB Output is correct
29 Correct 117 ms 295372 KB Output is correct
30 Correct 115 ms 295380 KB Output is correct
31 Correct 245 ms 316652 KB Output is correct
32 Correct 276 ms 316412 KB Output is correct
33 Correct 141 ms 307812 KB Output is correct
34 Correct 234 ms 312316 KB Output is correct
35 Correct 334 ms 316752 KB Output is correct
36 Correct 257 ms 315668 KB Output is correct
37 Correct 146 ms 308592 KB Output is correct
38 Correct 345 ms 316216 KB Output is correct
39 Correct 262 ms 316892 KB Output is correct
40 Correct 250 ms 312712 KB Output is correct
41 Correct 279 ms 314236 KB Output is correct
42 Correct 339 ms 315588 KB Output is correct
43 Correct 227 ms 312492 KB Output is correct
44 Correct 204 ms 311324 KB Output is correct
45 Correct 162 ms 308392 KB Output is correct
46 Correct 123 ms 294564 KB Output is correct
47 Correct 117 ms 294656 KB Output is correct
48 Correct 114 ms 294608 KB Output is correct
49 Correct 109 ms 294672 KB Output is correct
50 Correct 124 ms 294556 KB Output is correct
51 Correct 120 ms 294648 KB Output is correct
52 Correct 120 ms 294548 KB Output is correct
53 Correct 118 ms 294592 KB Output is correct
54 Correct 118 ms 294628 KB Output is correct
55 Correct 122 ms 294696 KB Output is correct
56 Correct 118 ms 294648 KB Output is correct
57 Correct 116 ms 294664 KB Output is correct
58 Correct 118 ms 294640 KB Output is correct
59 Correct 119 ms 294556 KB Output is correct
60 Correct 117 ms 294552 KB Output is correct
61 Correct 130 ms 295708 KB Output is correct
62 Correct 132 ms 295684 KB Output is correct
63 Correct 120 ms 295452 KB Output is correct
64 Correct 127 ms 295504 KB Output is correct
65 Correct 143 ms 295688 KB Output is correct
66 Correct 124 ms 295432 KB Output is correct
67 Correct 130 ms 295672 KB Output is correct
68 Correct 157 ms 295704 KB Output is correct
69 Correct 132 ms 295788 KB Output is correct
70 Correct 127 ms 295512 KB Output is correct
71 Correct 163 ms 295776 KB Output is correct
72 Correct 144 ms 295840 KB Output is correct
73 Correct 123 ms 295452 KB Output is correct
74 Correct 139 ms 295604 KB Output is correct
75 Correct 122 ms 295416 KB Output is correct
76 Correct 665 ms 317004 KB Output is correct
77 Correct 737 ms 316912 KB Output is correct
78 Correct 173 ms 308012 KB Output is correct
79 Correct 504 ms 312532 KB Output is correct
80 Correct 1345 ms 318436 KB Output is correct
81 Correct 206 ms 308976 KB Output is correct
82 Correct 799 ms 314236 KB Output is correct
83 Correct 1366 ms 317332 KB Output is correct
84 Correct 930 ms 318684 KB Output is correct
85 Correct 326 ms 311976 KB Output is correct
86 Correct 1762 ms 317868 KB Output is correct
87 Correct 1696 ms 317396 KB Output is correct
88 Correct 299 ms 310036 KB Output is correct
89 Correct 1120 ms 315820 KB Output is correct
90 Correct 258 ms 309544 KB Output is correct