#include "data_structure/RMQ.hpp"
#pragma once #include<assert.h> #include<vector> #include<stack> #include<numeric> #include<cmath> #include<algorithm> #include"small_rmq.hpp" #include"sparse_table.hpp" #include"../functional/MIN.hpp" #include"../alga/maybe.hpp" /** * @brief RMQ&lt;O(N),O(1)&gt; * @see https://noshi91.hatenablog.com/entry/2018/08/16/125415 */ template<typename T> class RMQ{ constexpr static int b=64; std::vector<T>v; std::vector<small_rmq<T>*>backet; sparse_table<T,MIN<T>>* st=0; public: RMQ(std::vector<T>v):v(v){ std::vector<T>tmp2; for(int i=0;i<(int)v.size();i+=b){ std::vector<T>tmp; T mn=std::numeric_limits<T>::max(); for(int j=0;i+j<(int)v.size()&&j<b;j++){ tmp.push_back(v[i+j]); if(mn>v[i+j])mn=v[i+j]; } tmp2.push_back(mn); backet.push_back(new small_rmq<T>(tmp)); } st=new sparse_table<T,MIN<T>>(tmp2); } T query(int s,int t){ if(s/b==t/b)return v[s/b*b+backet[s/b]->query(s%b,t%b)]; T res=std::numeric_limits<T>::max(); res=std::min(res,v[s/b*b+backet[s/b]->query(s%b,b)]); res=std::min(res,st->get(s/b+1,t/b).unwrap_or(std::numeric_limits<T>::max())); if(t%b!=0)res=std::min(res,v[t/b*b+backet[t/b]->query(0,t%b)]); return res; } };
#line 2 "data_structure/RMQ.hpp" #include<assert.h> #include<vector> #include<stack> #include<numeric> #include<cmath> #include<algorithm> #line 3 "data_structure/small_rmq.hpp" /** * RMQ(small) &lt;O(N),O(1)&gt;(N<=64) */ template<typename T> class small_rmq{ using u64=unsigned long long; std::vector<u64>table; std::vector<T> v; public: small_rmq(std::vector<T> v):v(v){ assert(v.size()<=64); std::vector<int>tmp(v.size()); table.resize(v.size(),0); std::stack<T>stk; for(int i=0;i<(int)v.size();++i){ tmp.resize(v.size()); while(!stk.empty()&&v[stk.top()]>=v[i]){ stk.pop(); } tmp[i]=stk.empty()?-1:stk.top(); stk.emplace(i); } for(int i=0;i<(int)v.size();++i){ if(tmp[i]!=-1)table[i]=table[tmp[i]]|(1ULL<<(tmp[i])); } } T query(int l,int r){ assert(l!=r); const u64 tmp=table[r-1]&~((1ULL<<l)-1); if(tmp==0)return r-1; else return __builtin_ctzll(tmp); } }; #line 3 "data_structure/sparse_table.hpp" #include<functional> #line 2 "alga/maybe.hpp" #include<cassert> /** * @brief Maybe * @see https://ja.wikipedia.org/wiki/%E3%83%A2%E3%83%8A%E3%83%89_(%E3%83%97%E3%83%AD%E3%82%B0%E3%83%A9%E3%83%9F%E3%83%B3%E3%82%B0)#Maybe%E3%83%A2%E3%83%8A%E3%83%89 */ template<typename T> struct maybe{ bool _is_none; T val; maybe():_is_none(true){} maybe(T val):_is_none(false),val(val){} T unwrap()const{ assert(!_is_none); return val; } T unwrap_or(T e)const{ return _is_none?e:val; } bool is_none()const{return _is_none;} bool is_some()const{return !_is_none;} }; template<typename T,typename F> auto expand(F op){ return [&op](const maybe<T>& s,const maybe<T>& t){ if(s.is_none())return t; if(t.is_none())return s; return maybe<T>(op(s.unwrap(),t.unwrap())); }; } #line 7 "data_structure/sparse_table.hpp" /** * @brief SparseTable */ template<typename T,typename F> class sparse_table{ F f; std::vector<std::vector<T>>data; public: sparse_table(std::vector<T> v,F f=F()):f(f){ int n=v.size(),log=log2(n)+1; data.resize(n,std::vector<T>(log)); for(int i=0;i<n;i++)data[i][0]=v[i]; for(int j=1;j<log;j++)for(int i=0;i+(1<<(j-1))<n;i++){ data[i][j]=f(data[i][j-1],data[i+(1<<(j-1))][j-1]); } } maybe<T> get(int l,int r){ if(l==r)return maybe<T>(); if(r<l)std::swap(l,r); int k=std::log2(r-l); return maybe<T>(f(data[l][k],data[r-(1<<k)][k])); } }; #line 3 "functional/MIN.hpp" /** * @brief 最小値 */ template<typename T> struct MIN{ T operator()(const T& s,const T& t){ return std::min(s,t); } }; #line 12 "data_structure/RMQ.hpp" /** * @brief RMQ&lt;O(N),O(1)&gt; * @see https://noshi91.hatenablog.com/entry/2018/08/16/125415 */ template<typename T> class RMQ{ constexpr static int b=64; std::vector<T>v; std::vector<small_rmq<T>*>backet; sparse_table<T,MIN<T>>* st=0; public: RMQ(std::vector<T>v):v(v){ std::vector<T>tmp2; for(int i=0;i<(int)v.size();i+=b){ std::vector<T>tmp; T mn=std::numeric_limits<T>::max(); for(int j=0;i+j<(int)v.size()&&j<b;j++){ tmp.push_back(v[i+j]); if(mn>v[i+j])mn=v[i+j]; } tmp2.push_back(mn); backet.push_back(new small_rmq<T>(tmp)); } st=new sparse_table<T,MIN<T>>(tmp2); } T query(int s,int t){ if(s/b==t/b)return v[s/b*b+backet[s/b]->query(s%b,t%b)]; T res=std::numeric_limits<T>::max(); res=std::min(res,v[s/b*b+backet[s/b]->query(s%b,b)]); res=std::min(res,st->get(s/b+1,t/b).unwrap_or(std::numeric_limits<T>::max())); if(t%b!=0)res=std::min(res,v[t/b*b+backet[t/b]->query(0,t%b)]); return res; } };