int main() { int b, p, l, n, n_input; while(scanf("%d %d %d %d", &b, &p, &l, &n) == 4) { if(b == 0 and p == 0 and l == 0 and n == 0) break; B = b; mod = p; n_input = n; SegmentTree tree; tree.initialize((int)l + 1); char cm; int i, v; while(n_input--) { assert(scanf(" %c %d %d", &cm, &i, &v) == 3); //D(cm); D(i); D(v); // responder al comando cm, i, v if (cm == 'E') { tree.update(i, v); } else if (cm == 'H'){ int ans = tree.query(i, v) % mod; printf("%d\n", ans); } } puts("-"); } return 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; }
// '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); } }
int test() { vui_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 < 10; ++i) { // printf("%d: %d\n", i, log2(i)); } SegmentTree st; st.initialize(v); printf("Testing SegmentTree implementation\n"); printf("----------------------------------\n"); for (size_t i = 0; i < v.size(); ++i) { for (size_t j = i; j < v.size(); ++j) { pui_t one = st.query_max(i, j); pui_t two = naive_query_max(v, i, j); printf("query_max(%u, %u) == (%u, %u)\n", (uint_t)i, (uint_t)j, one.first, two.first); assert_eq(one.first, two.first); } } printf("\n"); return 0; }
void build_random_tree(int n = 100){ t.arr.resize(n); for (int i=0; i<n; ++i) t.arr[i] = random() * (random() % 2 ? -1 : 1); t.initialize(); }