コード例 #1
0
    int
    test() {
        SegmentTree st;
        st.initialize(14, 100);

        st.insert(0, 45);
        st.insert(1, 4);
        st.insert(2, 5);
        st.insert(3, 2);
        st.insert(4, 99);
        st.insert(5, 41);
        st.insert(6, 45);
        st.insert(7, 45);
        st.insert(8, 51);
        st.insert(9, 89);
        st.insert(10, 1);
        st.insert(11, 3);
        st.insert(12, 5);
        st.insert(13, 98);

        vi_t v;
        v.push_back(45);
        v.push_back(4);
        v.push_back(5);
        v.push_back(2);
        v.push_back(99);
        v.push_back(41);
        v.push_back(45);
        v.push_back(45);
        v.push_back(51);
        v.push_back(89);
        v.push_back(1);
        v.push_back(3);
        v.push_back(5);
        v.push_back(98);

        for (int i = 0; i < 14; ++i) {
            for (int j = i; j < 14; ++j) {
                pii_t one = st.query_min(i, j);
                pii_t two = naive_query_min(v, i, j);
                printf("query_min(%d, %d) == (%d, %d)\n", i, j, one.INDEX, two.INDEX);
                assert(one.LENGTH == two.LENGTH);
            }
        }

        return 0;
    }
コード例 #2
0
// 'rin' is the input range set
//
// 'rout' is the output range set. It is guaranteed that 'rout'
// contains the mapping needed to re-contruct the least number of
// ranges needed to represent the complete range. Start with rout[0]
// and keep following the indexes till you reach some 'k' for which
// rout[k] == n.
//
// rout[i] contains the next index within rout (in sorted string
// order) where you need to jump to find the next range that completes
// the smallest number of ranges that cover the entire range.
// 
// you will need to index into 'slcp' to locate the index of this
// range in the original string
//
void
compress_ranges(vi_t &rin, vi_t &rout) {
    SegmentTree st;
    rout.clear();
    if (rin.empty()) {
        return;
    }

    const int n = rin.size();
    st.initialize(n + 1, n + 1);
    rout.resize(n);
    st.insert(n, 0);

    for (int i = rin.size() - 1; i >= 0; --i) {
        assert(rin[i] > 0);
        int l = i + 1, r = std::min(n, i+rin[i]);
        pii_t m = st.query_min(l, r);
        rout[i] = m.INDEX;
        st.insert(i, m.LENGTH + 1);
    }
}