Submission #505122

# Submission time Handle Problem Language Result Execution time Memory
505122 2022-01-10T15:37:32 Z tengiz05 Bubble Sort 2 (JOI18_bubblesort2) C++17
100 / 100
2146 ms 51456 KB
#include "bubblesort2.h"
#include <bits/stdc++.h>

constexpr int inf = 1E9;

struct Info {
    int x;
    Info(int x = -inf) : x(x) {}
};
Info operator+(const Info &a, const Info &b) {
    return Info(std::max(a.x, b.x));
}
struct Tag {
    int add;
    Tag(int add = 0) : add(add) {}
};
void apply(Tag &a, const Tag &b) {
    a.add += b.add;
}
void apply(Info &a, const Tag &b) {
    a.x += b.add;
}
template<class Info, class Tag,
    class Merge = std::plus<Info>>
struct LazySegmentTree {
    const int n;
    const Merge merge;
    std::vector<Info> info;
    std::vector<Tag> tag;
    LazySegmentTree(int n) : n(n), merge(Merge()), info(4 << std::__lg(n)), tag(4 << std::__lg(n)) {}
    LazySegmentTree(std::vector<Info> init) : LazySegmentTree(init.size()) {
        std::function<void(int, int, int)> build = [&](int p, int l, int r) {
            if (r - l == 1) {
                info[p] = init[l];
                return;
            }
            int m = (l + r) / 2;
            build(2 * p, l, m);
            build(2 * p + 1, m, r);
            pull(p);
        };
        build(1, 0, n);
    }
    void pull(int p) {
        info[p] = merge(info[2 * p], info[2 * p + 1]);
    }
    void apply(int p, const Tag &v) {
        ::apply(info[p], v);
        ::apply(tag[p], v);
    }
    void push(int p) {
        apply(2 * p, tag[p]);
        apply(2 * p + 1, tag[p]);
        tag[p] = Tag();
    }
    void modify(int p, int l, int r, int x, Info v) {
        if (r - l == 1) {
            info[p] = v;
            return;
        }
        int m = (l + r) / 2;
        push(p);
        if (x < m) {
            modify(2 * p, l, m, x, v);
        } else {
            modify(2 * p + 1, m, r, x, v);
        }
        pull(p);
    }
    void modify(int x, Info v) {
        modify(1, 0, n, x, v);
    }
    Info rangeQuery(int p, int l, int r, int x, int y) {
        if (l >= y || r <= x) {
            return Info();
        }
        if (l >= x && r <= y) {
            return info[p];
        }
        int m = (l + r) / 2;
        push(p);
        return merge(rangeQuery(2 * p, l, m, x, y), rangeQuery(2 * p + 1, m, r, x, y));
    }
    Info rangeQuery(int l, int r) {
        return rangeQuery(1, 0, n, l, r);
    }
    void rangeApply(int p, int l, int r, int x, int y, const Tag &v) {
        if (l >= y || r <= x) {
            return;
        }
        if (l >= x && r <= y) {
            apply(p, v);
            return;
        }
        int m = (l + r) / 2;
        push(p);
        rangeApply(2 * p, l, m, x, y, v);
        rangeApply(2 * p + 1, m, r, x, y, v);
        pull(p);
    }
    void rangeApply(int l, int r, const Tag &v) {
        return rangeApply(1, 0, n, l, r, v);
    }
};
std::vector<int> countScans(std::vector<int> a,std::vector<int> x,std::vector<int> v){
	int Q = x.size();
    int n = a.size();
	std::vector<int> answer(Q);
	
    std::vector<std::pair<int, int>> f;
    for (int i = 0; i < n; i++) {
        f.emplace_back(a[i], i);
    }
    for (int i = 0; i < Q; i++) {
        f.emplace_back(v[i], x[i]);
    }
    
    std::sort(f.begin(), f.end());
    f.erase(std::unique(f.begin(), f.end()), f.end());
    
    int m = f.size();
    LazySegmentTree<Info, Tag> s(m);
    
    auto add = [&](int x, int v) {
        int p = std::lower_bound(f.begin(), f.end(), std::pair(v, x)) - f.begin();
        s.rangeApply(p, p + 1, Tag(inf + x));
        s.rangeApply(p + 1, m, Tag(-1));
    };
    auto rem = [&](int x, int v) {
        int p = std::lower_bound(f.begin(), f.end(), std::pair(v, x)) - f.begin();
        s.rangeApply(p, p + 1, Tag(-(inf + x)));
        s.rangeApply(p + 1, m, Tag(1));
    };
    
    for (int i = 0; i < n; i++) {
        add(i, a[i]);
    }
    
    for (int i = 0; i < Q; i++) {
        rem(x[i], a[x[i]]);
        a[x[i]] = v[i];
        add(x[i], a[x[i]]);
        answer[i] = s.info[1].x;
    }
    
	return answer;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 2 ms 332 KB Output is correct
3 Correct 4 ms 424 KB Output is correct
4 Correct 5 ms 432 KB Output is correct
5 Correct 4 ms 524 KB Output is correct
6 Correct 4 ms 428 KB Output is correct
7 Correct 5 ms 516 KB Output is correct
8 Correct 4 ms 516 KB Output is correct
9 Correct 5 ms 524 KB Output is correct
10 Correct 4 ms 460 KB Output is correct
11 Correct 4 ms 588 KB Output is correct
12 Correct 4 ms 428 KB Output is correct
13 Correct 4 ms 432 KB Output is correct
14 Correct 4 ms 460 KB Output is correct
15 Correct 4 ms 628 KB Output is correct
16 Correct 4 ms 460 KB Output is correct
17 Correct 4 ms 460 KB Output is correct
18 Correct 4 ms 492 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 2 ms 332 KB Output is correct
3 Correct 4 ms 424 KB Output is correct
4 Correct 5 ms 432 KB Output is correct
5 Correct 4 ms 524 KB Output is correct
6 Correct 4 ms 428 KB Output is correct
7 Correct 5 ms 516 KB Output is correct
8 Correct 4 ms 516 KB Output is correct
9 Correct 5 ms 524 KB Output is correct
10 Correct 4 ms 460 KB Output is correct
11 Correct 4 ms 588 KB Output is correct
12 Correct 4 ms 428 KB Output is correct
13 Correct 4 ms 432 KB Output is correct
14 Correct 4 ms 460 KB Output is correct
15 Correct 4 ms 628 KB Output is correct
16 Correct 4 ms 460 KB Output is correct
17 Correct 4 ms 460 KB Output is correct
18 Correct 4 ms 492 KB Output is correct
19 Correct 16 ms 932 KB Output is correct
20 Correct 18 ms 1068 KB Output is correct
21 Correct 18 ms 1064 KB Output is correct
22 Correct 16 ms 964 KB Output is correct
23 Correct 17 ms 1044 KB Output is correct
24 Correct 18 ms 1068 KB Output is correct
25 Correct 16 ms 972 KB Output is correct
26 Correct 19 ms 920 KB Output is correct
27 Correct 16 ms 972 KB Output is correct
28 Correct 15 ms 972 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 23 ms 1436 KB Output is correct
2 Correct 69 ms 2980 KB Output is correct
3 Correct 125 ms 5176 KB Output is correct
4 Correct 127 ms 5320 KB Output is correct
5 Correct 122 ms 5208 KB Output is correct
6 Correct 132 ms 5208 KB Output is correct
7 Correct 135 ms 5332 KB Output is correct
8 Correct 131 ms 5208 KB Output is correct
9 Correct 134 ms 5160 KB Output is correct
10 Correct 129 ms 4128 KB Output is correct
11 Correct 112 ms 4128 KB Output is correct
12 Correct 108 ms 4132 KB Output is correct
13 Correct 107 ms 3996 KB Output is correct
14 Correct 104 ms 4112 KB Output is correct
15 Correct 102 ms 4112 KB Output is correct
16 Correct 98 ms 4044 KB Output is correct
17 Correct 93 ms 4112 KB Output is correct
18 Correct 107 ms 4112 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 2 ms 332 KB Output is correct
3 Correct 4 ms 424 KB Output is correct
4 Correct 5 ms 432 KB Output is correct
5 Correct 4 ms 524 KB Output is correct
6 Correct 4 ms 428 KB Output is correct
7 Correct 5 ms 516 KB Output is correct
8 Correct 4 ms 516 KB Output is correct
9 Correct 5 ms 524 KB Output is correct
10 Correct 4 ms 460 KB Output is correct
11 Correct 4 ms 588 KB Output is correct
12 Correct 4 ms 428 KB Output is correct
13 Correct 4 ms 432 KB Output is correct
14 Correct 4 ms 460 KB Output is correct
15 Correct 4 ms 628 KB Output is correct
16 Correct 4 ms 460 KB Output is correct
17 Correct 4 ms 460 KB Output is correct
18 Correct 4 ms 492 KB Output is correct
19 Correct 16 ms 932 KB Output is correct
20 Correct 18 ms 1068 KB Output is correct
21 Correct 18 ms 1064 KB Output is correct
22 Correct 16 ms 964 KB Output is correct
23 Correct 17 ms 1044 KB Output is correct
24 Correct 18 ms 1068 KB Output is correct
25 Correct 16 ms 972 KB Output is correct
26 Correct 19 ms 920 KB Output is correct
27 Correct 16 ms 972 KB Output is correct
28 Correct 15 ms 972 KB Output is correct
29 Correct 23 ms 1436 KB Output is correct
30 Correct 69 ms 2980 KB Output is correct
31 Correct 125 ms 5176 KB Output is correct
32 Correct 127 ms 5320 KB Output is correct
33 Correct 122 ms 5208 KB Output is correct
34 Correct 132 ms 5208 KB Output is correct
35 Correct 135 ms 5332 KB Output is correct
36 Correct 131 ms 5208 KB Output is correct
37 Correct 134 ms 5160 KB Output is correct
38 Correct 129 ms 4128 KB Output is correct
39 Correct 112 ms 4128 KB Output is correct
40 Correct 108 ms 4132 KB Output is correct
41 Correct 107 ms 3996 KB Output is correct
42 Correct 104 ms 4112 KB Output is correct
43 Correct 102 ms 4112 KB Output is correct
44 Correct 98 ms 4044 KB Output is correct
45 Correct 93 ms 4112 KB Output is correct
46 Correct 107 ms 4112 KB Output is correct
47 Correct 553 ms 18620 KB Output is correct
48 Correct 1975 ms 48696 KB Output is correct
49 Correct 2146 ms 51212 KB Output is correct
50 Correct 2095 ms 51244 KB Output is correct
51 Correct 2122 ms 51284 KB Output is correct
52 Correct 2015 ms 51256 KB Output is correct
53 Correct 2020 ms 51232 KB Output is correct
54 Correct 1779 ms 51396 KB Output is correct
55 Correct 1918 ms 51456 KB Output is correct
56 Correct 1800 ms 51396 KB Output is correct
57 Correct 1898 ms 51424 KB Output is correct
58 Correct 1894 ms 51420 KB Output is correct
59 Correct 1800 ms 50176 KB Output is correct
60 Correct 1724 ms 50156 KB Output is correct
61 Correct 1690 ms 50260 KB Output is correct
62 Correct 1625 ms 49920 KB Output is correct
63 Correct 1637 ms 49928 KB Output is correct
64 Correct 1614 ms 49928 KB Output is correct
65 Correct 1518 ms 49872 KB Output is correct
66 Correct 1500 ms 49780 KB Output is correct
67 Correct 1519 ms 49668 KB Output is correct