import java.util.*;
class SegmentTree {
private int[] st, A;
private int n;
private int left (int p) {
return p << 1;
}
private int right(int p) {
return (p << 1) + 1;
}
private void build(int p, int L, int R) {
if (L == R)
st[p] = L;
else {
build(left(p) , L , (L + R) / 2);
build(right(p), (L + R) / 2 + 1, R );
int p1 = st[left(p)], p2 = st[right(p)];
st[p] = (A[p1] <= A[p2]) ? p1 : p2;
} }
private int rmq(int p, int L, int R, int i, int j) {
if (i > R || j < L) return -1;
if (L >= i && R <= j) return st[p];
int p1 = rmq(left(p) , L , (L+R) / 2, i, j);
int p2 = rmq(right(p), (L+R) / 2 + 1, R , i, j);
if (p1 == -1) return p2;
if (p2 == -1) return p1;
return (A[p1] <= A[p2]) ? p1 : p2; }
private int update_point(int p, int L, int R, int idx, int new_value) {
int i = idx, j = idx;
if (i > R || j < L)
return st[p];
if (L == i && R == j) {
A[i] = new_value;
return st[p] = L;
}
int p1, p2;
p1 = update_point(left(p) , L , (L + R) / 2, idx, new_value);
p2 = update_point(right(p), (L + R) / 2 + 1, R , idx, new_value);
return st[p] = (A[p1] <= A[p2]) ? p1 : p2;
}
public SegmentTree(int[] _A) {
A = _A; n = A.length;
st = new int[4 * n];
for (int i = 0; i < 4 * n; i++) st[i] = 0;
build(1, 0, n - 1);
}
public int rmq(int i, int j) { return rmq(1, 0, n - 1, i, j); }
public int update_point(int idx, int new_value) {
return update_point(1, 0, n - 1, idx, new_value); }
}
class Main {
public static void main
(String[] args
) { int[] A = new int[] { 18, 17, 13, 19, 15, 11, 20 };
SegmentTree st = new SegmentTree(A);
System.
out.
printf(" idx 0, 1, 2, 3, 4, 5, 6\n"); System.
out.
printf(" A is {18,17,13,19,15, 11,20}\n"); System.
out.
printf("RMQ(1, 3) = %d\n", st.
rmq(1,
3)); // answer = index 2 System.
out.
printf("RMQ(4, 6) = %d\n", st.
rmq(4,
6)); // answer = index 5 System.
out.
printf("RMQ(3, 4) = %d\n", st.
rmq(3,
4)); // answer = index 4 System.
out.
printf("RMQ(0, 0) = %d\n", st.
rmq(0,
0)); // answer = index 0 System.
out.
printf("RMQ(0, 1) = %d\n", st.
rmq(0,
1)); // answer = index 1 System.
out.
printf("RMQ(0, 6) = %d\n", st.
rmq(0,
6)); // answer = index 5
System.
out.
printf(" idx 0, 1, 2, 3, 4, 5, 6\n"); System.
out.
printf("Now, modify A into {18,17,13,19,15,100,20}\n"); st.update_point(5, 100); // update A[5] from 11 to 100
System.
out.
printf("These values do not change\n"); System.
out.
printf("RMQ(1, 3) = %d\n", st.
rmq(1,
3)); // 2 System.
out.
printf("RMQ(3, 4) = %d\n", st.
rmq(3,
4)); // 4 System.
out.
printf("RMQ(0, 0) = %d\n", st.
rmq(0,
0)); // 0 System.
out.
printf("RMQ(0, 1) = %d\n", st.
rmq(0,
1)); // 1 System.
out.
printf("These values change\n"); System.
out.
printf("RMQ(0, 6) = %d\n", st.
rmq(0,
6)); // 5->2 System.
out.
printf("RMQ(4, 6) = %d\n", st.
rmq(4,
6)); // 5->4 System.
out.
printf("RMQ(4, 5) = %d\n", st.
rmq(4,
5)); // 5->4 }
}
aW1wb3J0IGphdmEudXRpbC4qOwoKY2xhc3MgU2VnbWVudFRyZWUgeyAgICAgICAgIAogIHByaXZhdGUgaW50W10gc3QsIEE7CiAgcHJpdmF0ZSBpbnQgbjsKICBwcml2YXRlIGludCBsZWZ0IChpbnQgcCkgeyAKICAJcmV0dXJuIHAgPDwgMTsgCiAgfQogIHByaXZhdGUgaW50IHJpZ2h0KGludCBwKSB7IAogIAlyZXR1cm4gKHAgPDwgMSkgKyAxOyAKICB9CgogIHByaXZhdGUgdm9pZCBidWlsZChpbnQgcCwgaW50IEwsIGludCBSKSB7CiAgICBpZiAoTCA9PSBSKSAgICAgICAgICAgICAgICAgICAgICAgICAgICAKICAgICAgc3RbcF0gPSBMOyAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgCiAgICBlbHNlIHsgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIAogICAgICBidWlsZChsZWZ0KHApICwgTCAgICAgICAgICAgICAgLCAoTCArIFIpIC8gMik7CiAgICAgIGJ1aWxkKHJpZ2h0KHApLCAoTCArIFIpIC8gMiArIDEsIFIgICAgICAgICAgKTsKICAgICAgaW50IHAxID0gc3RbbGVmdChwKV0sIHAyID0gc3RbcmlnaHQocCldOwogICAgICBzdFtwXSA9IChBW3AxXSA8PSBBW3AyXSkgPyBwMSA6IHAyOwogIH0gfQoKICBwcml2YXRlIGludCBybXEoaW50IHAsIGludCBMLCBpbnQgUiwgaW50IGksIGludCBqKSB7ICAgICAgICAgIAogICAgaWYgKGkgPiAgUiB8fCBqIDwgIEwpIHJldHVybiAtMTsgCiAgICBpZiAoTCA+PSBpICYmIFIgPD0gaikgcmV0dXJuIHN0W3BdOyAgICAgICAgICAgICAgCgogICAgCiAgICBpbnQgcDEgPSBybXEobGVmdChwKSAsIEwgICAgICAgICAgICAgICwgKEwrUikgLyAyLCBpLCBqKTsKICAgIGludCBwMiA9IHJtcShyaWdodChwKSwgKEwrUikgLyAyICsgMSwgUiAgICAgICAgICAsIGksIGopOwoKICAgIGlmIChwMSA9PSAtMSkgcmV0dXJuIHAyOyAgIAogICAgaWYgKHAyID09IC0xKSByZXR1cm4gcDE7ICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIAogICAgcmV0dXJuIChBW3AxXSA8PSBBW3AyXSkgPyBwMSA6IHAyOyB9ICAgICAgICAgIAoKICBwcml2YXRlIGludCB1cGRhdGVfcG9pbnQoaW50IHAsIGludCBMLCBpbnQgUiwgaW50IGlkeCwgaW50IG5ld192YWx1ZSkgewogICAgaW50IGkgPSBpZHgsIGogPSBpZHg7CiAgICBpZiAoaSA+IFIgfHwgaiA8IEwpCiAgICAgIHJldHVybiBzdFtwXTsKICAgIGlmIChMID09IGkgJiYgUiA9PSBqKSB7CiAgICAgIEFbaV0gPSBuZXdfdmFsdWU7IAogICAgICByZXR1cm4gc3RbcF0gPSBMOwogICAgfQogICAgaW50IHAxLCBwMjsKICAgIHAxID0gdXBkYXRlX3BvaW50KGxlZnQocCkgLCBMICAgICAgICAgICAgICAsIChMICsgUikgLyAyLCBpZHgsIG5ld192YWx1ZSk7CiAgICBwMiA9IHVwZGF0ZV9wb2ludChyaWdodChwKSwgKEwgKyBSKSAvIDIgKyAxLCBSICAgICAgICAgICwgaWR4LCBuZXdfdmFsdWUpOwogICAgcmV0dXJuIHN0W3BdID0gKEFbcDFdIDw9IEFbcDJdKSA/IHAxIDogcDI7CiAgfQoKICBwdWJsaWMgU2VnbWVudFRyZWUoaW50W10gX0EpIHsKICAgIEEgPSBfQTsgbiA9IEEubGVuZ3RoOyAgICAgICAgICAgICAgICAgICAKICAgIHN0ID0gbmV3IGludFs0ICogbl07CiAgICBmb3IgKGludCBpID0gMDsgaSA8IDQgKiBuOyBpKyspIHN0W2ldID0gMDsgCiAgICBidWlsZCgxLCAwLCBuIC0gMSk7ICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIAogIH0KCiAgcHVibGljIGludCBybXEoaW50IGksIGludCBqKSB7IHJldHVybiBybXEoMSwgMCwgbiAtIDEsIGksIGopOyB9CgogIHB1YmxpYyBpbnQgdXBkYXRlX3BvaW50KGludCBpZHgsIGludCBuZXdfdmFsdWUpIHsKICAgIHJldHVybiB1cGRhdGVfcG9pbnQoMSwgMCwgbiAtIDEsIGlkeCwgbmV3X3ZhbHVlKTsgfQp9CgpjbGFzcyBNYWluIHsKICBwdWJsaWMgc3RhdGljIHZvaWQgbWFpbihTdHJpbmdbXSBhcmdzKSB7CiAgICBpbnRbXSBBID0gbmV3IGludFtdIHsgMTgsIDE3LCAxMywgMTksIDE1LCAxMSwgMjAgfTsgCiAgICBTZWdtZW50VHJlZSBzdCA9IG5ldyBTZWdtZW50VHJlZShBKTsKCiAgICBTeXN0ZW0ub3V0LnByaW50ZigiICAgICAgICAgICAgICBpZHggICAgMCwgMSwgMiwgMywgNCwgIDUsIDZcbiIpOwogICAgU3lzdGVtLm91dC5wcmludGYoIi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