#include #include #include #include #include using namespace std; void izpisi(vector v) { for (int x : v) cout << x << " "; cout << endl; } class RMQ { private: int n; int INF=1000000000; vector array; struct Node { int min, begin, end; }; vector tree; public: RMQ(vector a) { n=pow(2,ceil(log2(a.size()))); array = a; array.resize(n,INF); tree.resize(2*n); build(); } void build(int id=1) { if (id>=n) { tree[id] = {array[id-n], id-n, id-n+1}; return; } int left=id*2, right=id*2+1; build(left); build(right); tree[id] = {min(tree[left].min, tree[right].min), tree[left].begin, tree[right].end}; } int query(int l, int r, int id=1) { if (l<=tree[id].begin && tree[id].end<=r) { // znotraj return tree[id].min; } if (r<=tree[id].begin || tree[id].end<=l) { // zunaj return INF; } return min(query(l,r,id*2), query(l,r,id*2+1)); } }; int main() { default_random_engine rnd(123); vector v; //= {6,8,4,5,2,6,3,7,1,2,9,2,7,8,4,2}; int n=1000000; for (int i=0;i