Submission #626039

# Submission time Handle Problem Language Result Execution time Memory
626039 2022-08-11T07:14:14 Z I_love_Hoang_Yen Radio Towers (IOI22_towers) C++17
14 / 100
954 ms 1864 KB
#include "bits/stdc++.h"
using namespace std;

/*
// sub 1 {{{
int k = -1;
std::vector<int> a;

void init(int n, std::vector<int> _a) {
    assert((int) _a.size() == n);
    a = _a;
    k = max_element(a.begin(), a.end()) - a.begin();
}

int max_towers(int l, int r, int d) {
    if (k < l || r < k) return 1;
    if (a[l] <= a[k] - d && a[r] <= a[k] - d) return 2;
    return 1;
}
// }}}

// sub 2 {{{
std::vector<int> a;

void init(int n, std::vector<int> _a) {
    assert((int) _a.size() == n);
    a = _a;
}

int max_towers(int l, int r, int d) {
    std::vector<int> f(a.size());
    for (int i = l; i <= r; ++i) {
        f[i] = 1;

        int max_tower = -1;
        for (int j = i - 1; j >= l; --j) {
            if (a[i] <= max_tower - d
                    && a[j] <= max_tower - d) {
                f[i] = std::max(f[i], f[j] + 1);
            }
            max_tower = std::max(max_tower, a[j]);
        }
    }
    return *max_element(f.begin(), f.end());
}
// }}}

// SegTree, copied from AtCoder library {{{
// AtCoder doc: https://atcoder.github.io/ac-library/master/document_en/segtree.html
//
// Notes:
// - Index of elements from 0 -> n-1
// - Range queries are [l, r-1]
//
// Tested:
// - (binary search) https://atcoder.jp/contests/practice2/tasks/practice2_j
// - https://oj.vnoi.info/problem/gss
// - https://oj.vnoi.info/problem/nklineup
// - (max_right & min_left for delete position queries) https://oj.vnoi.info/problem/segtree_itstr
// - https://judge.yosupo.jp/problem/point_add_range_sum
// - https://judge.yosupo.jp/problem/point_set_range_composite
int ceil_pow2(int n) {
    int x = 0;
    while ((1U << x) < (unsigned int)(n)) x++;
    return x;
}

template<
    class T,  // data type for nodes
    T (*op) (T, T),  // operator to combine 2 nodes
    T (*e)() // identity element
>
struct SegTree {
    SegTree() : SegTree(0) {}
    explicit SegTree(int n) : SegTree(vector<T> (n, e())) {}
    explicit SegTree(const vector<T>& v) : _n((int) v.size()) {
        log = ceil_pow2(_n);
        size = 1<<log;
        d = vector<T> (2*size, e());

        for (int i = 0; i < _n; i++) d[size+i] = v[i];
        for (int i = size - 1; i >= 1; i--) {
            update(i);
        }
    }

    // 0 <= p < n
    void set(int p, T x) {
        assert(0 <= p && p < _n);
        p += size;
        d[p] = x;
        for (int i = 1; i <= log; i++) update(p >> i);
    }

    // 0 <= p < n
    T get(int p) const {
        assert(0 <= p && p < _n);
        return d[p + size];
    }

    // Get product in range [l, r-1]
    // 0 <= l <= r <= n
    // For empty segment (l == r) -> return e()
    T prod(int l, int r) const {
        assert(0 <= l && l <= r && r <= _n);
        T sml = e(), smr = e();
        l += size;
        r += size;
        while (l < r) {
            if (l & 1) sml = op(sml, d[l++]);
            if (r & 1) smr = op(d[--r], smr);
            l >>= 1;
            r >>= 1;
        }
        return op(sml, smr);
    }

    T all_prod() const {
        return d[1];
    }

    // Binary search on SegTree to find largest r:
    //    f(op(a[l] .. a[r-1])) = true   (assuming empty array is always true)
    //    f(op(a[l] .. a[r])) = false    (assuming op(..., a[n]), which is out of bound, is always false)
    template <bool (*f)(T)> int max_right(int l) const {
        return max_right(l, [](T x) { return f(x); });
    }
    template <class F> int max_right(int l, F f) const {
        assert(0 <= l && l <= _n);
        assert(f(e()));
        if (l == _n) return _n;
        l += size;
        T sm = e();
        do {
            while (l % 2 == 0) l >>= 1;
            if (!f(op(sm, d[l]))) {
                while (l < size) {
                    l = (2 * l);
                    if (f(op(sm, d[l]))) {
                        sm = op(sm, d[l]);
                        l++;
                    }
                }
                return l - size;
            }
            sm = op(sm, d[l]);
            l++;
        } while ((l & -l) != l);
        return _n;
    }

    // Binary search on SegTree to find smallest l:
    //    f(op(a[l] .. a[r-1])) = true      (assuming empty array is always true)
    //    f(op(a[l-1] .. a[r-1])) = false   (assuming op(a[-1], ..), which is out of bound, is always false)
    template <bool (*f)(T)> int min_left(int r) const {
        return min_left(r, [](T x) { return f(x); });
    }
    template <class F> int min_left(int r, F f) const {
        assert(0 <= r && r <= _n);
        assert(f(e()));
        if (r == 0) return 0;
        r += size;
        T sm = e();
        do {
            r--;
            while (r > 1 && (r % 2)) r >>= 1;
            if (!f(op(d[r], sm))) {
                while (r < size) {
                    r = (2 * r + 1);
                    if (f(op(d[r], sm))) {
                        sm = op(d[r], sm);
                        r--;
                    }
                }
                return r + 1 - size;
            }
            sm = op(d[r], sm);
        } while ((r & -r) != r);
        return 0;
    }

private:
    int _n, size, log;
    vector<T> d;

    void update(int k) {
        d[k] = op(d[2*k], d[2*k+1]);
    }
};
// }}}

// sub 3 {{{
std::vector<int> a, vals;

void init(int n, std::vector<int> _a) {
    assert((int) _a.size() == n);

    // compress
    vals = _a;
    vals.push_back(2000111000);
    std::sort(vals.begin(), vals.end());
    vals.erase(unique(vals.begin(), vals.end()), vals.end());

    for (int i = 0; i < n; ++i) {
        a.push_back(std::lower_bound(vals.begin(), vals.end(), _a[i]) - vals.begin());
    }
}

struct MaxSegTreeOp {
    static int op(int x, int y) {
        return max(x, y);
    }
    static int e() {
        return 0;
    }
};

int max_towers(int l, int r, int d) {
    SegTree<int, MaxSegTreeOp::op, MaxSegTreeOp::e> f(vals.size()), g(vals.size());

    int res = 0;
    for (int i = l; i <= r; ++i) {
        // i: chosen tower
        int min_h = std::lower_bound(vals.begin(), vals.end(), vals[a[i]] + d) - vals.begin();
        int cur_f = 1 + g.prod(min_h, vals.size());
        res = std::max(res, cur_f);
        f.set(a[i], std::max(f.get(a[i]), cur_f));

        // i: not chosen
        int max_h = std::upper_bound(vals.begin(), vals.end(), vals[a[i]] - d) - vals.begin();
        int cur_g = f.prod(0, max_h);
        g.set(a[i], std::max(g.get(a[i]), cur_g));
    }
    return res;
}
// }}}
*/

// sub 4 {{{
std::vector<int> a;
std::vector<int> tops;

void init(int n, std::vector<int> _a) {
    assert((int) _a.size() == n);
    a = _a;

    tops.push_back(-1);
    for (int i = 1; i < n - 1; ++i) {
        if (a[i] > a[i-1] && a[i] > a[i+1]) tops.push_back(i);
    }
    tops.push_back(n + 1);
}

int max_towers(int l, int r, int d) {
    assert(d == 1);
    int first = std::upper_bound(tops.begin(), tops.end(), l) - tops.begin();
    int last = std::lower_bound(tops.begin(), tops.end(), r) - tops.begin();
    
    if (first <= last) return 1 + last - first;
    return 1;
}
// }}}
# Verdict Execution time Memory Grader output
1 Runtime error 12 ms 1316 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 1 ms 464 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 1 ms 464 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 681 ms 1736 KB Output is correct
2 Correct 751 ms 1736 KB Output is correct
3 Correct 830 ms 1808 KB Output is correct
4 Correct 831 ms 1832 KB Output is correct
5 Correct 705 ms 1824 KB Output is correct
6 Correct 730 ms 1824 KB Output is correct
7 Correct 954 ms 1844 KB Output is correct
8 Correct 939 ms 1352 KB Output is correct
9 Correct 582 ms 1456 KB Output is correct
10 Correct 864 ms 1480 KB Output is correct
11 Correct 699 ms 1464 KB Output is correct
12 Correct 664 ms 1444 KB Output is correct
13 Correct 828 ms 1444 KB Output is correct
14 Correct 0 ms 208 KB Output is correct
15 Correct 1 ms 208 KB Output is correct
16 Correct 1 ms 208 KB Output is correct
17 Correct 11 ms 1824 KB Output is correct
18 Correct 11 ms 1816 KB Output is correct
19 Correct 12 ms 1864 KB Output is correct
20 Correct 13 ms 1516 KB Output is correct
21 Correct 11 ms 1444 KB Output is correct
22 Correct 11 ms 1800 KB Output is correct
23 Correct 19 ms 1828 KB Output is correct
24 Correct 10 ms 1816 KB Output is correct
25 Correct 10 ms 1440 KB Output is correct
26 Correct 10 ms 1444 KB Output is correct
27 Correct 1 ms 208 KB Output is correct
28 Correct 1 ms 208 KB Output is correct
29 Correct 1 ms 208 KB Output is correct
30 Correct 1 ms 208 KB Output is correct
31 Correct 1 ms 208 KB Output is correct
32 Correct 1 ms 208 KB Output is correct
33 Correct 1 ms 208 KB Output is correct
34 Correct 1 ms 208 KB Output is correct
35 Correct 1 ms 208 KB Output is correct
36 Correct 1 ms 208 KB Output is correct
# Verdict Execution time Memory Grader output
1 Runtime error 5 ms 1108 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 1 ms 464 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 12 ms 1316 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -