Submission #1001061

# Submission time Handle Problem Language Result Execution time Memory
1001061 2024-06-18T13:54:45 Z mispertion Rope (JOI17_rope) C++17
80 / 100
2500 ms 205428 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, 1, cl - 1, -sz(ps[cl]));
        tr.add(1, 1, m, cl + 1, m, -sz(ps[cl]));
        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);
        tr.add(1, 1, m, 1, cl - 1, sz(ps[cl]));
        tr.add(1, 1, m, cl + 1, m, sz(ps[cl]));
        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];
 
void solve(){
    cin >> n >> m;
    for(int i = 1; i <= n; i++){
        cin >> 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++){
        if(i == c[n] && (n % 2 == 1)){
            fa.tr.add(1, 1, m, 1, c[n] - 1, -1);
            fa.tr.add(1, 1, m, c[n] + 1, m, -1);
        }
        int ans1 = fa.getans(i);
        if(i == c[n] && (n % 2 == 1)){
            fa.tr.add(1, 1, m, 1, c[n] - 1, 1);
            fa.tr.add(1, 1, m, c[n] + 1, m, 1);
        }
        if(n % 2){
            if(c[1] == i){
                sa.tr.add(1, 1, m, 1, c[1] - 1, -1);
                sa.tr.add(1, 1, m, c[1] + 1, m, -1);
            }
        }else{
            if(c[1] == i){
                sa.tr.add(1, 1, m, 1, c[1] - 1, -1);
                sa.tr.add(1, 1, m, c[1] + 1, m, -1);
            }
            if(c[n] == i){
                sa.tr.add(1, 1, m, 1, c[n] - 1, -1);
                sa.tr.add(1, 1, m, c[n] + 1, m, -1);
            }
        }
        int ans2 = sa.getans(i);
        if(n % 2){
            if(c[1] == i){
                sa.tr.add(1, 1, m, 1, c[1] - 1, 1);
                sa.tr.add(1, 1, m, c[1] + 1, m, 1);
            }
        }else{
            if(c[1] == i){
                sa.tr.add(1, 1, m, 1, c[1] - 1, 1);
                sa.tr.add(1, 1, m, c[1] + 1, m, 1);
            }
            if(c[n] == i){
                sa.tr.add(1, 1, m, 1, c[n] - 1, 1);
                sa.tr.add(1, 1, m, c[n] + 1, m, 1);
            }
        }
        cout << min(ans1, ans2) << '\n';
    }
}
 
signed main() {
    mispertion;
    int t = 1;
    //cin >> t;
    while(t--){
        solve();
    }
    return 0;
}

Compilation message

rope.cpp: In function 'void solve()':
rope.cpp:179:9: warning: unused variable 'sm' [-Wunused-variable]
  179 |     int sm = 0;
      |         ^~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 0 ms 2396 KB Output is correct
3 Correct 0 ms 2396 KB Output is correct
4 Correct 0 ms 2392 KB Output is correct
5 Correct 0 ms 2396 KB Output is correct
6 Correct 1 ms 2396 KB Output is correct
7 Correct 0 ms 2396 KB Output is correct
8 Correct 1 ms 2396 KB Output is correct
9 Correct 0 ms 2396 KB Output is correct
10 Correct 1 ms 2396 KB Output is correct
11 Correct 0 ms 2396 KB Output is correct
12 Correct 0 ms 2396 KB Output is correct
13 Correct 0 ms 2396 KB Output is correct
14 Correct 1 ms 2396 KB Output is correct
15 Correct 0 ms 2396 KB Output is correct
16 Correct 0 ms 2396 KB Output is correct
17 Correct 1 ms 2396 KB Output is correct
18 Correct 0 ms 2396 KB Output is correct
19 Correct 0 ms 2396 KB Output is correct
20 Correct 1 ms 2396 KB Output is correct
21 Correct 0 ms 2396 KB Output is correct
22 Correct 0 ms 2396 KB Output is correct
23 Correct 1 ms 2396 KB Output is correct
24 Correct 0 ms 2396 KB Output is correct
25 Correct 1 ms 2396 KB Output is correct
26 Correct 0 ms 2396 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 0 ms 2396 KB Output is correct
3 Correct 0 ms 2396 KB Output is correct
4 Correct 0 ms 2392 KB Output is correct
5 Correct 0 ms 2396 KB Output is correct
6 Correct 1 ms 2396 KB Output is correct
7 Correct 0 ms 2396 KB Output is correct
8 Correct 1 ms 2396 KB Output is correct
9 Correct 0 ms 2396 KB Output is correct
10 Correct 1 ms 2396 KB Output is correct
11 Correct 0 ms 2396 KB Output is correct
12 Correct 0 ms 2396 KB Output is correct
13 Correct 0 ms 2396 KB Output is correct
14 Correct 1 ms 2396 KB Output is correct
15 Correct 0 ms 2396 KB Output is correct
16 Correct 0 ms 2396 KB Output is correct
17 Correct 1 ms 2396 KB Output is correct
18 Correct 0 ms 2396 KB Output is correct
19 Correct 0 ms 2396 KB Output is correct
20 Correct 1 ms 2396 KB Output is correct
21 Correct 0 ms 2396 KB Output is correct
22 Correct 0 ms 2396 KB Output is correct
23 Correct 1 ms 2396 KB Output is correct
24 Correct 0 ms 2396 KB Output is correct
25 Correct 1 ms 2396 KB Output is correct
26 Correct 0 ms 2396 KB Output is correct
27 Correct 14 ms 7636 KB Output is correct
28 Correct 10 ms 7376 KB Output is correct
29 Correct 13 ms 7732 KB Output is correct
30 Correct 11 ms 7768 KB Output is correct
31 Correct 14 ms 7636 KB Output is correct
32 Correct 10 ms 7508 KB Output is correct
33 Correct 13 ms 7636 KB Output is correct
34 Correct 11 ms 7800 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 0 ms 2396 KB Output is correct
3 Correct 0 ms 2396 KB Output is correct
4 Correct 0 ms 2392 KB Output is correct
5 Correct 0 ms 2396 KB Output is correct
6 Correct 1 ms 2396 KB Output is correct
7 Correct 0 ms 2396 KB Output is correct
8 Correct 1 ms 2396 KB Output is correct
9 Correct 0 ms 2396 KB Output is correct
10 Correct 1 ms 2396 KB Output is correct
11 Correct 0 ms 2396 KB Output is correct
12 Correct 0 ms 2396 KB Output is correct
13 Correct 0 ms 2396 KB Output is correct
14 Correct 1 ms 2396 KB Output is correct
15 Correct 0 ms 2396 KB Output is correct
16 Correct 0 ms 2396 KB Output is correct
17 Correct 1 ms 2396 KB Output is correct
18 Correct 0 ms 2396 KB Output is correct
19 Correct 0 ms 2396 KB Output is correct
20 Correct 1 ms 2396 KB Output is correct
21 Correct 0 ms 2396 KB Output is correct
22 Correct 0 ms 2396 KB Output is correct
23 Correct 1 ms 2396 KB Output is correct
24 Correct 0 ms 2396 KB Output is correct
25 Correct 1 ms 2396 KB Output is correct
26 Correct 0 ms 2396 KB Output is correct
27 Correct 14 ms 7636 KB Output is correct
28 Correct 10 ms 7376 KB Output is correct
29 Correct 13 ms 7732 KB Output is correct
30 Correct 11 ms 7768 KB Output is correct
31 Correct 14 ms 7636 KB Output is correct
32 Correct 10 ms 7508 KB Output is correct
33 Correct 13 ms 7636 KB Output is correct
34 Correct 11 ms 7800 KB Output is correct
35 Correct 36 ms 7636 KB Output is correct
36 Correct 37 ms 7636 KB Output is correct
37 Correct 37 ms 7680 KB Output is correct
38 Correct 37 ms 7688 KB Output is correct
39 Correct 36 ms 7636 KB Output is correct
40 Correct 26 ms 7888 KB Output is correct
41 Correct 24 ms 8144 KB Output is correct
42 Correct 21 ms 7832 KB Output is correct
43 Correct 21 ms 7888 KB Output is correct
44 Correct 27 ms 7764 KB Output is correct
45 Correct 25 ms 7892 KB Output is correct
46 Correct 21 ms 7820 KB Output is correct
47 Correct 23 ms 7892 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 0 ms 2396 KB Output is correct
3 Correct 0 ms 2396 KB Output is correct
4 Correct 0 ms 2392 KB Output is correct
5 Correct 0 ms 2396 KB Output is correct
6 Correct 1 ms 2396 KB Output is correct
7 Correct 0 ms 2396 KB Output is correct
8 Correct 1 ms 2396 KB Output is correct
9 Correct 0 ms 2396 KB Output is correct
10 Correct 1 ms 2396 KB Output is correct
11 Correct 0 ms 2396 KB Output is correct
12 Correct 0 ms 2396 KB Output is correct
13 Correct 0 ms 2396 KB Output is correct
14 Correct 1 ms 2396 KB Output is correct
15 Correct 0 ms 2396 KB Output is correct
16 Correct 0 ms 2396 KB Output is correct
17 Correct 1 ms 2396 KB Output is correct
18 Correct 0 ms 2396 KB Output is correct
19 Correct 0 ms 2396 KB Output is correct
20 Correct 1 ms 2396 KB Output is correct
21 Correct 0 ms 2396 KB Output is correct
22 Correct 0 ms 2396 KB Output is correct
23 Correct 1 ms 2396 KB Output is correct
24 Correct 0 ms 2396 KB Output is correct
25 Correct 1 ms 2396 KB Output is correct
26 Correct 0 ms 2396 KB Output is correct
27 Correct 14 ms 7636 KB Output is correct
28 Correct 10 ms 7376 KB Output is correct
29 Correct 13 ms 7732 KB Output is correct
30 Correct 11 ms 7768 KB Output is correct
31 Correct 14 ms 7636 KB Output is correct
32 Correct 10 ms 7508 KB Output is correct
33 Correct 13 ms 7636 KB Output is correct
34 Correct 11 ms 7800 KB Output is correct
35 Correct 36 ms 7636 KB Output is correct
36 Correct 37 ms 7636 KB Output is correct
37 Correct 37 ms 7680 KB Output is correct
38 Correct 37 ms 7688 KB Output is correct
39 Correct 36 ms 7636 KB Output is correct
40 Correct 26 ms 7888 KB Output is correct
41 Correct 24 ms 8144 KB Output is correct
42 Correct 21 ms 7832 KB Output is correct
43 Correct 21 ms 7888 KB Output is correct
44 Correct 27 ms 7764 KB Output is correct
45 Correct 25 ms 7892 KB Output is correct
46 Correct 21 ms 7820 KB Output is correct
47 Correct 23 ms 7892 KB Output is correct
48 Correct 470 ms 39084 KB Output is correct
49 Correct 515 ms 38808 KB Output is correct
50 Correct 480 ms 38948 KB Output is correct
51 Correct 454 ms 38672 KB Output is correct
52 Correct 392 ms 36496 KB Output is correct
53 Correct 315 ms 37392 KB Output is correct
54 Correct 261 ms 35296 KB Output is correct
55 Correct 263 ms 35156 KB Output is correct
56 Correct 234 ms 34612 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 0 ms 2396 KB Output is correct
3 Correct 0 ms 2396 KB Output is correct
4 Correct 0 ms 2392 KB Output is correct
5 Correct 0 ms 2396 KB Output is correct
6 Correct 1 ms 2396 KB Output is correct
7 Correct 0 ms 2396 KB Output is correct
8 Correct 1 ms 2396 KB Output is correct
9 Correct 0 ms 2396 KB Output is correct
10 Correct 1 ms 2396 KB Output is correct
11 Correct 0 ms 2396 KB Output is correct
12 Correct 0 ms 2396 KB Output is correct
13 Correct 0 ms 2396 KB Output is correct
14 Correct 1 ms 2396 KB Output is correct
15 Correct 0 ms 2396 KB Output is correct
16 Correct 0 ms 2396 KB Output is correct
17 Correct 1 ms 2396 KB Output is correct
18 Correct 0 ms 2396 KB Output is correct
19 Correct 0 ms 2396 KB Output is correct
20 Correct 1 ms 2396 KB Output is correct
21 Correct 0 ms 2396 KB Output is correct
22 Correct 0 ms 2396 KB Output is correct
23 Correct 1 ms 2396 KB Output is correct
24 Correct 0 ms 2396 KB Output is correct
25 Correct 1 ms 2396 KB Output is correct
26 Correct 0 ms 2396 KB Output is correct
27 Correct 14 ms 7636 KB Output is correct
28 Correct 10 ms 7376 KB Output is correct
29 Correct 13 ms 7732 KB Output is correct
30 Correct 11 ms 7768 KB Output is correct
31 Correct 14 ms 7636 KB Output is correct
32 Correct 10 ms 7508 KB Output is correct
33 Correct 13 ms 7636 KB Output is correct
34 Correct 11 ms 7800 KB Output is correct
35 Correct 36 ms 7636 KB Output is correct
36 Correct 37 ms 7636 KB Output is correct
37 Correct 37 ms 7680 KB Output is correct
38 Correct 37 ms 7688 KB Output is correct
39 Correct 36 ms 7636 KB Output is correct
40 Correct 26 ms 7888 KB Output is correct
41 Correct 24 ms 8144 KB Output is correct
42 Correct 21 ms 7832 KB Output is correct
43 Correct 21 ms 7888 KB Output is correct
44 Correct 27 ms 7764 KB Output is correct
45 Correct 25 ms 7892 KB Output is correct
46 Correct 21 ms 7820 KB Output is correct
47 Correct 23 ms 7892 KB Output is correct
48 Correct 470 ms 39084 KB Output is correct
49 Correct 515 ms 38808 KB Output is correct
50 Correct 480 ms 38948 KB Output is correct
51 Correct 454 ms 38672 KB Output is correct
52 Correct 392 ms 36496 KB Output is correct
53 Correct 315 ms 37392 KB Output is correct
54 Correct 261 ms 35296 KB Output is correct
55 Correct 263 ms 35156 KB Output is correct
56 Correct 234 ms 34612 KB Output is correct
57 Execution timed out 2559 ms 205428 KB Time limit exceeded
58 Halted 0 ms 0 KB -