Submission #809696

# Submission time Handle Problem Language Result Execution time Memory
809696 2023-08-06T03:03:55 Z QwertyPi Rope (JOI17_rope) C++14
80 / 100
2500 ms 91608 KB
#include <bits/stdc++.h>
#define int long long
using namespace std;

const int MAXN = 1e6 + 11;

int c[MAXN];
vector<int> ci[MAXN];
bool in[MAXN];
int n, m;

int a[MAXN], cnt[MAXN], cnt2[MAXN];
int c1, c2;

struct BIT{
    int bit[MAXN];
    void add(int p, int v){ p++; for(int i = p; i < MAXN; i += i & -i) bit[i] += v; }
    int qry(int p){ p++; int r = 0; for(int i = p; i; i -= i & -i) r += bit[i]; return r; }
    int search(int v){ int r = 0, s = 0; for(int j = 19; j >= 0; j--) if(r + (1 << j) < MAXN && s + bit[r + (1 << j)] <= v) s += bit[r + (1 << j)], r += (1 << j); return r; }
} bit;

void add(int x){
    bit.add(cnt[x], -1); cnt[x]++; bit.add(cnt[x], 1);
}

void rem(int x){
    bit.add(cnt[x], -1); cnt[x]--; bit.add(cnt[x], 1);
}

void build(int c[]){
    cnt2[0] = m; bit.add(0, m);
    for(int i = 0; i < n; i++) add(c[i]);
}

void upd(int pos, int val){
    add(val); rem(a[pos]); a[pos] = val;
}

int qry(int except){
    int p1 = bit.search(m - 1), p2 = bit.search(m - 2);
    return cnt[except] == p1 ? p2 : p1;
}

bool in_range(int id){
    return 0 <= id && id < n;
}

int32_t main(){
    cin.tie(0); cout.tie(0)->sync_with_stdio(false);
    cin >> n >> m;
    for(int i = 0; i < n; i++){
        cin >> c[i]; c[i]--; ci[c[i]].push_back(i); a[i] = c[i];
    }

    build(c);

    for(int i = 0; i < m; i++){
        vector<int> revert;
        for(auto j : ci[i]) in[j] = true;
    
        // even index start
        int sz = ci[i].size(), l = 0, r = sz - 1;
        for(int k = l; k <= r; k++) {
            int j = ci[i][k]; int nxt = j ^ 1;
            if(in_range(nxt) && !in[nxt]) revert.push_back(nxt), upd(nxt, i), in[nxt] = true;
        }
        int q1 = n - qry(i) - sz;
        for(auto j : revert) in[j] = false, upd(j, c[j]); revert.clear();

        // odd index start
        
        for(int k = l; k <= r; k++) {
            int j = ci[i][k]; int nxt = ((j + 1) ^ 1) - 1;
            if(in_range(nxt) && !in[nxt]) revert.push_back(nxt), upd(nxt, i), in[nxt] = true;
        }

        int q2 = n - qry(i) - sz;
        for(auto j : revert) in[j] = false, upd(j, c[j]); revert.clear();

        int ans = min(q1, q2);
        cout << ans << endl;
        for(auto j : ci[i]) in[j] = false;
    }
}

Compilation message

rope.cpp: In function 'int32_t main()':
rope.cpp:68:9: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
   68 |         for(auto j : revert) in[j] = false, upd(j, c[j]); revert.clear();
      |         ^~~
rope.cpp:68:59: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
   68 |         for(auto j : revert) in[j] = false, upd(j, c[j]); revert.clear();
      |                                                           ^~~~~~
rope.cpp:78:9: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
   78 |         for(auto j : revert) in[j] = false, upd(j, c[j]); revert.clear();
      |         ^~~
rope.cpp:78:59: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
   78 |         for(auto j : revert) in[j] = false, upd(j, c[j]); revert.clear();
      |                                                           ^~~~~~
# Verdict Execution time Memory Grader output
1 Correct 13 ms 23824 KB Output is correct
2 Correct 12 ms 23832 KB Output is correct
3 Correct 14 ms 23824 KB Output is correct
4 Correct 15 ms 23824 KB Output is correct
5 Correct 16 ms 23828 KB Output is correct
6 Correct 13 ms 23892 KB Output is correct
7 Correct 14 ms 23892 KB Output is correct
8 Correct 14 ms 23872 KB Output is correct
9 Correct 12 ms 23824 KB Output is correct
10 Correct 11 ms 23892 KB Output is correct
11 Correct 14 ms 23980 KB Output is correct
12 Correct 12 ms 23892 KB Output is correct
13 Correct 12 ms 23892 KB Output is correct
14 Correct 12 ms 23892 KB Output is correct
15 Correct 12 ms 23824 KB Output is correct
16 Correct 12 ms 23892 KB Output is correct
17 Correct 12 ms 23820 KB Output is correct
18 Correct 12 ms 23872 KB Output is correct
19 Correct 14 ms 23892 KB Output is correct
20 Correct 12 ms 23892 KB Output is correct
21 Correct 12 ms 23820 KB Output is correct
22 Correct 12 ms 23892 KB Output is correct
23 Correct 12 ms 23844 KB Output is correct
24 Correct 14 ms 23852 KB Output is correct
25 Correct 12 ms 23824 KB Output is correct
26 Correct 12 ms 23892 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 23824 KB Output is correct
2 Correct 12 ms 23832 KB Output is correct
3 Correct 14 ms 23824 KB Output is correct
4 Correct 15 ms 23824 KB Output is correct
5 Correct 16 ms 23828 KB Output is correct
6 Correct 13 ms 23892 KB Output is correct
7 Correct 14 ms 23892 KB Output is correct
8 Correct 14 ms 23872 KB Output is correct
9 Correct 12 ms 23824 KB Output is correct
10 Correct 11 ms 23892 KB Output is correct
11 Correct 14 ms 23980 KB Output is correct
12 Correct 12 ms 23892 KB Output is correct
13 Correct 12 ms 23892 KB Output is correct
14 Correct 12 ms 23892 KB Output is correct
15 Correct 12 ms 23824 KB Output is correct
16 Correct 12 ms 23892 KB Output is correct
17 Correct 12 ms 23820 KB Output is correct
18 Correct 12 ms 23872 KB Output is correct
19 Correct 14 ms 23892 KB Output is correct
20 Correct 12 ms 23892 KB Output is correct
21 Correct 12 ms 23820 KB Output is correct
22 Correct 12 ms 23892 KB Output is correct
23 Correct 12 ms 23844 KB Output is correct
24 Correct 14 ms 23852 KB Output is correct
25 Correct 12 ms 23824 KB Output is correct
26 Correct 12 ms 23892 KB Output is correct
27 Correct 64 ms 27260 KB Output is correct
28 Correct 52 ms 27252 KB Output is correct
29 Correct 62 ms 27168 KB Output is correct
30 Correct 58 ms 27088 KB Output is correct
31 Correct 58 ms 27268 KB Output is correct
32 Correct 46 ms 27284 KB Output is correct
33 Correct 55 ms 27132 KB Output is correct
34 Correct 61 ms 27096 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 23824 KB Output is correct
2 Correct 12 ms 23832 KB Output is correct
3 Correct 14 ms 23824 KB Output is correct
4 Correct 15 ms 23824 KB Output is correct
5 Correct 16 ms 23828 KB Output is correct
6 Correct 13 ms 23892 KB Output is correct
7 Correct 14 ms 23892 KB Output is correct
8 Correct 14 ms 23872 KB Output is correct
9 Correct 12 ms 23824 KB Output is correct
10 Correct 11 ms 23892 KB Output is correct
11 Correct 14 ms 23980 KB Output is correct
12 Correct 12 ms 23892 KB Output is correct
13 Correct 12 ms 23892 KB Output is correct
14 Correct 12 ms 23892 KB Output is correct
15 Correct 12 ms 23824 KB Output is correct
16 Correct 12 ms 23892 KB Output is correct
17 Correct 12 ms 23820 KB Output is correct
18 Correct 12 ms 23872 KB Output is correct
19 Correct 14 ms 23892 KB Output is correct
20 Correct 12 ms 23892 KB Output is correct
21 Correct 12 ms 23820 KB Output is correct
22 Correct 12 ms 23892 KB Output is correct
23 Correct 12 ms 23844 KB Output is correct
24 Correct 14 ms 23852 KB Output is correct
25 Correct 12 ms 23824 KB Output is correct
26 Correct 12 ms 23892 KB Output is correct
27 Correct 64 ms 27260 KB Output is correct
28 Correct 52 ms 27252 KB Output is correct
29 Correct 62 ms 27168 KB Output is correct
30 Correct 58 ms 27088 KB Output is correct
31 Correct 58 ms 27268 KB Output is correct
32 Correct 46 ms 27284 KB Output is correct
33 Correct 55 ms 27132 KB Output is correct
34 Correct 61 ms 27096 KB Output is correct
35 Correct 112 ms 27084 KB Output is correct
36 Correct 114 ms 27024 KB Output is correct
37 Correct 112 ms 26936 KB Output is correct
38 Correct 115 ms 26964 KB Output is correct
39 Correct 112 ms 27076 KB Output is correct
40 Correct 98 ms 27248 KB Output is correct
41 Correct 97 ms 27164 KB Output is correct
42 Correct 95 ms 27080 KB Output is correct
43 Correct 100 ms 27160 KB Output is correct
44 Correct 101 ms 27152 KB Output is correct
45 Correct 97 ms 27252 KB Output is correct
46 Correct 100 ms 27060 KB Output is correct
47 Correct 102 ms 27200 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 23824 KB Output is correct
2 Correct 12 ms 23832 KB Output is correct
3 Correct 14 ms 23824 KB Output is correct
4 Correct 15 ms 23824 KB Output is correct
5 Correct 16 ms 23828 KB Output is correct
6 Correct 13 ms 23892 KB Output is correct
7 Correct 14 ms 23892 KB Output is correct
8 Correct 14 ms 23872 KB Output is correct
9 Correct 12 ms 23824 KB Output is correct
10 Correct 11 ms 23892 KB Output is correct
11 Correct 14 ms 23980 KB Output is correct
12 Correct 12 ms 23892 KB Output is correct
13 Correct 12 ms 23892 KB Output is correct
14 Correct 12 ms 23892 KB Output is correct
15 Correct 12 ms 23824 KB Output is correct
16 Correct 12 ms 23892 KB Output is correct
17 Correct 12 ms 23820 KB Output is correct
18 Correct 12 ms 23872 KB Output is correct
19 Correct 14 ms 23892 KB Output is correct
20 Correct 12 ms 23892 KB Output is correct
21 Correct 12 ms 23820 KB Output is correct
22 Correct 12 ms 23892 KB Output is correct
23 Correct 12 ms 23844 KB Output is correct
24 Correct 14 ms 23852 KB Output is correct
25 Correct 12 ms 23824 KB Output is correct
26 Correct 12 ms 23892 KB Output is correct
27 Correct 64 ms 27260 KB Output is correct
28 Correct 52 ms 27252 KB Output is correct
29 Correct 62 ms 27168 KB Output is correct
30 Correct 58 ms 27088 KB Output is correct
31 Correct 58 ms 27268 KB Output is correct
32 Correct 46 ms 27284 KB Output is correct
33 Correct 55 ms 27132 KB Output is correct
34 Correct 61 ms 27096 KB Output is correct
35 Correct 112 ms 27084 KB Output is correct
36 Correct 114 ms 27024 KB Output is correct
37 Correct 112 ms 26936 KB Output is correct
38 Correct 115 ms 26964 KB Output is correct
39 Correct 112 ms 27076 KB Output is correct
40 Correct 98 ms 27248 KB Output is correct
41 Correct 97 ms 27164 KB Output is correct
42 Correct 95 ms 27080 KB Output is correct
43 Correct 100 ms 27160 KB Output is correct
44 Correct 101 ms 27152 KB Output is correct
45 Correct 97 ms 27252 KB Output is correct
46 Correct 100 ms 27060 KB Output is correct
47 Correct 102 ms 27200 KB Output is correct
48 Correct 1130 ms 58280 KB Output is correct
49 Correct 1117 ms 58328 KB Output is correct
50 Correct 1107 ms 58364 KB Output is correct
51 Correct 1006 ms 57952 KB Output is correct
52 Correct 865 ms 55116 KB Output is correct
53 Correct 828 ms 56712 KB Output is correct
54 Correct 711 ms 54688 KB Output is correct
55 Correct 690 ms 54336 KB Output is correct
56 Correct 645 ms 53880 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 23824 KB Output is correct
2 Correct 12 ms 23832 KB Output is correct
3 Correct 14 ms 23824 KB Output is correct
4 Correct 15 ms 23824 KB Output is correct
5 Correct 16 ms 23828 KB Output is correct
6 Correct 13 ms 23892 KB Output is correct
7 Correct 14 ms 23892 KB Output is correct
8 Correct 14 ms 23872 KB Output is correct
9 Correct 12 ms 23824 KB Output is correct
10 Correct 11 ms 23892 KB Output is correct
11 Correct 14 ms 23980 KB Output is correct
12 Correct 12 ms 23892 KB Output is correct
13 Correct 12 ms 23892 KB Output is correct
14 Correct 12 ms 23892 KB Output is correct
15 Correct 12 ms 23824 KB Output is correct
16 Correct 12 ms 23892 KB Output is correct
17 Correct 12 ms 23820 KB Output is correct
18 Correct 12 ms 23872 KB Output is correct
19 Correct 14 ms 23892 KB Output is correct
20 Correct 12 ms 23892 KB Output is correct
21 Correct 12 ms 23820 KB Output is correct
22 Correct 12 ms 23892 KB Output is correct
23 Correct 12 ms 23844 KB Output is correct
24 Correct 14 ms 23852 KB Output is correct
25 Correct 12 ms 23824 KB Output is correct
26 Correct 12 ms 23892 KB Output is correct
27 Correct 64 ms 27260 KB Output is correct
28 Correct 52 ms 27252 KB Output is correct
29 Correct 62 ms 27168 KB Output is correct
30 Correct 58 ms 27088 KB Output is correct
31 Correct 58 ms 27268 KB Output is correct
32 Correct 46 ms 27284 KB Output is correct
33 Correct 55 ms 27132 KB Output is correct
34 Correct 61 ms 27096 KB Output is correct
35 Correct 112 ms 27084 KB Output is correct
36 Correct 114 ms 27024 KB Output is correct
37 Correct 112 ms 26936 KB Output is correct
38 Correct 115 ms 26964 KB Output is correct
39 Correct 112 ms 27076 KB Output is correct
40 Correct 98 ms 27248 KB Output is correct
41 Correct 97 ms 27164 KB Output is correct
42 Correct 95 ms 27080 KB Output is correct
43 Correct 100 ms 27160 KB Output is correct
44 Correct 101 ms 27152 KB Output is correct
45 Correct 97 ms 27252 KB Output is correct
46 Correct 100 ms 27060 KB Output is correct
47 Correct 102 ms 27200 KB Output is correct
48 Correct 1130 ms 58280 KB Output is correct
49 Correct 1117 ms 58328 KB Output is correct
50 Correct 1107 ms 58364 KB Output is correct
51 Correct 1006 ms 57952 KB Output is correct
52 Correct 865 ms 55116 KB Output is correct
53 Correct 828 ms 56712 KB Output is correct
54 Correct 711 ms 54688 KB Output is correct
55 Correct 690 ms 54336 KB Output is correct
56 Correct 645 ms 53880 KB Output is correct
57 Execution timed out 2568 ms 91608 KB Time limit exceeded
58 Halted 0 ms 0 KB -