제출 #1366905

#제출 시각아이디문제언어결과실행 시간메모리
1366905atharva_a_bBridges (APIO19_bridges)C++17
16 / 100
31 ms1912 KiB
#include <iostream>
#include <vector>
#include <cstdint>

struct segment_tree {
    int32_t n;
    std::vector<int32_t> tree;

    segment_tree(const std::vector<int32_t>& x) {
        n = x.size();
        tree.assign(4*n, INT32_MIN);
        build(1, 0, n-1, x);
    }

    void build(int32_t node, int32_t l, int32_t r, const std::vector<int32_t>& x) {
        if(l == r) {
            tree[node] = x[l];
        } else {
            build(2*node, l, (l+r)/2, x);
            build((2*node)+1, ((l+r)/2)+1, r, x);
            tree[node] = std::min(tree[2*node], tree[(2*node)+1]);
        }
    }

    void do_update(int32_t node, int32_t l, int32_t r, const int32_t& i, const int32_t& u) {
        if(l == r) {
            tree[node] = u;
            return;
        }

        if(i <= ((l+r)/2)) {
            do_update(2*node, l, (l+r)/2, i, u);
        } else {
            do_update((2*node)+1, ((l+r)/2)+1, r, i, u);
        }

        tree[node] = std::min(tree[2*node], tree[(2*node)+1]);
    }

    int32_t do_rightmost_query(int32_t node, int32_t l, int32_t r, const int32_t& i, const int32_t& w) {
        if(i < l || tree[node] >= w) return -1;
        if(l == r) return l;

        int32_t res = do_rightmost_query((2*node)+1, ((l+r)/2)+1, r, i, w);
        if(res != -1) return res;

        return do_rightmost_query(2*node, l, (l+r)/2, i, w);
    }

    int32_t do_leftmost_query(int32_t node, int32_t l, int32_t r, const int32_t& i, const int32_t& w) {
        if(r < i || tree[node] >= w) return -1;
        if(l == r) return l;

        int32_t res = do_leftmost_query(2*node, l, (l+r)/2, i, w);
        if(res != -1) return res;

        return do_leftmost_query((2*node)+1, ((l+r)/2)+1, r, i, w);
    }

    void update(const int32_t& i, const int32_t& u) {
        do_update(1, 0, n-1, i, u);
    }

    int32_t rightmost_query(const int32_t& i, const int32_t& w) {
        return do_rightmost_query(1, 0, n-1, i, w);
    }

    int32_t leftmost_query(const int32_t& i, const int32_t& w) {
        return do_leftmost_query(1, 0, n-1, i, w);
    }
};

int main(void) {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);

    int32_t n, m;
    std::cin >> n >> m;

    if(n == 1) {
        int32_t q;
        std::cin >> q;
        while(q--) std::cout << "1\n";
        return 0;
    }

    std::vector<int32_t> d(n-1);
    for(auto& e: d) std::cin >> e >> e >> e;

    segment_tree st(d);

    int32_t q;
    std::cin >> q;

    int32_t t, b, r, s, w;
    int32_t lmost_res;

    while(q--) {
        std::cin >> t;

        if(t == 1) {
            std::cin >> b >> r;
            --b;

            st.update(b, r);
        } else {
            std::cin >> s >> w;

            if(s < n) {
                lmost_res = st.leftmost_query(s-1, w);
                if(lmost_res != -1) {
                    lmost_res = lmost_res-(s-1);
                } else {
                    lmost_res = (n-1)-(s-1);
                }
            } else {
                lmost_res = 0;
            }

            std::cout << (1
                        +(s-2 >= 0 ? ((s-2)-st.rightmost_query(s-2, w)) : 0)
                        +lmost_res) << "\n";
        }
    }

    return 0;
}
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…