답안 #705453

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
705453 2023-03-04T12:19:10 Z bebra Bubble Sort 2 (JOI18_bubblesort2) C++17
100 / 100
3462 ms 168612 KB
#include "bubblesort2.h"
#include <bits/stdc++.h>
using ll = long long;
using namespace std;

#define dbg(x) cerr << #x << ": " << x << endl;


struct FenwickTree {
    vector<int> tree;
    int size;

    FenwickTree(int n) {
        size = n;
        tree.resize(size);
    }

    void update(int pos, int value) {
        for (int i = pos; i < size; i |= i + 1) {
            tree[i] += value;
        }
    }

    int query(int l, int r) {
        int res = 0;
        for (int i = r; i >= 0; i = (i & (i + 1)) - 1) {
            res += tree[i];
        }
        for (int i = l - 1; i >= 0; i = (i & (i + 1)) - 1) {
            res -= tree[i];
        }
        return res;
    }
};


const int INF = 1e9;

struct SegmentTree {
    struct Node {
        int value;
        int push;

        Node(int _value = -INF, int _push = 0)
            : value(_value), push(_push) {}

        friend Node combine(const Node& lhs, const Node& rhs) {
            return {max(lhs.value, rhs.value), 0};
        }
    };

    vector<Node> tree;
    int size;

    SegmentTree(int n) {
        size = 1 << (__lg(n) + 1);
        tree.resize(2 * size - 1);
    }

    void point_update(int pos, int value) {
        point_update(0, size, 0, pos, value);
    }

    void point_update(int l, int r, int v, int pos, int value) {
        push(l, r, v);
        if (l == r - 1) {
            tree[v].value = value;
            return;
        }
        int m = (l + r) / 2;
        if (pos < m) {
            point_update(l, m, 2 * v + 1, pos, value);
        } else {
            point_update(m, r, 2 * v + 2, pos, value);
        }
        tree[v] = combine(tree[2 * v + 1], tree[2 * v + 2]);
    }

    void update(int lq, int rq, int value) {
        update(0, size, 0, lq, rq, value);
    }

    void apply(int v, int value) {
        tree[v].value += value;
        tree[v].push += value;
    }

    void push(int l, int r, int v) {
        if (l == r - 1 || tree[v].push == 0) return;
        apply(2 * v + 1, tree[v].push);
        apply(2 * v + 2, tree[v].push);
        tree[v].push = 0;
    }

    void update(int l, int r, int v, int lq, int rq, int value) {
        push(l, r, v);
        if (lq <= l && r <= rq) {
            apply(v, value);
            return;
        } else if (l >= rq || r <= lq) {
            return;
        }
        int m = (l + r) / 2;
        update(l, m, 2 * v + 1, lq, rq, value);
        update(m, r, 2 * v + 2, lq, rq, value);
        tree[v] = combine(tree[2 * v + 1], tree[2 * v + 2]);
    }

    int get_max() const {
        return tree[0].value;
    }
};



vector<int> countScans(vector<int> a, vector<int> pos, vector<int> value){
    int n = a.size();
	int q = pos.size();

    vector<int> all_values = a;
    all_values.insert(all_values.end(), value.begin(), value.end());

    sort(all_values.begin(), all_values.end());
    all_values.resize(unique(all_values.begin(), all_values.end()) - all_values.begin());

    int k = all_values.size();
    map<int, int> mp;
    for (int i = 0; i < k; ++i) {
        mp[all_values[i]] = i;
    }

    for (auto& x : a) {
        x = mp[x];
    }
    for (auto& x : value) {
        x = mp[x];
    }

    FenwickTree tree(k);
    for (auto x : a) {
        tree.update(x, 1);
    }

    SegmentTree segtree(k);
    vector<set<int, greater<int>>> occurs(k);
    for (int i = 0; i < n; ++i) {
        segtree.point_update(a[i], i - tree.query(0, a[i]) + 1);
        occurs[a[i]].insert(i);
    }

    vector<int> ans(q);
    for (int i = 0; i < q; ++i) {
        {
            int x = a[pos[i]];
            
            tree.update(x, -1);
            occurs[x].erase(pos[i]);

            segtree.update(x, k, 1);
            
            if (occurs[x].empty()) {
                segtree.point_update(x, -INF);
            } else {
                int new_value = *occurs[x].begin() - tree.query(0, x) + 1;
                segtree.point_update(x, new_value);
            }
        }

        {
            int x = value[i];
            a[pos[i]] = x;

            occurs[x].insert(pos[i]);
            tree.update(x, 1);

            int new_value = *occurs[x].begin() - tree.query(0, x) + 1;

            segtree.update(x, k, -1);
            segtree.point_update(x, new_value);
        }

        ans[i] = segtree.get_max();
    }

	return ans;
}



// int main() {
//     ios_base::sync_with_stdio(false);
//     cin.tie(nullptr);

//     int n, q;
//     cin >> n >> q;
//     vector<int> a(n);
//     for (auto& x : a) {
//         cin >> x;
//     }
//     vector<int> pos(q), value(q);
//     for (int i = 0; i < q; ++i) {
//         cin >> pos[i] >> value[i];
//     }
//     for (auto x : countScans(a, pos, value)) {
//         cout << x << '\n';
//     }
//     return 0;
// }


/*
4 2
1 2 3 4
0 3
2 1


5 5
5 4 4 2 1
0 1
1 2
2 3
3 4
4 5


right:
3
3
3
3
0
*/
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 468 KB Output is correct
2 Correct 3 ms 468 KB Output is correct
3 Correct 5 ms 928 KB Output is correct
4 Correct 5 ms 852 KB Output is correct
5 Correct 5 ms 952 KB Output is correct
6 Correct 5 ms 852 KB Output is correct
7 Correct 5 ms 852 KB Output is correct
8 Correct 5 ms 852 KB Output is correct
9 Correct 5 ms 860 KB Output is correct
10 Correct 5 ms 852 KB Output is correct
11 Correct 5 ms 856 KB Output is correct
12 Correct 5 ms 916 KB Output is correct
13 Correct 5 ms 820 KB Output is correct
14 Correct 5 ms 828 KB Output is correct
15 Correct 5 ms 820 KB Output is correct
16 Correct 5 ms 852 KB Output is correct
17 Correct 4 ms 824 KB Output is correct
18 Correct 4 ms 852 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 468 KB Output is correct
2 Correct 3 ms 468 KB Output is correct
3 Correct 5 ms 928 KB Output is correct
4 Correct 5 ms 852 KB Output is correct
5 Correct 5 ms 952 KB Output is correct
6 Correct 5 ms 852 KB Output is correct
7 Correct 5 ms 852 KB Output is correct
8 Correct 5 ms 852 KB Output is correct
9 Correct 5 ms 860 KB Output is correct
10 Correct 5 ms 852 KB Output is correct
11 Correct 5 ms 856 KB Output is correct
12 Correct 5 ms 916 KB Output is correct
13 Correct 5 ms 820 KB Output is correct
14 Correct 5 ms 828 KB Output is correct
15 Correct 5 ms 820 KB Output is correct
16 Correct 5 ms 852 KB Output is correct
17 Correct 4 ms 824 KB Output is correct
18 Correct 4 ms 852 KB Output is correct
19 Correct 20 ms 2644 KB Output is correct
20 Correct 23 ms 2980 KB Output is correct
21 Correct 23 ms 2900 KB Output is correct
22 Correct 21 ms 2968 KB Output is correct
23 Correct 21 ms 2644 KB Output is correct
24 Correct 22 ms 2620 KB Output is correct
25 Correct 19 ms 2644 KB Output is correct
26 Correct 20 ms 2616 KB Output is correct
27 Correct 20 ms 2532 KB Output is correct
28 Correct 25 ms 2512 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 1972 KB Output is correct
2 Correct 55 ms 3500 KB Output is correct
3 Correct 92 ms 4984 KB Output is correct
4 Correct 98 ms 4872 KB Output is correct
5 Correct 87 ms 4940 KB Output is correct
6 Correct 84 ms 4872 KB Output is correct
7 Correct 87 ms 4940 KB Output is correct
8 Correct 86 ms 5000 KB Output is correct
9 Correct 89 ms 4912 KB Output is correct
10 Correct 66 ms 4964 KB Output is correct
11 Correct 73 ms 5072 KB Output is correct
12 Correct 67 ms 5004 KB Output is correct
13 Correct 69 ms 5020 KB Output is correct
14 Correct 69 ms 4976 KB Output is correct
15 Correct 66 ms 5032 KB Output is correct
16 Correct 65 ms 5012 KB Output is correct
17 Correct 66 ms 5000 KB Output is correct
18 Correct 62 ms 4980 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 468 KB Output is correct
2 Correct 3 ms 468 KB Output is correct
3 Correct 5 ms 928 KB Output is correct
4 Correct 5 ms 852 KB Output is correct
5 Correct 5 ms 952 KB Output is correct
6 Correct 5 ms 852 KB Output is correct
7 Correct 5 ms 852 KB Output is correct
8 Correct 5 ms 852 KB Output is correct
9 Correct 5 ms 860 KB Output is correct
10 Correct 5 ms 852 KB Output is correct
11 Correct 5 ms 856 KB Output is correct
12 Correct 5 ms 916 KB Output is correct
13 Correct 5 ms 820 KB Output is correct
14 Correct 5 ms 828 KB Output is correct
15 Correct 5 ms 820 KB Output is correct
16 Correct 5 ms 852 KB Output is correct
17 Correct 4 ms 824 KB Output is correct
18 Correct 4 ms 852 KB Output is correct
19 Correct 20 ms 2644 KB Output is correct
20 Correct 23 ms 2980 KB Output is correct
21 Correct 23 ms 2900 KB Output is correct
22 Correct 21 ms 2968 KB Output is correct
23 Correct 21 ms 2644 KB Output is correct
24 Correct 22 ms 2620 KB Output is correct
25 Correct 19 ms 2644 KB Output is correct
26 Correct 20 ms 2616 KB Output is correct
27 Correct 20 ms 2532 KB Output is correct
28 Correct 25 ms 2512 KB Output is correct
29 Correct 15 ms 1972 KB Output is correct
30 Correct 55 ms 3500 KB Output is correct
31 Correct 92 ms 4984 KB Output is correct
32 Correct 98 ms 4872 KB Output is correct
33 Correct 87 ms 4940 KB Output is correct
34 Correct 84 ms 4872 KB Output is correct
35 Correct 87 ms 4940 KB Output is correct
36 Correct 86 ms 5000 KB Output is correct
37 Correct 89 ms 4912 KB Output is correct
38 Correct 66 ms 4964 KB Output is correct
39 Correct 73 ms 5072 KB Output is correct
40 Correct 67 ms 5004 KB Output is correct
41 Correct 69 ms 5020 KB Output is correct
42 Correct 69 ms 4976 KB Output is correct
43 Correct 66 ms 5032 KB Output is correct
44 Correct 65 ms 5012 KB Output is correct
45 Correct 66 ms 5000 KB Output is correct
46 Correct 62 ms 4980 KB Output is correct
47 Correct 712 ms 53776 KB Output is correct
48 Correct 2937 ms 154368 KB Output is correct
49 Correct 3462 ms 168004 KB Output is correct
50 Correct 3383 ms 168472 KB Output is correct
51 Correct 3346 ms 168540 KB Output is correct
52 Correct 3455 ms 168456 KB Output is correct
53 Correct 3293 ms 168480 KB Output is correct
54 Correct 2860 ms 168572 KB Output is correct
55 Correct 3075 ms 168484 KB Output is correct
56 Correct 2849 ms 168612 KB Output is correct
57 Correct 3143 ms 168520 KB Output is correct
58 Correct 2789 ms 168564 KB Output is correct
59 Correct 2740 ms 155208 KB Output is correct
60 Correct 2695 ms 155144 KB Output is correct
61 Correct 2695 ms 155164 KB Output is correct
62 Correct 2557 ms 148864 KB Output is correct
63 Correct 2584 ms 148864 KB Output is correct
64 Correct 2582 ms 148900 KB Output is correct
65 Correct 2503 ms 142696 KB Output is correct
66 Correct 2492 ms 142532 KB Output is correct
67 Correct 2400 ms 142532 KB Output is correct