Submission #964841

# Submission time Handle Problem Language Result Execution time Memory
964841 2024-04-17T16:07:46 Z efedmrlr Rope (JOI17_rope) C++17
100 / 100
1099 ms 143320 KB
// #pragma GCC optimize("O3,Ofast,unroll-loops")
// #pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#include <bits/stdc++.h>

using namespace std;


#define lli long long int
#define MP make_pair
#define pb push_back
#define REP(i,n) for(int i = 0; (i) < (n); (i)++)
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()


void fastio() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
}


const double EPS = 0.00001;
const int INF = 1e9+500;
const int N = 3e5+5;
const int ALPH = 26;
const int LGN = 25;
constexpr int MOD = 1e9+7;

struct SegT {
    vector<int> dt;
    int n;
    void reset(int s) {
        n = s;
        dt.assign(2*n, 0);
    }
    void build() {
        for(int i = n - 1; i>=1; i--) {
            dt[i] = max(dt[i << 1], dt[i << 1 ^ 1]);
        }
    }

    void update(int ind, int val) {
        ind += n;
        for(dt[ind] += val; ind > 1; ind >>= 1) dt[ind >> 1] = max(dt[ind], dt[ind ^ 1]);          
    }
    int query(int l, int r) {
        // [l, r)
        int ret = 0;
        for(l += n, r += n; l < r; l >>= 1, r >>= 1) {
            if(l & 1) ret = max(ret, dt[l++]);
            if(r & 1) ret = max(ret, dt[--r]); 
        }
        return ret;
    }
    int query() {
        return query(0, n);
    }
};

vector<int> a;
vector<int> cnt;
vector<vector<int> > pr1, pr2;
SegT st;
int n,m,q;

inline void solve() {
    cin>>n>>m;
    a.resize(n + 5);
    cnt.assign(m + 5, 0);
    pr1.assign(m + 5, vector<int>());
    pr2.assign(m + 5, vector<int>());
    st.reset(m + 3);
    for(int i = 1; i<=n; i++) {
        cin >> a[i];
    } 
    for(int i = 1; i<=n; i++) {
        cnt[a[i]]++;
    }
    for(int i = 1; i<n; i+=2) {
        if(a[i] == a[i + 1]) continue;
        pr1[a[i]].pb(a[i + 1]);
        pr1[a[i + 1]].pb(a[i]);
    }
    for(int i = 2; i<n; i+=2) {
        if(a[i] == a[i + 1]) continue; 
        pr2[a[i]].pb(a[i + 1]);
        pr2[a[i + 1]].pb(a[i]);
        
    }
    for(int i = 1; i<=m; i++) {
        st.dt[i + st.n] = cnt[i];
    }
    st.build();
    vector<int> res(m + 1, INF);
    for(int i = 1; i<=m; i++) {
        res[i] = n - cnt[i];
    }
    
    for(int i = 1; i<=m; i++) {
        // Case 1
        for(auto c : pr1[i]) {
            st.update(c, -1);
        }
        st.update(i, -cnt[i]);
        res[i] = min(res[i], n - (cnt[i] + st.query()));

        for(auto c : pr1[i]) {
            st.update(c, 1);
        }

        // Case 2
        for(auto c : pr2[i]) {
            st.update(c, -1);
        }
        res[i] = min(res[i], n - (cnt[i] + st.query()));
        st.update(i, cnt[i]);
        for(auto c : pr2[i]) {
            st.update(c, 1);
        }
    }
    for(int i = 1; i<=m; i++) {
        cout << res[i] << "\n";
    }
}
 
signed main() {

    fastio();
    int test = 1;
    //cin>>test;
    while(test--) {
        solve();
    }
    
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 452 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 452 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 1 ms 344 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 1 ms 344 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 0 ms 344 KB Output is correct
23 Correct 1 ms 344 KB Output is correct
24 Correct 1 ms 344 KB Output is correct
25 Correct 1 ms 600 KB Output is correct
26 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 452 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 452 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 1 ms 344 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 1 ms 344 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 0 ms 344 KB Output is correct
23 Correct 1 ms 344 KB Output is correct
24 Correct 1 ms 344 KB Output is correct
25 Correct 1 ms 600 KB Output is correct
26 Correct 0 ms 348 KB Output is correct
27 Correct 7 ms 2216 KB Output is correct
28 Correct 6 ms 1880 KB Output is correct
29 Correct 8 ms 2100 KB Output is correct
30 Correct 6 ms 1628 KB Output is correct
31 Correct 7 ms 2152 KB Output is correct
32 Correct 7 ms 1748 KB Output is correct
33 Correct 8 ms 2032 KB Output is correct
34 Correct 6 ms 1628 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 452 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 452 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 1 ms 344 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 1 ms 344 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 0 ms 344 KB Output is correct
23 Correct 1 ms 344 KB Output is correct
24 Correct 1 ms 344 KB Output is correct
25 Correct 1 ms 600 KB Output is correct
26 Correct 0 ms 348 KB Output is correct
27 Correct 7 ms 2216 KB Output is correct
28 Correct 6 ms 1880 KB Output is correct
29 Correct 8 ms 2100 KB Output is correct
30 Correct 6 ms 1628 KB Output is correct
31 Correct 7 ms 2152 KB Output is correct
32 Correct 7 ms 1748 KB Output is correct
33 Correct 8 ms 2032 KB Output is correct
34 Correct 6 ms 1628 KB Output is correct
35 Correct 11 ms 2396 KB Output is correct
36 Correct 11 ms 2396 KB Output is correct
37 Correct 12 ms 2344 KB Output is correct
38 Correct 11 ms 2140 KB Output is correct
39 Correct 14 ms 2260 KB Output is correct
40 Correct 11 ms 2392 KB Output is correct
41 Correct 11 ms 2568 KB Output is correct
42 Correct 14 ms 2396 KB Output is correct
43 Correct 14 ms 2508 KB Output is correct
44 Correct 12 ms 2548 KB Output is correct
45 Correct 11 ms 2396 KB Output is correct
46 Correct 11 ms 2396 KB Output is correct
47 Correct 11 ms 2396 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 452 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 452 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 1 ms 344 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 1 ms 344 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 0 ms 344 KB Output is correct
23 Correct 1 ms 344 KB Output is correct
24 Correct 1 ms 344 KB Output is correct
25 Correct 1 ms 600 KB Output is correct
26 Correct 0 ms 348 KB Output is correct
27 Correct 7 ms 2216 KB Output is correct
28 Correct 6 ms 1880 KB Output is correct
29 Correct 8 ms 2100 KB Output is correct
30 Correct 6 ms 1628 KB Output is correct
31 Correct 7 ms 2152 KB Output is correct
32 Correct 7 ms 1748 KB Output is correct
33 Correct 8 ms 2032 KB Output is correct
34 Correct 6 ms 1628 KB Output is correct
35 Correct 11 ms 2396 KB Output is correct
36 Correct 11 ms 2396 KB Output is correct
37 Correct 12 ms 2344 KB Output is correct
38 Correct 11 ms 2140 KB Output is correct
39 Correct 14 ms 2260 KB Output is correct
40 Correct 11 ms 2392 KB Output is correct
41 Correct 11 ms 2568 KB Output is correct
42 Correct 14 ms 2396 KB Output is correct
43 Correct 14 ms 2508 KB Output is correct
44 Correct 12 ms 2548 KB Output is correct
45 Correct 11 ms 2396 KB Output is correct
46 Correct 11 ms 2396 KB Output is correct
47 Correct 11 ms 2396 KB Output is correct
48 Correct 131 ms 22180 KB Output is correct
49 Correct 137 ms 22076 KB Output is correct
50 Correct 139 ms 22272 KB Output is correct
51 Correct 129 ms 21584 KB Output is correct
52 Correct 103 ms 18764 KB Output is correct
53 Correct 143 ms 20468 KB Output is correct
54 Correct 126 ms 18624 KB Output is correct
55 Correct 134 ms 18256 KB Output is correct
56 Correct 133 ms 17760 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 452 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 452 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 1 ms 344 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 1 ms 344 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 0 ms 344 KB Output is correct
23 Correct 1 ms 344 KB Output is correct
24 Correct 1 ms 344 KB Output is correct
25 Correct 1 ms 600 KB Output is correct
26 Correct 0 ms 348 KB Output is correct
27 Correct 7 ms 2216 KB Output is correct
28 Correct 6 ms 1880 KB Output is correct
29 Correct 8 ms 2100 KB Output is correct
30 Correct 6 ms 1628 KB Output is correct
31 Correct 7 ms 2152 KB Output is correct
32 Correct 7 ms 1748 KB Output is correct
33 Correct 8 ms 2032 KB Output is correct
34 Correct 6 ms 1628 KB Output is correct
35 Correct 11 ms 2396 KB Output is correct
36 Correct 11 ms 2396 KB Output is correct
37 Correct 12 ms 2344 KB Output is correct
38 Correct 11 ms 2140 KB Output is correct
39 Correct 14 ms 2260 KB Output is correct
40 Correct 11 ms 2392 KB Output is correct
41 Correct 11 ms 2568 KB Output is correct
42 Correct 14 ms 2396 KB Output is correct
43 Correct 14 ms 2508 KB Output is correct
44 Correct 12 ms 2548 KB Output is correct
45 Correct 11 ms 2396 KB Output is correct
46 Correct 11 ms 2396 KB Output is correct
47 Correct 11 ms 2396 KB Output is correct
48 Correct 131 ms 22180 KB Output is correct
49 Correct 137 ms 22076 KB Output is correct
50 Correct 139 ms 22272 KB Output is correct
51 Correct 129 ms 21584 KB Output is correct
52 Correct 103 ms 18764 KB Output is correct
53 Correct 143 ms 20468 KB Output is correct
54 Correct 126 ms 18624 KB Output is correct
55 Correct 134 ms 18256 KB Output is correct
56 Correct 133 ms 17760 KB Output is correct
57 Correct 1099 ms 143320 KB Output is correct
58 Correct 824 ms 90380 KB Output is correct
59 Correct 770 ms 90192 KB Output is correct
60 Correct 897 ms 103496 KB Output is correct
61 Correct 894 ms 103720 KB Output is correct
62 Correct 396 ms 56572 KB Output is correct
63 Correct 561 ms 71612 KB Output is correct
64 Correct 498 ms 62312 KB Output is correct
65 Correct 267 ms 38424 KB Output is correct