#pragma once
template<classT,T(*op)(T,T)>structSparseTable{private:intn;vector<vector<T>>st;public:SparseTable(){}SparseTable(constvector<T>&arr){n=arr.size();intlog=1;while(n>>log)log++;st=vector<vector<T>>(log);st[0]=vector<T>(arr.begin(),arr.end());for(intk=1;k<log;k++){autostp=st[k-1];autosti=vector<T>(n-(1<<k)+1);for(inti=0;i<sti.size();i++)sti[i]=op(stp[i],stp[i+(1<<(k-1))]);st[k]=sti;}}Tprod(intl,intr)// [l,r){assert(0<=l&&l<r&&r<=n);intj=0;while((1<<j)<=r-l)j++;j--;returnop(st[j][l],st[j][r-(1<<j)]);}};