summaryrefslogtreecommitdiff
path: root/datastructures
diff options
context:
space:
mode:
authorJBatzill <batzilljohannes@gmail.com>2014-11-22 20:15:44 +0100
committerJBatzill <batzilljohannes@gmail.com>2014-11-22 20:15:44 +0100
commit9753d518eb3204e380bdf4af24145166896bcf76 (patch)
tree470429feb0cc36ff81306f59f2e0a399795a41df /datastructures
parent3e14dcd6c67c80b4cbc5c7e949a25fe20dc676e2 (diff)
Create RMQ.cpp
Diffstat (limited to 'datastructures')
-rw-r--r--datastructures/RMQ.cpp20
1 files changed, 20 insertions, 0 deletions
diff --git a/datastructures/RMQ.cpp b/datastructures/RMQ.cpp
new file mode 100644
index 0000000..899db15
--- /dev/null
+++ b/datastructures/RMQ.cpp
@@ -0,0 +1,20 @@
+vector<int> data(RMQ_SIZE);
+vector<vector<int>> rmq(floor(log2(RMQ_SIZE)) + 1, vector<int>(RMQ_SIZE));
+
+void initRMQ() {
+ for(int i = 0, s = 1, ss = 1; s <= RMQ_SIZE; ss=s, s*=2, i++) {
+ for(int l = 0; l + s <= RMQ_SIZE; l++) {
+ if(i == 0) rmq[0][l] = l;
+ else {
+ int r = l + ss;
+ rmq[i][l] = (data[rmq[i-1][l]] <= data[rmq[i-1][r]] ? rmq[i-1][l] : rmq[i-1][r]);
+ }
+ }
+ }
+}
+//returns index of minimum! [a, b)
+int queryRMQ(int l, int r) {
+ if(l >= r) return l;
+ int s = floor(log2(r-l)); r = r - (1 << s);
+ return (data[rmq[s][l]] <= data[rmq[s][r]] ? rmq[s][l] : rmq[s][r]);
+}