답안 #705455

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
705455 2023-03-04T12:20:56 Z bebra Bubble Sort 2 (JOI18_bubblesort2) C++17
100 / 100
3205 ms 157080 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;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 468 KB Output is correct
2 Correct 2 ms 468 KB Output is correct
3 Correct 5 ms 980 KB Output is correct
4 Correct 5 ms 852 KB Output is correct
5 Correct 6 ms 852 KB Output is correct
6 Correct 5 ms 956 KB Output is correct
7 Correct 5 ms 852 KB Output is correct
8 Correct 7 ms 980 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 852 KB Output is correct
12 Correct 5 ms 852 KB Output is correct
13 Correct 5 ms 824 KB Output is correct
14 Correct 5 ms 852 KB Output is correct
15 Correct 5 ms 852 KB Output is correct
16 Correct 5 ms 852 KB Output is correct
17 Correct 5 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 2 ms 468 KB Output is correct
3 Correct 5 ms 980 KB Output is correct
4 Correct 5 ms 852 KB Output is correct
5 Correct 6 ms 852 KB Output is correct
6 Correct 5 ms 956 KB Output is correct
7 Correct 5 ms 852 KB Output is correct
8 Correct 7 ms 980 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 852 KB Output is correct
12 Correct 5 ms 852 KB Output is correct
13 Correct 5 ms 824 KB Output is correct
14 Correct 5 ms 852 KB Output is correct
15 Correct 5 ms 852 KB Output is correct
16 Correct 5 ms 852 KB Output is correct
17 Correct 5 ms 824 KB Output is correct
18 Correct 4 ms 852 KB Output is correct
19 Correct 20 ms 2616 KB Output is correct
20 Correct 22 ms 2868 KB Output is correct
21 Correct 20 ms 2880 KB Output is correct
22 Correct 21 ms 2900 KB Output is correct
23 Correct 20 ms 2644 KB Output is correct
24 Correct 22 ms 2724 KB Output is correct
25 Correct 19 ms 2644 KB Output is correct
26 Correct 19 ms 2644 KB Output is correct
27 Correct 18 ms 2556 KB Output is correct
28 Correct 21 ms 2420 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 2032 KB Output is correct
2 Correct 48 ms 3372 KB Output is correct
3 Correct 84 ms 4880 KB Output is correct
4 Correct 88 ms 4960 KB Output is correct
5 Correct 84 ms 4872 KB Output is correct
6 Correct 85 ms 4888 KB Output is correct
7 Correct 97 ms 4880 KB Output is correct
8 Correct 80 ms 4884 KB Output is correct
9 Correct 80 ms 4892 KB Output is correct
10 Correct 66 ms 5060 KB Output is correct
11 Correct 63 ms 5024 KB Output is correct
12 Correct 62 ms 5000 KB Output is correct
13 Correct 63 ms 5012 KB Output is correct
14 Correct 62 ms 5008 KB Output is correct
15 Correct 61 ms 5004 KB Output is correct
16 Correct 60 ms 5012 KB Output is correct
17 Correct 64 ms 4944 KB Output is correct
18 Correct 60 ms 5000 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 468 KB Output is correct
2 Correct 2 ms 468 KB Output is correct
3 Correct 5 ms 980 KB Output is correct
4 Correct 5 ms 852 KB Output is correct
5 Correct 6 ms 852 KB Output is correct
6 Correct 5 ms 956 KB Output is correct
7 Correct 5 ms 852 KB Output is correct
8 Correct 7 ms 980 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 852 KB Output is correct
12 Correct 5 ms 852 KB Output is correct
13 Correct 5 ms 824 KB Output is correct
14 Correct 5 ms 852 KB Output is correct
15 Correct 5 ms 852 KB Output is correct
16 Correct 5 ms 852 KB Output is correct
17 Correct 5 ms 824 KB Output is correct
18 Correct 4 ms 852 KB Output is correct
19 Correct 20 ms 2616 KB Output is correct
20 Correct 22 ms 2868 KB Output is correct
21 Correct 20 ms 2880 KB Output is correct
22 Correct 21 ms 2900 KB Output is correct
23 Correct 20 ms 2644 KB Output is correct
24 Correct 22 ms 2724 KB Output is correct
25 Correct 19 ms 2644 KB Output is correct
26 Correct 19 ms 2644 KB Output is correct
27 Correct 18 ms 2556 KB Output is correct
28 Correct 21 ms 2420 KB Output is correct
29 Correct 14 ms 2032 KB Output is correct
30 Correct 48 ms 3372 KB Output is correct
31 Correct 84 ms 4880 KB Output is correct
32 Correct 88 ms 4960 KB Output is correct
33 Correct 84 ms 4872 KB Output is correct
34 Correct 85 ms 4888 KB Output is correct
35 Correct 97 ms 4880 KB Output is correct
36 Correct 80 ms 4884 KB Output is correct
37 Correct 80 ms 4892 KB Output is correct
38 Correct 66 ms 5060 KB Output is correct
39 Correct 63 ms 5024 KB Output is correct
40 Correct 62 ms 5000 KB Output is correct
41 Correct 63 ms 5012 KB Output is correct
42 Correct 62 ms 5008 KB Output is correct
43 Correct 61 ms 5004 KB Output is correct
44 Correct 60 ms 5012 KB Output is correct
45 Correct 64 ms 4944 KB Output is correct
46 Correct 60 ms 5000 KB Output is correct
47 Correct 705 ms 53876 KB Output is correct
48 Correct 2900 ms 144896 KB Output is correct
49 Correct 3179 ms 157080 KB Output is correct
50 Correct 3205 ms 156592 KB Output is correct
51 Correct 3176 ms 156740 KB Output is correct
52 Correct 3105 ms 156648 KB Output is correct
53 Correct 3190 ms 156572 KB Output is correct
54 Correct 2780 ms 156632 KB Output is correct
55 Correct 3004 ms 156652 KB Output is correct
56 Correct 2738 ms 156608 KB Output is correct
57 Correct 2975 ms 156656 KB Output is correct
58 Correct 2764 ms 156720 KB Output is correct
59 Correct 2583 ms 144456 KB Output is correct
60 Correct 2583 ms 144528 KB Output is correct
61 Correct 2553 ms 144372 KB Output is correct
62 Correct 2483 ms 138264 KB Output is correct
63 Correct 2442 ms 138252 KB Output is correct
64 Correct 2449 ms 138272 KB Output is correct
65 Correct 2287 ms 132104 KB Output is correct
66 Correct 2267 ms 132060 KB Output is correct
67 Correct 2271 ms 131984 KB Output is correct