답안 #374796

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
374796 2021-03-08T07:55:54 Z valerikk Bubble Sort 2 (JOI18_bubblesort2) C++17
100 / 100
2659 ms 138792 KB
/*
    doing virtual contest
    start: 10:00
    finish: 14:00
*/
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;

struct segtree {
    struct node {
        int mx, add;
    };

    node merge(const node& l, const node& r) {
        return {max(l.mx, r.mx), 0};
    }

    int n;
    vector<node> t;

    void apply(int v, int d) {
        t[v].mx += d;
        t[v].add += d;
    }

    void push(int v) {
        apply(2 * v, t[v].add);
        apply(2 * v + 1, t[v].add);
        t[v].add = 0;
    }

    void upd(int v) {
        t[v] = merge(t[2 * v], t[2 * v + 1]);
    }

    void add_seg(int v, int vl, int vr, int l, int r, int d) {
        if (vl >= r || vr <= l) return;
        if (vl >= l && vr <= r) {
            apply(v, d);
            return;
        }
        push(v);
        int vm = (vl + vr) / 2;
        add_seg(2 * v, vl, vm, l, r, d);
        add_seg(2 * v + 1, vm, vr, l, r, d);
        upd(v);
    }

    void add_seg(int l, int r, int d) {
        add_seg(1, 0, n, l, r, d);
    }

    void change(int v, int vl, int vr, int pos, int val) {
        if (vr - vl == 1) {
            t[v] = {val, 0};
        } else {
            push(v);
            int vm = (vl + vr) / 2;
            if (pos < vm) change(2 * v, vl, vm, pos, val); else change(2 * v + 1, vm, vr, pos, val);
            upd(v);
        }
    }

    void change(int pos, int val) {
        change(1, 0, n, pos, val);
    }

    int get_max() {
        return t[1].mx;
    }

    segtree(int n_) : n(n_) {
        t = vector<node>(4 * n, {0, 0});
    }
};

struct fenwick {
    int n;
    vector<int> f;

    void add(int pos, int d) {
        for (int i = pos + 1; i <= n; i += i & -i) f[i] += d;
    }

    int sum(int r) {
        int s = 0;
        for (int i = r; i > 0; i -= i & -i) s += f[i];
        return s;
    }

    int sum(int l, int r) {
        return sum(r) - sum(l);
    }

    fenwick(int n_) : n(n_) {
        f = vector<int>(n + 1, 0);
    }
};

vector<int> countScans(vector<int> a, vector<int> x, vector<int> v) {
    int n = (int)a.size(), q = (int)x.size();
    vector<int> all;
    for (int i = 0; i < n; ++i) all.push_back(a[i]);
    for (int i = 0; i < q; ++i) all.push_back(v[i]);
    sort(all.begin(), all.end());
    all.erase(unique(all.begin(), all.end()), all.end());
    for (int i = 0; i < n; ++i) a[i] = lower_bound(all.begin(), all.end(), a[i]) - all.begin();
    for (int i = 0; i < q; ++i) v[i] = lower_bound(all.begin(), all.end(), v[i]) - all.begin();
    int m = (int)all.size();
    vector<set<int>> st(m);
    segtree t(m);
    fenwick f(m);
    for (int i = 0; i < n; ++i) {
        st[a[i]].insert(i);
        f.add(a[i], 1);
    }
    static const int INF = (int)1e9;
    auto eval = [&](int ind) {
        if (st[ind].empty()) return -INF;
        return *st[ind].rbegin() - f.sum(ind) - (int)st[ind].size() + 1;
    };
    for (int i = 0; i < m; ++i) t.change(i, eval(i));
    vector<int> s;
    for (int i = 0; i < q; ++i) {
        t.add_seg(a[x[i]] + 1, m, 1);
        f.add(a[x[i]], -1);
        st[a[x[i]]].erase(x[i]);
        st[v[i]].insert(x[i]);
        f.add(v[i], 1);
        t.add_seg(v[i] + 1, m, -1);
        t.change(a[x[i]], eval(a[x[i]]));
        t.change(v[i], eval(v[i]));
        a[x[i]] = v[i];
        s.push_back(max(0, t.get_max()));
    }
    return s;
}

#ifdef LOCAL
int main() {
    freopen("input.txt", "r", stdin);
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n, q;
    cin >> n >> q;
    vector<int> a(n);
    for (int i = 0; i < n; ++i) cin >> a[i];
    vector<int> x(q), v(q);
    for (int i = 0; i < q; ++i) cin >> x[i] >> v[i];
    auto s = countScans(a, x, v);
    for (int i = 0; i < q; ++i) cout << s[i] << '\n';
    return 0;
}
#endif
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 492 KB Output is correct
2 Correct 2 ms 492 KB Output is correct
3 Correct 5 ms 748 KB Output is correct
4 Correct 6 ms 748 KB Output is correct
5 Correct 5 ms 748 KB Output is correct
6 Correct 5 ms 748 KB Output is correct
7 Correct 5 ms 748 KB Output is correct
8 Correct 5 ms 748 KB Output is correct
9 Correct 5 ms 748 KB Output is correct
10 Correct 5 ms 748 KB Output is correct
11 Correct 5 ms 748 KB Output is correct
12 Correct 5 ms 748 KB Output is correct
13 Correct 5 ms 896 KB Output is correct
14 Correct 6 ms 748 KB Output is correct
15 Correct 5 ms 748 KB Output is correct
16 Correct 5 ms 748 KB Output is correct
17 Correct 4 ms 748 KB Output is correct
18 Correct 4 ms 748 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 492 KB Output is correct
2 Correct 2 ms 492 KB Output is correct
3 Correct 5 ms 748 KB Output is correct
4 Correct 6 ms 748 KB Output is correct
5 Correct 5 ms 748 KB Output is correct
6 Correct 5 ms 748 KB Output is correct
7 Correct 5 ms 748 KB Output is correct
8 Correct 5 ms 748 KB Output is correct
9 Correct 5 ms 748 KB Output is correct
10 Correct 5 ms 748 KB Output is correct
11 Correct 5 ms 748 KB Output is correct
12 Correct 5 ms 748 KB Output is correct
13 Correct 5 ms 896 KB Output is correct
14 Correct 6 ms 748 KB Output is correct
15 Correct 5 ms 748 KB Output is correct
16 Correct 5 ms 748 KB Output is correct
17 Correct 4 ms 748 KB Output is correct
18 Correct 4 ms 748 KB Output is correct
19 Correct 20 ms 2028 KB Output is correct
20 Correct 22 ms 2284 KB Output is correct
21 Correct 23 ms 2348 KB Output is correct
22 Correct 21 ms 2284 KB Output is correct
23 Correct 20 ms 2156 KB Output is correct
24 Correct 20 ms 2156 KB Output is correct
25 Correct 19 ms 2028 KB Output is correct
26 Correct 19 ms 2028 KB Output is correct
27 Correct 19 ms 2028 KB Output is correct
28 Correct 18 ms 2028 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 2028 KB Output is correct
2 Correct 51 ms 3304 KB Output is correct
3 Correct 91 ms 4728 KB Output is correct
4 Correct 95 ms 4732 KB Output is correct
5 Correct 94 ms 4708 KB Output is correct
6 Correct 93 ms 4708 KB Output is correct
7 Correct 89 ms 4708 KB Output is correct
8 Correct 88 ms 4708 KB Output is correct
9 Correct 92 ms 4708 KB Output is correct
10 Correct 79 ms 4708 KB Output is correct
11 Correct 70 ms 4708 KB Output is correct
12 Correct 71 ms 4708 KB Output is correct
13 Correct 68 ms 4856 KB Output is correct
14 Correct 66 ms 4708 KB Output is correct
15 Correct 68 ms 4708 KB Output is correct
16 Correct 62 ms 4708 KB Output is correct
17 Correct 62 ms 4708 KB Output is correct
18 Correct 62 ms 4708 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 492 KB Output is correct
2 Correct 2 ms 492 KB Output is correct
3 Correct 5 ms 748 KB Output is correct
4 Correct 6 ms 748 KB Output is correct
5 Correct 5 ms 748 KB Output is correct
6 Correct 5 ms 748 KB Output is correct
7 Correct 5 ms 748 KB Output is correct
8 Correct 5 ms 748 KB Output is correct
9 Correct 5 ms 748 KB Output is correct
10 Correct 5 ms 748 KB Output is correct
11 Correct 5 ms 748 KB Output is correct
12 Correct 5 ms 748 KB Output is correct
13 Correct 5 ms 896 KB Output is correct
14 Correct 6 ms 748 KB Output is correct
15 Correct 5 ms 748 KB Output is correct
16 Correct 5 ms 748 KB Output is correct
17 Correct 4 ms 748 KB Output is correct
18 Correct 4 ms 748 KB Output is correct
19 Correct 20 ms 2028 KB Output is correct
20 Correct 22 ms 2284 KB Output is correct
21 Correct 23 ms 2348 KB Output is correct
22 Correct 21 ms 2284 KB Output is correct
23 Correct 20 ms 2156 KB Output is correct
24 Correct 20 ms 2156 KB Output is correct
25 Correct 19 ms 2028 KB Output is correct
26 Correct 19 ms 2028 KB Output is correct
27 Correct 19 ms 2028 KB Output is correct
28 Correct 18 ms 2028 KB Output is correct
29 Correct 15 ms 2028 KB Output is correct
30 Correct 51 ms 3304 KB Output is correct
31 Correct 91 ms 4728 KB Output is correct
32 Correct 95 ms 4732 KB Output is correct
33 Correct 94 ms 4708 KB Output is correct
34 Correct 93 ms 4708 KB Output is correct
35 Correct 89 ms 4708 KB Output is correct
36 Correct 88 ms 4708 KB Output is correct
37 Correct 92 ms 4708 KB Output is correct
38 Correct 79 ms 4708 KB Output is correct
39 Correct 70 ms 4708 KB Output is correct
40 Correct 71 ms 4708 KB Output is correct
41 Correct 68 ms 4856 KB Output is correct
42 Correct 66 ms 4708 KB Output is correct
43 Correct 68 ms 4708 KB Output is correct
44 Correct 62 ms 4708 KB Output is correct
45 Correct 62 ms 4708 KB Output is correct
46 Correct 62 ms 4708 KB Output is correct
47 Correct 633 ms 38444 KB Output is correct
48 Correct 2432 ms 125712 KB Output is correct
49 Correct 2604 ms 138748 KB Output is correct
50 Correct 2659 ms 138444 KB Output is correct
51 Correct 2627 ms 138680 KB Output is correct
52 Correct 2646 ms 138696 KB Output is correct
53 Correct 2648 ms 138792 KB Output is correct
54 Correct 2448 ms 138764 KB Output is correct
55 Correct 2559 ms 138580 KB Output is correct
56 Correct 2395 ms 138580 KB Output is correct
57 Correct 2523 ms 138648 KB Output is correct
58 Correct 2391 ms 138580 KB Output is correct
59 Correct 2150 ms 127060 KB Output is correct
60 Correct 2178 ms 127296 KB Output is correct
61 Correct 2126 ms 127208 KB Output is correct
62 Correct 2040 ms 121884 KB Output is correct
63 Correct 2035 ms 121960 KB Output is correct
64 Correct 2066 ms 122072 KB Output is correct
65 Correct 1907 ms 116692 KB Output is correct
66 Correct 1915 ms 116636 KB Output is correct
67 Correct 1913 ms 116564 KB Output is correct