Submission #274496

# Submission time Handle Problem Language Result Execution time Memory
274496 2020-08-19T12:23:11 Z johutha Bubble Sort 2 (JOI18_bubblesort2) C++17
100 / 100
3409 ms 84068 KB
#include "bubblesort2.h"
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

pair<int,int> merge(const pair<int,int>& p1, const pair<int,int>& p2)
{
    return make_pair(max(p1.first, p2.first), p1.second + p2.second);
}

struct segtree
{
    vector<pair<int,int>> table;
    vector<int> op;

    void apply(int pos, int o)
    {
        table[pos].first += o;
        op[pos] += o;
    }

    void propagate(int pos)
    {
        apply(2*pos + 1, op[pos]);
        apply(2*pos + 2, op[pos]);
        op[pos] = 0;
    }

    pair<int,int> query(int ql, int qr, int l, int r, int pos)
    {
        if (ql <= l && r <= qr) return table[pos];
        if (r < ql || qr < l) return {-1e8, 0};
        propagate(pos);
        return merge(query(ql, qr, l, (l + r)/2, 2*pos + 1), query(ql, qr, (l + r)/2 + 1, r, 2*pos + 2));
    }

    void update(int ql, int qr, int o, int l, int r, int pos)
    {
        if (ql <= l && r <= qr) { apply(pos, o); return; }
        if (r < ql || qr < l) return;
        propagate(pos);
        update(ql, qr, o, l, (l + r)/2, 2*pos + 1);
        update(ql, qr, o, (l + r)/2 + 1, r, 2*pos + 2);

        table[pos] = merge(table[2*pos + 1], table[2*pos + 2]);
    }

    void set(int k, int v, int l, int r, int pos)
    {
        if (l == r)
        {
            table[pos].first = v;
            table[pos].second = (v > -1e7);
            return;
        }
        propagate(pos);
        if (k <= (l + r)/2) set(k, v, l, (l + r)/2, 2*pos + 1);
        else set(k, v, (l + r)/2 + 1, r, 2*pos + 2);

        table[pos] = merge(table[2*pos + 1], table[2*pos + 2]);
    }
};


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

	vector<pair<int,int>> comp;
	
	for (int i = 0; i < n; i++) comp.emplace_back(a[i], i);
	for (int i = 0; i < q; i++) comp.emplace_back(v[i], x[i]);

	sort(comp.begin(), comp.end());
	comp.erase(unique(comp.begin(), comp.end()), comp.end());

	for (int i = 0; i < n; i++) a[i] = lower_bound(comp.begin(), comp.end(), make_pair(a[i], i)) - comp.begin();
	for (int i = 0; i < q; i++) v[i] = lower_bound(comp.begin(), comp.end(), make_pair(v[i], x[i])) - comp.begin();

	int vs = comp.size();

	segtree st;
	st.op.resize(4*vs);
	st.table.resize(4*vs, {-1e8, 0});

    auto insert = [&](int pos, int ind)
    {
        int lf = st.query(0, ind - 1, 0, vs - 1, 0).second;
        st.update(ind + 1, vs - 1, -1, 0, vs - 1, 0);
        st.set(ind, pos - lf, 0, vs - 1, 0);
    };

    auto remove = [&](int ind)
    {
        st.update(ind + 1, vs - 1, 1, 0, vs - 1, 0);
        st.set(ind, -1e8, 0, vs - 1, 0);
    };

    for (int i = 0; i < n; i++)
    {
        insert(i, a[i]);
    }

    vector<int> res;
    for (int i = 0; i < q; i++)
    {
        remove(a[x[i]]);
        insert(x[i], v[i]);
        a[x[i]] = v[i];
        res.push_back(st.query(0, vs - 1, 0, vs - 1, 0).first);
    }
    return res;
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 384 KB Output is correct
2 Correct 3 ms 512 KB Output is correct
3 Correct 6 ms 640 KB Output is correct
4 Correct 6 ms 640 KB Output is correct
5 Correct 6 ms 640 KB Output is correct
6 Correct 8 ms 640 KB Output is correct
7 Correct 7 ms 640 KB Output is correct
8 Correct 6 ms 640 KB Output is correct
9 Correct 6 ms 640 KB Output is correct
10 Correct 6 ms 640 KB Output is correct
11 Correct 6 ms 640 KB Output is correct
12 Correct 7 ms 640 KB Output is correct
13 Correct 8 ms 640 KB Output is correct
14 Correct 7 ms 640 KB Output is correct
15 Correct 7 ms 640 KB Output is correct
16 Correct 6 ms 640 KB Output is correct
17 Correct 7 ms 640 KB Output is correct
18 Correct 6 ms 640 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 384 KB Output is correct
2 Correct 3 ms 512 KB Output is correct
3 Correct 6 ms 640 KB Output is correct
4 Correct 6 ms 640 KB Output is correct
5 Correct 6 ms 640 KB Output is correct
6 Correct 8 ms 640 KB Output is correct
7 Correct 7 ms 640 KB Output is correct
8 Correct 6 ms 640 KB Output is correct
9 Correct 6 ms 640 KB Output is correct
10 Correct 6 ms 640 KB Output is correct
11 Correct 6 ms 640 KB Output is correct
12 Correct 7 ms 640 KB Output is correct
13 Correct 8 ms 640 KB Output is correct
14 Correct 7 ms 640 KB Output is correct
15 Correct 7 ms 640 KB Output is correct
16 Correct 6 ms 640 KB Output is correct
17 Correct 7 ms 640 KB Output is correct
18 Correct 6 ms 640 KB Output is correct
19 Correct 23 ms 1568 KB Output is correct
20 Correct 28 ms 1660 KB Output is correct
21 Correct 26 ms 1660 KB Output is correct
22 Correct 27 ms 1664 KB Output is correct
23 Correct 25 ms 1536 KB Output is correct
24 Correct 25 ms 1536 KB Output is correct
25 Correct 28 ms 1656 KB Output is correct
26 Correct 26 ms 1664 KB Output is correct
27 Correct 24 ms 1536 KB Output is correct
28 Correct 24 ms 1536 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 38 ms 2472 KB Output is correct
2 Correct 118 ms 5096 KB Output is correct
3 Correct 205 ms 8100 KB Output is correct
4 Correct 200 ms 7976 KB Output is correct
5 Correct 200 ms 7944 KB Output is correct
6 Correct 196 ms 7884 KB Output is correct
7 Correct 202 ms 7912 KB Output is correct
8 Correct 196 ms 7916 KB Output is correct
9 Correct 200 ms 7920 KB Output is correct
10 Correct 175 ms 5996 KB Output is correct
11 Correct 172 ms 5976 KB Output is correct
12 Correct 172 ms 5996 KB Output is correct
13 Correct 185 ms 5996 KB Output is correct
14 Correct 173 ms 5996 KB Output is correct
15 Correct 179 ms 6000 KB Output is correct
16 Correct 159 ms 5940 KB Output is correct
17 Correct 160 ms 5940 KB Output is correct
18 Correct 161 ms 5944 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 384 KB Output is correct
2 Correct 3 ms 512 KB Output is correct
3 Correct 6 ms 640 KB Output is correct
4 Correct 6 ms 640 KB Output is correct
5 Correct 6 ms 640 KB Output is correct
6 Correct 8 ms 640 KB Output is correct
7 Correct 7 ms 640 KB Output is correct
8 Correct 6 ms 640 KB Output is correct
9 Correct 6 ms 640 KB Output is correct
10 Correct 6 ms 640 KB Output is correct
11 Correct 6 ms 640 KB Output is correct
12 Correct 7 ms 640 KB Output is correct
13 Correct 8 ms 640 KB Output is correct
14 Correct 7 ms 640 KB Output is correct
15 Correct 7 ms 640 KB Output is correct
16 Correct 6 ms 640 KB Output is correct
17 Correct 7 ms 640 KB Output is correct
18 Correct 6 ms 640 KB Output is correct
19 Correct 23 ms 1568 KB Output is correct
20 Correct 28 ms 1660 KB Output is correct
21 Correct 26 ms 1660 KB Output is correct
22 Correct 27 ms 1664 KB Output is correct
23 Correct 25 ms 1536 KB Output is correct
24 Correct 25 ms 1536 KB Output is correct
25 Correct 28 ms 1656 KB Output is correct
26 Correct 26 ms 1664 KB Output is correct
27 Correct 24 ms 1536 KB Output is correct
28 Correct 24 ms 1536 KB Output is correct
29 Correct 38 ms 2472 KB Output is correct
30 Correct 118 ms 5096 KB Output is correct
31 Correct 205 ms 8100 KB Output is correct
32 Correct 200 ms 7976 KB Output is correct
33 Correct 200 ms 7944 KB Output is correct
34 Correct 196 ms 7884 KB Output is correct
35 Correct 202 ms 7912 KB Output is correct
36 Correct 196 ms 7916 KB Output is correct
37 Correct 200 ms 7920 KB Output is correct
38 Correct 175 ms 5996 KB Output is correct
39 Correct 172 ms 5976 KB Output is correct
40 Correct 172 ms 5996 KB Output is correct
41 Correct 185 ms 5996 KB Output is correct
42 Correct 173 ms 5996 KB Output is correct
43 Correct 179 ms 6000 KB Output is correct
44 Correct 159 ms 5940 KB Output is correct
45 Correct 160 ms 5940 KB Output is correct
46 Correct 161 ms 5944 KB Output is correct
47 Correct 738 ms 25360 KB Output is correct
48 Correct 2823 ms 77120 KB Output is correct
49 Correct 3200 ms 83920 KB Output is correct
50 Correct 3409 ms 83828 KB Output is correct
51 Correct 3294 ms 83832 KB Output is correct
52 Correct 3166 ms 83808 KB Output is correct
53 Correct 3107 ms 83804 KB Output is correct
54 Correct 2793 ms 83976 KB Output is correct
55 Correct 3311 ms 83924 KB Output is correct
56 Correct 2998 ms 83944 KB Output is correct
57 Correct 2946 ms 84000 KB Output is correct
58 Correct 2794 ms 84068 KB Output is correct
59 Correct 2524 ms 76888 KB Output is correct
60 Correct 2585 ms 76884 KB Output is correct
61 Correct 2642 ms 76728 KB Output is correct
62 Correct 2646 ms 73684 KB Output is correct
63 Correct 2638 ms 73672 KB Output is correct
64 Correct 2575 ms 73688 KB Output is correct
65 Correct 2477 ms 70668 KB Output is correct
66 Correct 2590 ms 70604 KB Output is correct
67 Correct 2484 ms 70588 KB Output is correct