#pragma once
template<typenameT,intLOG>classF2{boolis_sorted;std::vector<T>B;boolxor_chmin(T&a,Tb){if(a>(a^b)){a^=b;returntrue;}returnfalse;}public:F2():is_sorted(true){}F2(conststd::vector<T>&v):is_sorted(true){for(constT&a:v)add(a);}booladd(Ta){if(B.size()==LOG)returnfalse;for(Te:B)xor_chmin(a,e);if(a){for(T&e:B)xor_chmin(e,a);if(B.size()andB.back()>a)is_sorted=false;B.push_back(a);returntrue;}returnfalse;}std::vector<T>base(){if(!is_sorted){std::ranges::sort(B);is_sorted=true;}returnB;}Tkth_smallest(Tk){assert(!(k>>(B.size())));if(k==0)return0;if(!is_sorted){std::ranges::sort(B);is_sorted=true;}Tres=0;for(inti=0;i<B.size();i++)if((k>>i)&1)res^=B[i];returnres;}Tkth_largest(Tk){assert(!(k>>(B.size())));returnkth_smallest((T{1}<<B.size())-k-1);}boolin(Ta){if(B.size()==LOG)returntrue;for(Te:B)xor_chmin(a,e);returna;}std::optional<std::vector<T>>make(Ta){std::vector<T>v;for(Te:B)if(xor_chmin(a,e))v.push_back(e);if(a)returnstd::nullopt;returnv;}};