답안 #1001052

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1001052 2024-06-18T13:44:58 Z mispertion Rope (JOI17_rope) C++14
80 / 100
2500 ms 211892 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){
        if(l > r)
            return infi;
        push(v, tl, tr);
        if(tl > r || l > tr)
            return infi;
        if(l <= tl && tr <= r)
            return t[v];
        int tm = (tl + tr) / 2;
        return min(get(2 * v, tl, tm, l, r), get(2 * v + 1, tm + 1, tr, l, r));
    }

    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]);
    }
};

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, 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], 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], 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:173:9: warning: unused variable 'sm' [-Wunused-variable]
  173 |     int sm = 0;
      |         ^~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2648 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 0 ms 2396 KB Output is correct
4 Correct 1 ms 2396 KB Output is correct
5 Correct 0 ms 2396 KB Output is correct
6 Correct 0 ms 2396 KB Output is correct
7 Correct 1 ms 2396 KB Output is correct
8 Correct 1 ms 2396 KB Output is correct
9 Correct 1 ms 2392 KB Output is correct
10 Correct 0 ms 2492 KB Output is correct
11 Correct 1 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 1 ms 2396 KB Output is correct
17 Correct 0 ms 2396 KB Output is correct
18 Correct 1 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 1 ms 2396 KB Output is correct
25 Correct 0 ms 2396 KB Output is correct
26 Correct 0 ms 2396 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2648 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 0 ms 2396 KB Output is correct
4 Correct 1 ms 2396 KB Output is correct
5 Correct 0 ms 2396 KB Output is correct
6 Correct 0 ms 2396 KB Output is correct
7 Correct 1 ms 2396 KB Output is correct
8 Correct 1 ms 2396 KB Output is correct
9 Correct 1 ms 2392 KB Output is correct
10 Correct 0 ms 2492 KB Output is correct
11 Correct 1 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 1 ms 2396 KB Output is correct
17 Correct 0 ms 2396 KB Output is correct
18 Correct 1 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 1 ms 2396 KB Output is correct
25 Correct 0 ms 2396 KB Output is correct
26 Correct 0 ms 2396 KB Output is correct
27 Correct 16 ms 7636 KB Output is correct
28 Correct 11 ms 7380 KB Output is correct
29 Correct 17 ms 7636 KB Output is correct
30 Correct 13 ms 7632 KB Output is correct
31 Correct 16 ms 7788 KB Output is correct
32 Correct 11 ms 7636 KB Output is correct
33 Correct 17 ms 7712 KB Output is correct
34 Correct 12 ms 7636 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2648 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 0 ms 2396 KB Output is correct
4 Correct 1 ms 2396 KB Output is correct
5 Correct 0 ms 2396 KB Output is correct
6 Correct 0 ms 2396 KB Output is correct
7 Correct 1 ms 2396 KB Output is correct
8 Correct 1 ms 2396 KB Output is correct
9 Correct 1 ms 2392 KB Output is correct
10 Correct 0 ms 2492 KB Output is correct
11 Correct 1 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 1 ms 2396 KB Output is correct
17 Correct 0 ms 2396 KB Output is correct
18 Correct 1 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 1 ms 2396 KB Output is correct
25 Correct 0 ms 2396 KB Output is correct
26 Correct 0 ms 2396 KB Output is correct
27 Correct 16 ms 7636 KB Output is correct
28 Correct 11 ms 7380 KB Output is correct
29 Correct 17 ms 7636 KB Output is correct
30 Correct 13 ms 7632 KB Output is correct
31 Correct 16 ms 7788 KB Output is correct
32 Correct 11 ms 7636 KB Output is correct
33 Correct 17 ms 7712 KB Output is correct
34 Correct 12 ms 7636 KB Output is correct
35 Correct 44 ms 7632 KB Output is correct
36 Correct 44 ms 7688 KB Output is correct
37 Correct 42 ms 7636 KB Output is correct
38 Correct 42 ms 7632 KB Output is correct
39 Correct 42 ms 7632 KB Output is correct
40 Correct 34 ms 7888 KB Output is correct
41 Correct 34 ms 7892 KB Output is correct
42 Correct 33 ms 7632 KB Output is correct
43 Correct 29 ms 7888 KB Output is correct
44 Correct 37 ms 7708 KB Output is correct
45 Correct 33 ms 7636 KB Output is correct
46 Correct 30 ms 7636 KB Output is correct
47 Correct 30 ms 7636 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2648 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 0 ms 2396 KB Output is correct
4 Correct 1 ms 2396 KB Output is correct
5 Correct 0 ms 2396 KB Output is correct
6 Correct 0 ms 2396 KB Output is correct
7 Correct 1 ms 2396 KB Output is correct
8 Correct 1 ms 2396 KB Output is correct
9 Correct 1 ms 2392 KB Output is correct
10 Correct 0 ms 2492 KB Output is correct
11 Correct 1 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 1 ms 2396 KB Output is correct
17 Correct 0 ms 2396 KB Output is correct
18 Correct 1 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 1 ms 2396 KB Output is correct
25 Correct 0 ms 2396 KB Output is correct
26 Correct 0 ms 2396 KB Output is correct
27 Correct 16 ms 7636 KB Output is correct
28 Correct 11 ms 7380 KB Output is correct
29 Correct 17 ms 7636 KB Output is correct
30 Correct 13 ms 7632 KB Output is correct
31 Correct 16 ms 7788 KB Output is correct
32 Correct 11 ms 7636 KB Output is correct
33 Correct 17 ms 7712 KB Output is correct
34 Correct 12 ms 7636 KB Output is correct
35 Correct 44 ms 7632 KB Output is correct
36 Correct 44 ms 7688 KB Output is correct
37 Correct 42 ms 7636 KB Output is correct
38 Correct 42 ms 7632 KB Output is correct
39 Correct 42 ms 7632 KB Output is correct
40 Correct 34 ms 7888 KB Output is correct
41 Correct 34 ms 7892 KB Output is correct
42 Correct 33 ms 7632 KB Output is correct
43 Correct 29 ms 7888 KB Output is correct
44 Correct 37 ms 7708 KB Output is correct
45 Correct 33 ms 7636 KB Output is correct
46 Correct 30 ms 7636 KB Output is correct
47 Correct 30 ms 7636 KB Output is correct
48 Correct 556 ms 38928 KB Output is correct
49 Correct 663 ms 43540 KB Output is correct
50 Correct 579 ms 43564 KB Output is correct
51 Correct 498 ms 42992 KB Output is correct
52 Correct 449 ms 40172 KB Output is correct
53 Correct 494 ms 41740 KB Output is correct
54 Correct 486 ms 39948 KB Output is correct
55 Correct 450 ms 39440 KB Output is correct
56 Correct 456 ms 39124 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2648 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 0 ms 2396 KB Output is correct
4 Correct 1 ms 2396 KB Output is correct
5 Correct 0 ms 2396 KB Output is correct
6 Correct 0 ms 2396 KB Output is correct
7 Correct 1 ms 2396 KB Output is correct
8 Correct 1 ms 2396 KB Output is correct
9 Correct 1 ms 2392 KB Output is correct
10 Correct 0 ms 2492 KB Output is correct
11 Correct 1 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 1 ms 2396 KB Output is correct
17 Correct 0 ms 2396 KB Output is correct
18 Correct 1 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 1 ms 2396 KB Output is correct
25 Correct 0 ms 2396 KB Output is correct
26 Correct 0 ms 2396 KB Output is correct
27 Correct 16 ms 7636 KB Output is correct
28 Correct 11 ms 7380 KB Output is correct
29 Correct 17 ms 7636 KB Output is correct
30 Correct 13 ms 7632 KB Output is correct
31 Correct 16 ms 7788 KB Output is correct
32 Correct 11 ms 7636 KB Output is correct
33 Correct 17 ms 7712 KB Output is correct
34 Correct 12 ms 7636 KB Output is correct
35 Correct 44 ms 7632 KB Output is correct
36 Correct 44 ms 7688 KB Output is correct
37 Correct 42 ms 7636 KB Output is correct
38 Correct 42 ms 7632 KB Output is correct
39 Correct 42 ms 7632 KB Output is correct
40 Correct 34 ms 7888 KB Output is correct
41 Correct 34 ms 7892 KB Output is correct
42 Correct 33 ms 7632 KB Output is correct
43 Correct 29 ms 7888 KB Output is correct
44 Correct 37 ms 7708 KB Output is correct
45 Correct 33 ms 7636 KB Output is correct
46 Correct 30 ms 7636 KB Output is correct
47 Correct 30 ms 7636 KB Output is correct
48 Correct 556 ms 38928 KB Output is correct
49 Correct 663 ms 43540 KB Output is correct
50 Correct 579 ms 43564 KB Output is correct
51 Correct 498 ms 42992 KB Output is correct
52 Correct 449 ms 40172 KB Output is correct
53 Correct 494 ms 41740 KB Output is correct
54 Correct 486 ms 39948 KB Output is correct
55 Correct 450 ms 39440 KB Output is correct
56 Correct 456 ms 39124 KB Output is correct
57 Execution timed out 2547 ms 211892 KB Time limit exceeded
58 Halted 0 ms 0 KB -