Submission #1001069

# Submission time Handle Problem Language Result Execution time Memory
1001069 2024-06-18T14:00:29 Z mispertion Rope (JOI17_rope) C++17
100 / 100
2222 ms 210956 KB
#include<bits/stdc++.h>
 
using namespace std;
 
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("O3")

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ll, ll> pii;
typedef pair<ll, ll> pll;
 
#define pb push_back
#define all(x) x.begin(), x.end()
#define sz(x) (int)x.size()
#define mispertion ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define F first
#define S second
#define getlast(s) (*s.rbegin())
#define debg cout << "OK\n"
 
const ld PI = 3.1415926535;
const int N = 1e6+10;
const int M = 7e6 + 1;
int mod = 1e9+7;
const int infi = INT_MAX;
const ll infl = LLONG_MAX;
const int P = 2;
 
int mult(int a, int b) {
    return a * 1LL * b % mod;
}
 
int sum(int a, int b) { 
    if (a + b < 0)
        return a + b + mod;
    if (a + b >= mod)
        return a + b - mod;
    return a + b;
}
 
ll binpow(ll a, ll n) {
    if (n == 0)
        return 1;
    if (n % 2 == 1) {
        return binpow(a, n - 1) * a % mod;
    } else {
        ll b = binpow(a, n / 2);
        return b * b % mod;
    }
}

struct segtree{
    vector<int> t, lz;

    segtree(){

    }

    segtree(int n){
        t.assign(4 * n + 2, 0);
        lz.assign(4 * n + 2, 0);
    }

    void push(int v, int tl, int tr){
        if(tl == tr || !lz[v])
            return;
        lz[2 * v] += lz[v];
        lz[2 * v + 1] += lz[v];
        t[2 * v] += lz[v];
        t[2 * v + 1] += lz[v];
        lz[v] = 0;
    }

    int get(int v, int tl, int tr, int l, int r){
        push(v, tl, tr);
        return t[v];
    }

    void add(int v, int tl, int tr, int l, int r, int x){
        if(l > r)
            return;
        push(v, tl, tr);
        if(tl > r || l > tr)
            return;
        if(l <= tl && tr <= r){
            t[v] += x;
            lz[v] += x;
            return;
        }   
        int tm = (tl + tr) / 2;
        add(2 * v, tl, tm, l, r, x);
        add(2 * v + 1, tm + 1, tr, l, r, x);
        t[v] = min(t[2 * v], t[2 * v + 1]);
    }
    void add(int v, int tl, int tr, int i, int x){
        push(v, tl, tr);
        if(tl == tr){
            t[v] += x;
            return;
        }
        int tm = (tl + tr) / 2;
        if(i <= tm)
            add(2 * v, tl, tm, i, x);
        else
            add(2 * v + 1, tm + 1, tr, i, x);
        t[v] = min(t[2 * v], t[2 * v + 1]);
    }
};

struct answer{
    int n, m;
    vector<int> c;
    vector<vector<int>> ps;
    segtree tr;
    
    answer(){

    }

    answer(int _n, int _m, vector<int> _c){ 
        n = _n;
        m = _m;
        c = _c;
        ps.assign(m + 1, vector<int>());
        for(int i = 1; i <= n; i++){
            ps[c[i]].pb(i);
        }
    }

    void prec(){
        tr = segtree(m);
        for(int i = 1; i <= m; i++){
            tr.add(1, 1, m, i, n - sz(ps[i]));
        }
    }

    int getans(int cl){
        tr.add(1, 1, m, cl, 1e9);
        for(int i : ps[cl]){
            int d;
            if(i % 2)
                d = 1;
            else 
                d = -1;
            if(c[i + d] == cl)
                continue;
            tr.add(1, 1, m, c[i + d], 1);
        }
        int ret = tr.get(1, 1, m, 1, m) - sz(ps[cl]);
        tr.add(1, 1, m, cl, -1e9);
        for(int i : ps[cl]){
            int d;
            if(i % 2)
                d = 1;
            else 
                d = -1;
            if(c[i + d] == cl)
                continue;
            tr.add(1, 1, m, c[i + d], -1);
        }
        return ret;
    }    
};

int n, m, c[N], ans[N], blya[N];
 
void solve(){
    cin >> n >> m;
    for(int i = 1; i <= n; i++){
        cin >> c[i];
        blya[c[i]]++;
    }
    for(int i = 1; i <= m; i++)
        ans[i] = infi;
    int sm = 0;
    vector<int> fc(n - (n % 2) + 1);
    for(int i = 1; i < sz(fc); i++)
        fc[i] = c[i];
    answer fa = answer(n - (n % 2), m, fc);
    fa.prec();
    if(n % 2){
        fa.tr.add(1, 1, m, 1, c[n] - 1, 1);
        fa.tr.add(1, 1, m, c[n] + 1, m, 1);
    }
    answer sa;
    vector<int> sc;
    if(n % 2 == 1){
        sc.resize(n);
        for(int i = 2; i <= n; i++)
            sc[i - 1] = c[i];
        sa = answer(n - 1, m, sc);
        sa.prec();
        sa.tr.add(1, 1, m, 1, c[1] - 1, 1);
        sa.tr.add(1, 1, m, c[1] + 1, m, 1);
    }else{
        sc.resize(n - 1);
        for(int i = 2; i < n; i++)
            sc[i - 1] = c[i];
        sa = answer(n - 2, m, sc);
        sa.prec();
        sa.tr.add(1, 1, m, 1, c[1] - 1, 1);
        sa.tr.add(1, 1, m, c[1] + 1, m, 1);
        sa.tr.add(1, 1, m, 1, c[n] - 1, 1);
        sa.tr.add(1, 1, m, c[n] + 1, m, 1);
    }
    for(int i = 1; i <= m; i++){
        int ans1 = fa.getans(i);
        if(i == c[n] && (n % 2 == 1)){
            ans1--;
        }
        int ans2 = sa.getans(i);
        if(n % 2){
            if(c[1] == i){
                ans2--;
            }
        }else{
            if(c[1] == i){
                ans2--;
            }
            if(c[n] == i){
                ans2--;
            }
        }
        cout << min(min(ans1, ans2), n - blya[i]) << '\n';
    }
}
 
signed main() {
    mispertion;
    int t = 1;
    //cin >> t;
    while(t--){
        solve();
    }
    return 0;
}

Compilation message

rope.cpp: In function 'void solve()':
rope.cpp:178:9: warning: unused variable 'sm' [-Wunused-variable]
  178 |     int sm = 0;
      |         ^~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4444 KB Output is correct
2 Correct 1 ms 4440 KB Output is correct
3 Correct 1 ms 4440 KB Output is correct
4 Correct 1 ms 4444 KB Output is correct
5 Correct 1 ms 4444 KB Output is correct
6 Correct 1 ms 4444 KB Output is correct
7 Correct 1 ms 4444 KB Output is correct
8 Correct 1 ms 4444 KB Output is correct
9 Correct 1 ms 4444 KB Output is correct
10 Correct 1 ms 4444 KB Output is correct
11 Correct 1 ms 4444 KB Output is correct
12 Correct 1 ms 4444 KB Output is correct
13 Correct 1 ms 4444 KB Output is correct
14 Correct 1 ms 4440 KB Output is correct
15 Correct 1 ms 4444 KB Output is correct
16 Correct 1 ms 4444 KB Output is correct
17 Correct 1 ms 4564 KB Output is correct
18 Correct 1 ms 4444 KB Output is correct
19 Correct 1 ms 4444 KB Output is correct
20 Correct 1 ms 4440 KB Output is correct
21 Correct 1 ms 4444 KB Output is correct
22 Correct 1 ms 4444 KB Output is correct
23 Correct 1 ms 4444 KB Output is correct
24 Correct 1 ms 4444 KB Output is correct
25 Correct 1 ms 4444 KB Output is correct
26 Correct 1 ms 4444 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4444 KB Output is correct
2 Correct 1 ms 4440 KB Output is correct
3 Correct 1 ms 4440 KB Output is correct
4 Correct 1 ms 4444 KB Output is correct
5 Correct 1 ms 4444 KB Output is correct
6 Correct 1 ms 4444 KB Output is correct
7 Correct 1 ms 4444 KB Output is correct
8 Correct 1 ms 4444 KB Output is correct
9 Correct 1 ms 4444 KB Output is correct
10 Correct 1 ms 4444 KB Output is correct
11 Correct 1 ms 4444 KB Output is correct
12 Correct 1 ms 4444 KB Output is correct
13 Correct 1 ms 4444 KB Output is correct
14 Correct 1 ms 4440 KB Output is correct
15 Correct 1 ms 4444 KB Output is correct
16 Correct 1 ms 4444 KB Output is correct
17 Correct 1 ms 4564 KB Output is correct
18 Correct 1 ms 4444 KB Output is correct
19 Correct 1 ms 4444 KB Output is correct
20 Correct 1 ms 4440 KB Output is correct
21 Correct 1 ms 4444 KB Output is correct
22 Correct 1 ms 4444 KB Output is correct
23 Correct 1 ms 4444 KB Output is correct
24 Correct 1 ms 4444 KB Output is correct
25 Correct 1 ms 4444 KB Output is correct
26 Correct 1 ms 4444 KB Output is correct
27 Correct 13 ms 7692 KB Output is correct
28 Correct 12 ms 7380 KB Output is correct
29 Correct 18 ms 7736 KB Output is correct
30 Correct 12 ms 7548 KB Output is correct
31 Correct 14 ms 7632 KB Output is correct
32 Correct 14 ms 7636 KB Output is correct
33 Correct 13 ms 7504 KB Output is correct
34 Correct 11 ms 7796 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4444 KB Output is correct
2 Correct 1 ms 4440 KB Output is correct
3 Correct 1 ms 4440 KB Output is correct
4 Correct 1 ms 4444 KB Output is correct
5 Correct 1 ms 4444 KB Output is correct
6 Correct 1 ms 4444 KB Output is correct
7 Correct 1 ms 4444 KB Output is correct
8 Correct 1 ms 4444 KB Output is correct
9 Correct 1 ms 4444 KB Output is correct
10 Correct 1 ms 4444 KB Output is correct
11 Correct 1 ms 4444 KB Output is correct
12 Correct 1 ms 4444 KB Output is correct
13 Correct 1 ms 4444 KB Output is correct
14 Correct 1 ms 4440 KB Output is correct
15 Correct 1 ms 4444 KB Output is correct
16 Correct 1 ms 4444 KB Output is correct
17 Correct 1 ms 4564 KB Output is correct
18 Correct 1 ms 4444 KB Output is correct
19 Correct 1 ms 4444 KB Output is correct
20 Correct 1 ms 4440 KB Output is correct
21 Correct 1 ms 4444 KB Output is correct
22 Correct 1 ms 4444 KB Output is correct
23 Correct 1 ms 4444 KB Output is correct
24 Correct 1 ms 4444 KB Output is correct
25 Correct 1 ms 4444 KB Output is correct
26 Correct 1 ms 4444 KB Output is correct
27 Correct 13 ms 7692 KB Output is correct
28 Correct 12 ms 7380 KB Output is correct
29 Correct 18 ms 7736 KB Output is correct
30 Correct 12 ms 7548 KB Output is correct
31 Correct 14 ms 7632 KB Output is correct
32 Correct 14 ms 7636 KB Output is correct
33 Correct 13 ms 7504 KB Output is correct
34 Correct 11 ms 7796 KB Output is correct
35 Correct 37 ms 7636 KB Output is correct
36 Correct 33 ms 7632 KB Output is correct
37 Correct 32 ms 7636 KB Output is correct
38 Correct 32 ms 7632 KB Output is correct
39 Correct 33 ms 7632 KB Output is correct
40 Correct 24 ms 7636 KB Output is correct
41 Correct 22 ms 7892 KB Output is correct
42 Correct 19 ms 7636 KB Output is correct
43 Correct 30 ms 7636 KB Output is correct
44 Correct 22 ms 7760 KB Output is correct
45 Correct 23 ms 7636 KB Output is correct
46 Correct 20 ms 7632 KB Output is correct
47 Correct 20 ms 7636 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4444 KB Output is correct
2 Correct 1 ms 4440 KB Output is correct
3 Correct 1 ms 4440 KB Output is correct
4 Correct 1 ms 4444 KB Output is correct
5 Correct 1 ms 4444 KB Output is correct
6 Correct 1 ms 4444 KB Output is correct
7 Correct 1 ms 4444 KB Output is correct
8 Correct 1 ms 4444 KB Output is correct
9 Correct 1 ms 4444 KB Output is correct
10 Correct 1 ms 4444 KB Output is correct
11 Correct 1 ms 4444 KB Output is correct
12 Correct 1 ms 4444 KB Output is correct
13 Correct 1 ms 4444 KB Output is correct
14 Correct 1 ms 4440 KB Output is correct
15 Correct 1 ms 4444 KB Output is correct
16 Correct 1 ms 4444 KB Output is correct
17 Correct 1 ms 4564 KB Output is correct
18 Correct 1 ms 4444 KB Output is correct
19 Correct 1 ms 4444 KB Output is correct
20 Correct 1 ms 4440 KB Output is correct
21 Correct 1 ms 4444 KB Output is correct
22 Correct 1 ms 4444 KB Output is correct
23 Correct 1 ms 4444 KB Output is correct
24 Correct 1 ms 4444 KB Output is correct
25 Correct 1 ms 4444 KB Output is correct
26 Correct 1 ms 4444 KB Output is correct
27 Correct 13 ms 7692 KB Output is correct
28 Correct 12 ms 7380 KB Output is correct
29 Correct 18 ms 7736 KB Output is correct
30 Correct 12 ms 7548 KB Output is correct
31 Correct 14 ms 7632 KB Output is correct
32 Correct 14 ms 7636 KB Output is correct
33 Correct 13 ms 7504 KB Output is correct
34 Correct 11 ms 7796 KB Output is correct
35 Correct 37 ms 7636 KB Output is correct
36 Correct 33 ms 7632 KB Output is correct
37 Correct 32 ms 7636 KB Output is correct
38 Correct 32 ms 7632 KB Output is correct
39 Correct 33 ms 7632 KB Output is correct
40 Correct 24 ms 7636 KB Output is correct
41 Correct 22 ms 7892 KB Output is correct
42 Correct 19 ms 7636 KB Output is correct
43 Correct 30 ms 7636 KB Output is correct
44 Correct 22 ms 7760 KB Output is correct
45 Correct 23 ms 7636 KB Output is correct
46 Correct 20 ms 7632 KB Output is correct
47 Correct 20 ms 7636 KB Output is correct
48 Correct 449 ms 40720 KB Output is correct
49 Correct 477 ms 40716 KB Output is correct
50 Correct 433 ms 40928 KB Output is correct
51 Correct 419 ms 40748 KB Output is correct
52 Correct 364 ms 38412 KB Output is correct
53 Correct 299 ms 39284 KB Output is correct
54 Correct 243 ms 37132 KB Output is correct
55 Correct 244 ms 37004 KB Output is correct
56 Correct 230 ms 36364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4444 KB Output is correct
2 Correct 1 ms 4440 KB Output is correct
3 Correct 1 ms 4440 KB Output is correct
4 Correct 1 ms 4444 KB Output is correct
5 Correct 1 ms 4444 KB Output is correct
6 Correct 1 ms 4444 KB Output is correct
7 Correct 1 ms 4444 KB Output is correct
8 Correct 1 ms 4444 KB Output is correct
9 Correct 1 ms 4444 KB Output is correct
10 Correct 1 ms 4444 KB Output is correct
11 Correct 1 ms 4444 KB Output is correct
12 Correct 1 ms 4444 KB Output is correct
13 Correct 1 ms 4444 KB Output is correct
14 Correct 1 ms 4440 KB Output is correct
15 Correct 1 ms 4444 KB Output is correct
16 Correct 1 ms 4444 KB Output is correct
17 Correct 1 ms 4564 KB Output is correct
18 Correct 1 ms 4444 KB Output is correct
19 Correct 1 ms 4444 KB Output is correct
20 Correct 1 ms 4440 KB Output is correct
21 Correct 1 ms 4444 KB Output is correct
22 Correct 1 ms 4444 KB Output is correct
23 Correct 1 ms 4444 KB Output is correct
24 Correct 1 ms 4444 KB Output is correct
25 Correct 1 ms 4444 KB Output is correct
26 Correct 1 ms 4444 KB Output is correct
27 Correct 13 ms 7692 KB Output is correct
28 Correct 12 ms 7380 KB Output is correct
29 Correct 18 ms 7736 KB Output is correct
30 Correct 12 ms 7548 KB Output is correct
31 Correct 14 ms 7632 KB Output is correct
32 Correct 14 ms 7636 KB Output is correct
33 Correct 13 ms 7504 KB Output is correct
34 Correct 11 ms 7796 KB Output is correct
35 Correct 37 ms 7636 KB Output is correct
36 Correct 33 ms 7632 KB Output is correct
37 Correct 32 ms 7636 KB Output is correct
38 Correct 32 ms 7632 KB Output is correct
39 Correct 33 ms 7632 KB Output is correct
40 Correct 24 ms 7636 KB Output is correct
41 Correct 22 ms 7892 KB Output is correct
42 Correct 19 ms 7636 KB Output is correct
43 Correct 30 ms 7636 KB Output is correct
44 Correct 22 ms 7760 KB Output is correct
45 Correct 23 ms 7636 KB Output is correct
46 Correct 20 ms 7632 KB Output is correct
47 Correct 20 ms 7636 KB Output is correct
48 Correct 449 ms 40720 KB Output is correct
49 Correct 477 ms 40716 KB Output is correct
50 Correct 433 ms 40928 KB Output is correct
51 Correct 419 ms 40748 KB Output is correct
52 Correct 364 ms 38412 KB Output is correct
53 Correct 299 ms 39284 KB Output is correct
54 Correct 243 ms 37132 KB Output is correct
55 Correct 244 ms 37004 KB Output is correct
56 Correct 230 ms 36364 KB Output is correct
57 Correct 2222 ms 210956 KB Output is correct
58 Correct 1607 ms 145932 KB Output is correct
59 Correct 1703 ms 145512 KB Output is correct
60 Correct 1867 ms 163716 KB Output is correct
61 Correct 1981 ms 163852 KB Output is correct
62 Correct 866 ms 97976 KB Output is correct
63 Correct 1223 ms 118780 KB Output is correct
64 Correct 1048 ms 106060 KB Output is correct
65 Correct 638 ms 70804 KB Output is correct