//#define READ_NUM_TEST_CASES //#define ASSERTS // force asserts on online judge //#define FASTIO //#define DEBUG_BUILD //#include<atablash/splay/common.hpp> //#include<atablash/splay/node.hpp> //#include<atablash/interval-tree.hpp> #include<stdint.h> #include<utility> #include<vector> #include<memory> // abbrevs typedef unsigned int uint; typedef int8_t i8; typedef uint8_t ui8; typedef int16_t i16; typedef uint16_t ui16; typedef int32_t i32; typedef uint32_t ui32; typedef int64_t i64; typedef uint64_t ui64; // old: typedef i64 ll; typedef ui64 ull; typedef std::pair<i32,i32> pii; typedef std::pair<double,double> pdd; typedef std::vector<i32> vi; #define VE std::vector #define umap std::unordered_map #define umultimap std::unordered_multimap #define uset std::unordered_set #define umultiset std::unordered_multiset typedef unsigned short ushort; typedef unsigned char uchar; template<class T> using sptr = std::shared_ptr<T>; template<class T> using uptr = std::unique_ptr<T>; template<class A, class B> using p = std::pair<A, B>; namespace ab{ struct Void {}; // convert to float with enough precision template<typename I>struct FloatFrom{typedef Void R;}; template<>struct FloatFrom<i32>{typedef double R;}; template<>struct FloatFrom<ui32>{typedef double R;}; template<>struct FloatFrom<i64>{typedef long double R;}; template<>struct FloatFrom<ui64>{typedef long double R;}; template<>struct FloatFrom<i16>{typedef float R;}; template<>struct FloatFrom<ui16>{typedef float R;}; template<>struct FloatFrom<i8>{typedef float R;}; template<>struct FloatFrom<ui8>{typedef float R;}; template<>struct FloatFrom<float>{typedef float R;}; template<>struct FloatFrom<double>{typedef double R;}; template<>struct FloatFrom<long double>{typedef long double R;}; // get type capable of storing multiplication result template<typename I>struct Double{typedef Void R;}; template<>struct Double<i8>{typedef i16 R;}; template<>struct Double<ui8>{typedef ui16 R;}; template<>struct Double<i16>{typedef i32 R;}; template<>struct Double<ui16>{typedef ui32 R;}; template<>struct Double<i32>{typedef i64 R;}; template<>struct Double<ui32>{typedef ui64 R;}; template<>struct Double<float>{typedef float R;}; template<>struct Double<double>{typedef double R;}; template<>struct Double<long double>{typedef long double R;}; #if defined __GNUC__ && ( defined __x86_64__ || defined __ppc64__ || defined __MINGW64__ ) typedef __int128_t i128; typedef __uint128_t ui128; template<>struct Double<i64>{typedef i128 R;}; template<>struct Double<ui64>{typedef ui128 R;}; #endif } // enable debug if any debug macro is defined #if (defined _DEBUG || defined DEBUG) && !defined DEBUG_BUILD #define DEBUG_BUILD #endif // DEBUG_BUILD (set or not set) overrides NDEBUG #ifdef DEBUG_BUILD #undef NDEBUG //#pragma message "Compiling in DEBUG mode." #else #ifndef NDEBUG #define NDEBUG #endif #endif //#include <cstdio> //#include <cfloat> //#include<type_traits> #define _CAT(a,b) a##b #define CAT(a,b) _CAT(a,b) #define _10TH(a,b,c,d,e,f,g,h,i,X,...) X #define VA_CNT(...) _10TH(__VA_ARGS__,9,8,7,6,5,4,3,2,1,0) #define VA_SEL(a,...) CAT(a,VA_CNT(__VA_ARGS__))(__VA_ARGS__) #define _VA_GTONE(...) _10TH(__VA_ARGS__,1,1,1,1,1,1,1,1,0,0) #define VA_GTONE(a,...) CAT(a,_VA_GTONE(__VA_ARGS__))(__VA_ARGS__) #define MIN_GCC(a,b,c) (__GNUC__ > (a) || \ (__GNUC__ == (a) && (__GNUC_MINOR__ > (b) || \ (__GNUC_MINOR__ == (b) && \ __GNUC_PATCHLEVEL__ >= (c))))) // TODO: po co to bylo?? /* #include<functional> #if __cplusplus >= 201103L namespace std { template<class T> struct hash { typedef T argument_type; typedef typename std::underlying_type< argument_type >::type underlying_type; typedef typename std::hash< underlying_type >::result_type result_type; result_type operator()( const argument_type& arg ) const { std::hash< underlying_type > hasher; return hasher( static_cast< underlying_type >( arg ) ); } }; } #endif */ #define bac back() #define fro front() #define emp empty() #define fi first #define se second #define siz size() #define ins insert #define era erase #define mp make_pair #define be begin() #define en end() #define rbe rbegin() #define ren rend() #define pub push_back #define puf push_front #define pob pop_back() #define pof pop_front() #define bs binary_search #define lb lower_bound #define ub upper_bound #define pq priority_queue #define si (int) #undef FO #undef OF #define FOR(i,n) for(int i=0; i<(int)(n); ++i) #define FO(i,a,b) for(int i=(a); i<(int)(b); ++i) #define OF(i,a,b) for(int i=(int)(b)-1;i>=(int)(a);--i) #define FE(it,c) for(__typeof((c).begin()) it=(c).begin(); it!=(c).end(); ++it) #define FER(it,c) for(__typeof((c).begin()) it=(c).begin(); it!=(c).end(); ) #define ALL(c) (c).begin(),(c).end() #define ZERO(c) memset(c,0,sizeof(c)) #define AU(v,val) __typeof(val) v(val) const char NL='\n'; const char SP=' '; // Visual C++ intristics #ifdef _MSC_VER #include <intrin.h> #define __builtin_popcount __popcnt #ifdef _WIN64 #define __builtin_popcountll __popcnt64 #else inline int __builtin_popcountll(__int64 a){ return __builtin_popcount((ui32)a) + __builtin_popcount(a >> 32); } #endif #endif // Visual C++ 'and', 'or', 'not' #ifdef _MSC_VER #define and && #define or || #define not ! #endif namespace ab{ template<class T>inline T cbitl(T x,int i){return (x<<i) ^ (x>>(sizeof(T)*8-i));} // cycle bits left inline int popcount(uint i){return __builtin_popcount(i);} inline int popcount(ull i){return __builtin_popcountll(i);} #ifdef __GNUC__ inline int parity(uint i){return __builtin_parity(i);} inline int parity(ull i){return __builtin_parityll(i);} inline int ilog2(uint i){return 31-__builtin_clz(i);} // int log; floor(log2(i)) inline int ilog2(ull i){return 63-__builtin_clzll(i);} #endif template<class T>inline bool isPowerOf2(const T&x){return !(x&(x-1));} struct Static{ Static() = default; Static(const Static&) = delete; Static(Static&&) = delete; Static(const Static&&) = delete; Static& operator=(const Static&) = delete; Static& operator=(Static&&) = delete; Static& operator=(const Static&&) = delete; }; } #include<vector> #include<unordered_map> // ? #include<iostream> #include<fstream> #include<string> #include<algorithm> #include<mutex> // to_string fix for MinGW #include<map> #include<iostream> #if __cplusplus >= 201103L or defined _MSC_VER #include <unordered_map> #include <unordered_set> namespace std{ #else #include <ext/hash_map> #include <ext/hash_set> namespace __gnu_cxx{ #define unordered_map hash_map #define unordered_multimap hash_multimap #define unordered_set hash_set #define unordered_multiset hash_multiset /* template<class K, class V> class unordered_map : public hash_map<K,V>{ public: typename hash_map<K,V>::iterator erase(const typename hash_map<K,V>::iterator& it){ typename hash_map<K,V>::iterator r = it; ++r; if(r!=hash_map<K,V>::end()){ K key = r->first; hash_map<K,V>::erase(it); return hash_map<K,V>::find(key); } else{ hash_map<K,V>::erase(it); return r; } } typename hash_map<K,V>::size_type erase(const K& key){ return hash_map<K,V>::erase(key); } }; */ #endif // 1,2,last (!!!!!) /*template<>*/ template<class A,class B>struct hash<std::map<A,B> >{ size_t operator()(const std::map<A,B>& m) const{ size_t r = m.size(); if(!r)return 0; __typeof(m.begin()) i=m.be; r^=hash<__typeof(i)>()(i); ++i; r^=cbitl(hash<__typeof(i)>()(*i),sizeof(r)*8/4); i=m.end(); --i; r^=cbitl(hash<__typeof(i)>()(*i),sizeof(r)*8/2); return r; } }; // hash pairs: /*template<>*/ template<class A, class B>struct hash<pair<A,B> >{ size_t operator()(const pair<A,B>& p) const{ return hash<A>()(p.first) ^ ab::cbitl(hash<B>()(p.second),sizeof(size_t)*8/2); } }; // hash enums: // // TODO: fix for msvc #if defined __GNUC__ and MIN_GCC(4,8,4) template<class E> struct hash { using sfinae = typename std::enable_if<std::is_enum<E>::value, E>::type; public: size_t operator()(const E&e) const { return std::hash<typename std::underlying_type<E>::type>()((typename std::underlying_type<E>::type)e); } }; #endif } // namespace std / __gnu_cxx // component-wise pair operators template<class A, class B, class C, class D> inline std::pair<A,B>& operator+=(std::pair<A,B>& p, const std::pair<C,D>& o){ p.first+=o.first; p.second+=o.second; return p; } template<class A, class B, class C, class D> inline std::pair<A,B>& operator-=(std::pair<A,B>& p, const std::pair<C,D>& o){ p.first-=o.first; p.second-=o.second; return p; } template<class A, class B, class C, class D> inline std::pair<A,B>& operator*=(std::pair<A,B>& p, const std::pair<C,D>& o){ p.first*=o.first; p.second*=o.second; return p; } template<class A, class B, class C, class D> inline std::pair<A,B>& operator/=(std::pair<A,B>& p, const std::pair<C,D>& o){ p.first/=o.first; p.second/=o.second; return p; } template<class Afi, class Ase, class Bfi, class Bse> inline std::pair<decltype(Afi() + Bfi()), decltype(Ase() + Bse())>& operator+(const std::pair<Afi,Ase>& a, const std::pair<Bfi,Bse>& b){ return std::make_pair(a.fi+b.fi, a.se+b.se); } template<class Afi, class Ase, class Bfi, class Bse> inline std::pair<decltype(Afi() - Bfi()), decltype(Ase() - Bse())>& operator-(const std::pair<Afi,Ase>& a, const std::pair<Bfi,Bse>& b){ return std::make_pair(a.fi-b.fi, a.se-b.se); } template<class Afi, class Ase, class Bfi, class Bse> inline std::pair<decltype(Afi()*Bfi()), decltype(Ase()*Bse())>& operator*(const std::pair<Afi,Ase>& a, const std::pair<Bfi,Bse>& b){ return std::make_pair(a.fi*b.fi, a.se*b.se); } template<class Afi, class Ase, class Bfi, class Bse> inline std::pair<decltype(Afi() / Bfi()), decltype(Ase() / Bse())>& operator/(const std::pair<Afi,Ase>& a, const std::pair<Bfi,Bse>& b){ return std::make_pair(a.fi/b.fi, a.se/b.se); } // vector operators template<class T, class U> inline std::vector<T>& operator+=(std::vector<T>& v, const std::vector<U>& o){ ASS(v.siz == o.siz); FOR(i,v.siz) v[i] += o[i]; return v; } template<class T> inline std::vector<T>& operator*=(std::vector<T>& v, const T& a){ FOR(i,v.siz) v[i] *= a; return v; } // serialization template<class A,class B>inline std::ostream&operator<<(std::ostream&o,const std::pair<A,B>&p){o<<'<'<<p.first<<", "<<p.second<<'>';return o;} template<class A,class B>inline std::ostream&operator<<(std::ostream&o,const std::map<A,B>&m){o<<'\n';FE(k,m)o<<"map["<<k->first<<"] -> "<<k->se<<'\n';return o;} template<class A,class B>inline std::ostream&operator<<(std::ostream&o,const umap<A,B>&m){o<<'\n';FE(k,m)o<<"umap["<<k->first<<"] -> "<<k->se<<'\n';return o;} template<class A,class B>inline std::ostream&operator<<(std::ostream&o,const uset<A,B>&_m){o<<"uset{\n";FE(k,_m)o<<*k<<'\n';o<<'\n';return o;} template<class A>inline std::ostream&operator<<(std::ostream&o,const std::vector<A>&_v){o<<'\n';FOR(i,_v.siz)o<<"vector["<<i<<"] = "<<_v[i]<<"}\n";return o;} // fix for MinGW: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=52015 #ifdef _GLIBCXX_HAVE_BROKEN_VSWPRINTF namespace std{ template<class T> inline string to_string(const T& t){ stringstream ss; ss << t; return ss.str(); } } #endif //#define LOG0(str) fprintf(stderr,"%s:%d - " str "\n",__FILE__,__LINE__) //#define LOG1(str,...) fprintf(stderr,"%s:%d - " str "\n",__FILE__,__LINE__,__VA_ARGS__) //#define LOG(...) VA_GTONE(LOG,__VA_ARGS__) #define LOGD(str) if(ab::Log::global()) ab::Log::global()->debug(str,__FILE__,__LINE__) #define LOGI(str) if(ab::Log::global()) ab::Log::global()->info(str,__FILE__,__LINE__) #define LOGW(str) if(ab::Log::global()) ab::Log::global()->warning(str,__FILE__,__LINE__) #define LOGE(str) if(ab::Log::global()) ab::Log::global()->error(str,__FILE__,__LINE__) #define LOGF(str) if(ab::Log::global()) ab::Log::global()->fail(str,__FILE__,__LINE__) namespace ab{ // TODO: czas logowania // TODO: interfejs do dodawania nazw message levelom, ustawiania nazwy pliku z logiem, czy printowac na stderr, ... // // level >= DEBUG powoduje ignorowanie logowania w release class Log{ public: enum{ DEBUG = 5000, INFO = 4000, // default? WARNING = 3000, ERROR = 2000, FAIL = 1000}; public: void debug(const std::string& _message, const std::string& file="", int line=0){ message(DEBUG, _message, file, line); } void info(const std::string& _message, const std::string& file = "", int line = 0){ message(INFO, _message, file, line); } void warning(const std::string& _message, const std::string& file = "", int line = 0){ message(WARNING, _message, file, line); } void error(const std::string& _message, const std::string& file = "", int line = 0){ message(ERROR, _message, file, line); } void fail(const std::string& _message, const std::string& file = "", int line = 0){ message(FAIL, _message, file, line); } // slow void flushConsole(){ // avoid slow cout during lock // copy messages to print static const uint max_messages_flush = 128; std::vector<Message> messages_copy; { std::lock_guard<std::mutex> lock(mut); messages_copy.assign(messages.begin() + numPrinted, messages.begin() + numPrinted + std::min(max_messages_flush, (uint)messages.size() - numPrinted)); } for (uint i = 0; i < messages_copy.size(); ++i){ std::cout << messageToString(messages_copy[i]) << '\n'; } std::cout.flush(); numPrinted += messages_copy.size(); } void message(int level, const std::string& messageText, const std::string& file = "", int line = 0){ #if !defined DEBUG_BUILD if (level >= DEBUG) return; #endif std::lock_guard<std::mutex> lock(mut); messages.push_back(Message{ level, messageText, file, line }); //if(autoFlush) flushConsole(); #ifdef DEBUG_BUILD if (level <= ERROR){ #ifdef _MSC_VER __debugbreak(); #endif #ifdef __GNUC__ __builtin_trap(); #endif } #endif } // reference to pointer. client should create the log himself static Log*& global(){ static Log* log = nullptr; return log; } public: Log(){ levelNames[DEBUG] = "DEBUG"; levelNames[INFO] = "INFO"; levelNames[WARNING] = "WARNING"; levelNames[ERROR] = "ERROR"; levelNames[FAIL] = "FAIL"; } ~Log(){ if (filename != ""){ std::ofstream os(filename); for (auto&m : messages){ os << messageToString(m) << '\n'; } } } //bool autoFlush = false; private: struct Message{ int level; std::string text; std::string file; int line; }; static std::string shortenPath(const std::string& s){ std::string result = s; std::string ss[] = {"include/", "include\\", "src/", "src\\"}; for(uint i=0; i<sizeof(ss)/sizeof(std::string); ++i){ auto pos = result.rfind(ss[i]); if(pos == std::string::npos) continue; result = result.substr(pos + ss[i].size(), std::string::npos); } return result; } std::string messageToString(const Message& m) const { std::string levelName; auto r = levelNames.find(m.level); if (r == levelNames.end()) levelName = std::to_string(m.level); else levelName = r->second; std::string result = levelName + ": " + m.text; if (m.file != "") result += " at " + shortenPath(m.file) + ":" + std::to_string(m.line); return result; } uint numPrinted = 0; std::vector<Message> messages; std::mutex mut; // TODO: spinlock std::unordered_map<int, std::string> levelNames; std::string filename = "atablash.log"; private: Log(const Log&) = delete; Log& operator=(const Log&) = delete; }; } inline void breakpoint(){} #ifdef NDEBUG #define DE if(0) #else // DEBUG #define DE #endif #if defined NDEBUG && not defined ASSERTS #define ASS(x) #define FLOAT_OK(x) #define ASSERT_FLOAT(x) #else #include<cassert> #define ASS(x) assert(x) #define FLOAT_OK(x) ((x) <= LDBL_MAX && (x) >= -LDBL_MAX) #define ASSERT_FLOAT(f) ASS(FLOAT_OK(f)) #endif #define DV(x) (E<#x<": "<(x)<N) #define XX E<"XX at line "<__LINE__<NL; #if defined DEBUG_BUILD or defined COLOR_CONSOLE #ifdef _WIN32 #ifndef NOMINMAX #define NOMINMAX #endif #ifndef WIN32_LEAN_AND_MEAN #define WIN32_LEAN_AND_MEAN #endif #include<windows.h> inline void ec(int a){static HANDLE h=GetStdHandle(-12);SetConsoleTextAttribute(h,a?12:0xf);} // color 12 inline void oc(int a){static HANDLE h=GetStdHandle(-11);SetConsoleTextAttribute(h,a?10:0xf);} // color 10 #else // *nix includes for debug #endif #else #define ec(x) #define oc(x) #endif #include <iostream> struct _E{template<class T>_E&operator<(const T&_t){DE{ec(1);std::cerr<<_t;ec(0);}return*this;}}; #ifdef __GNUC__ #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wunused-variable" // not working #endif static _E E; #ifdef __GNUC__ #pragma GCC diagnostic pop #endif // E unused #ifdef __GNUC__ template<class T>void ___fhsdhsdhrtsh(){(void)E;} #endif #include<cstdlib> namespace ab { /* template<typename Ret, typename T> class Binder { public: Binder(Ret T::*_var) : var(_var) {} Ret& operator()(T& obj) const { return obj.*var; } const Ret& operator()(const T& obj) const { return obj.*var; } private: Ret T::*var; }; template<typename Ret, typename T> Binder<Ret,T> bind(Ret T::*var) { return Binder<Ret,T>(var); }*/ /* template<class A, class B> __typeof(A()+B()) plus(const A& a, const B& b){return a+b;} template<class A, class B> __typeof(A()-B()) minus(const A& a, const B& b){return a-b;} template<class A, class B> __typeof(A()*B()) multiplies(const A& a, const B& b){return a*b;} template<class A, class B> __typeof(A()/B()) divides(const A& a, const B& b){return a/b;} template<class A, class B> A& plus_assign(A& a,const B& b){return a+=b;} template<class A, class B> A& minus_assign(A& a,const B& b){return a-=b;} template<class A, class B> A& multiplies_assign(A& a,const B& b){return a*=b;} template<class A, class B> A& divides_assign(A& a,const B& b){return a/=b;} */ } //#include<type_traits> namespace ab { template<bool cond, typename T, typename F> struct IF {typedef T R;}; template<typename T, typename F> struct IF<false,T,F> {typedef F R;}; template <bool B, class T = void> struct ENABLE_IF {typedef T R;}; template <class T> struct ENABLE_IF<false, T> {}; template <bool B, class T = void> struct DISABLE_IF {typedef T R;}; template <class T> struct DISABLE_IF<true, T> {}; template<class A, class B> struct SAME{ static const bool r = false;}; template<typename T> struct SAME<T,T>{ static const bool r = true;}; template<typename T> struct IS_CLASS{ template<typename C> static char (&f(int C::*))[1]; template<typename C> static char (&f(...))[2]; static bool const r = sizeof(f<T>(0))==sizeof(char); }; // remove reference template<class T> struct RR{typedef T R;}; template<class T> struct RR<T&>{typedef T R;}; //remove const template<class T> struct RC{typedef T R;}; template<class T> struct RC<const T>{typedef T R;}; // remove const and reference template<class T> struct RRC{typedef typename RC<typename RR<T>::R>::R R;}; #define TYPE(T,...) typename T<__VA_ARGS__>::R struct TRU{static const bool r = true; typedef TRU R;}; struct FAL{static const bool r = false; typedef FAL R;}; // SFINAE test #define HAS_MEMBER(member_alias, member) \ template<typename __T> struct HAS_CLASS_##member_alias { \ struct Fallback { void member(); }; \ struct Derived : __T, Fallback { }; \ \ template<typename C, C> struct ChT; \ \ template<typename C> static char (&f(ChT<void (Fallback::*)(), &C::member>*))[1]; \ template<typename C> static char (&f(...))[2]; \ \ static const bool r = sizeof(f<Derived>(0)) == 2; \ }; \ template<typename __T> \ struct HAS_##member_alias \ { \ typedef typename IF<IS_CLASS<__T>::r, HAS_CLASS_##member_alias<__T>, FAL>::R R; \ static const bool r = R::r; \ }; // Dzia│a dok│adnie, uwzglŕdnia typ (const, referencje) wyniku i argumentˇw, uwzglŕdnia const membera // Przyk│ad dla non-static klasy A: // typedef int (A::*sign1)(unsigned short&); // Przyk│ad dla static dowolnej klasy: // typedef int (*sign1)(unsigned short&); #define HAS_MEMBER_SIG(member_name, member_signature) \ template<typename CLS> \ struct HAS_SIG_##member_name \ { \ template<typename C, C> struct ChT; \ template<typename C> static char (&f(ChT<member_signature, &C::member_name>*))[1]; \ template<typename C> static char (&f(...))[2]; \ static const bool r = sizeof(f<CLS>(0)) == sizeof(char); \ }; HAS_MEMBER(OPERATOR_CALL, operator()) /* // get &T::x or &T::first template<class T,class=void>struct GET_X{}; template<class T>struct GET_X<T,typename std::enable_if<std::is_object<decltype(T::x)>::value>::type>{typedef decltype(&T::x) R;}; template<class T>struct GET_X<T,typename std::enable_if<std::is_object<decltype(T::first)>::value>::type>{typedef decltype(&T::first) R;}; // get &T::y or &T::second template<class T,class=void>struct GET_Y{}; template<class T>struct GET_Y<T,typename std::enable_if<std::is_object<decltype(T::y)>::value>::type>{typedef decltype(&T::y) R;}; template<class T>struct GET_Y<T,typename std::enable_if<std::is_object<decltype(T::second)>::value>::type>{typedef decltype(&T::second) R;}; */ } #include<utility> namespace ab{ struct Identity{ template<class TT> TT operator()(TT&& x)const { return std::forward<TT>(x); } template<class TT> TT operator[](TT&& x) const { return std::forward<TT>(x); } }; } namespace ab{ struct Lambda1{ template<class A1> const A1& operator()(const A1& a1)const{return a1;} template<class A1> A1& operator()( A1& a1)const{return a1;} template<class A1,class A2> const A1& operator()(const A1& a1, const A2&)const{return a1;} template<class A1,class A2> A1& operator()( A1& a1, const A2&)const{return a1;} template<class A1,class A2,class A3> const A1& operator()(const A1& a1, const A2&, const A3&)const{return a1;} template<class A1,class A2,class A3> A1& operator()( A1& a1, const A2&, const A3&)const{return a1;} }; struct Lambda2{ template<class A1,class A2> const A2& operator()(const A1& a1, const A2& a2)const{return a2;} template<class A1,class A2> A2& operator()(const A1& a1, A2& a2)const{return a2;} template<class A1,class A2,class A3> const A2& operator()(const A1& a1, const A2& a2, const A3& a3)const{return a2;} template<class A1,class A2,class A3> A2& operator()(const A1& a1, A2& a2, const A3& a3)const{return a2;} }; struct Lambda3{ template<class A1,class A2,class A3> const A3& operator()(const A1& a1, const A2& a2, const A3& a3)const{return a3;} template<class A1,class A2,class A3> A3& operator()(const A1& a1, const A2& a2, A3& a3)const{return a3;} }; #ifdef __GNUC__ #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wunused-variable" // not working #endif static Lambda1 _1; static Lambda2 _2; static Lambda3 _3; //#define _1 Lambda1() //#define _2 Lambda2() //#define _3 Lambda3() #ifdef __GNUC__ #pragma GCC diagnostic pop #endif #ifdef __GNUC__ // _1 _2 _3 unused // pragma diagnostic ignored not working? template<class T>void ___fhsdhsdhrtshx(){(void)_1;(void)_2;(void)_3;} #endif template<class L, class A1, class A2, class A3> struct RET_TYPE3{typedef L& R;}; template<class L, class A1> struct RET_TYPE1{typedef typename RET_TYPE3<L,A1,A1,A1>::R R;}; template<class L, class A1, class A2> struct RET_TYPE2{typedef typename RET_TYPE3<L,A1,A2,A2>::R R;}; // is lambda before evaluation template<class L> struct IS_LAMBDA{static const bool r = false;}; // is lambda after evaluation template<class L, class A1, class A2, class A3> struct IS_LAMBDA3{ static const bool r = !SAME<L&, typename RET_TYPE3<L,A1,A2,A3>::R&>::r;}; template<class L, class A1, class A2> struct IS_LAMBDA2{ static const bool r = !SAME<typename RR<L>::R, typename RR<typename RET_TYPE2<L,A1,A2>::R>::R>::r;}; template<class L, class A1> struct IS_LAMBDA1{ static const bool r = !SAME<typename RR<L>::R, typename RR<typename RET_TYPE1<L,A1>::R>::R>::r;}; template<class T, class A1> typename ENABLE_IF < IS_LAMBDA1<T,A1>::r, typename RET_TYPE1<T,const A1>::R>::R Eval(const T& t, const A1& a1){return t(a1);} template<class T, class A1> typename ENABLE_IF < IS_LAMBDA1<T,A1>::r, typename RET_TYPE1<T, A1>::R>::R Eval(const T& t, A1& a1){return t(a1);} template<class T, class A1> typename DISABLE_IF < IS_LAMBDA1<T,A1>::r, typename RET_TYPE1<T,A1>::R&>::R Eval( T& t, const A1& a1){return t;} template<class T, class A1, class A2> typename ENABLE_IF < IS_LAMBDA2<T,A1,A2>::r, typename RET_TYPE2<T,const A1,const A2>::R>::R Eval(const T& t, const A1& a1, const A2& a2){return t(a1,a2);} template<class T, class A1, class A2> typename ENABLE_IF < IS_LAMBDA2<T,A1,A2>::r, typename RET_TYPE2<T,const A1, A2>::R>::R Eval(const T& t, const A1& a1, A2& a2){return t(a1,a2);} template<class T, class A1, class A2> typename ENABLE_IF < IS_LAMBDA2<T,A1,A2>::r, typename RET_TYPE2<T, A1,const A2>::R>::R Eval(const T& t, A1& a1, const A2& a2){return t(a1,a2);} template<class T, class A1, class A2> typename ENABLE_IF < IS_LAMBDA2<T,A1,A2>::r, typename RET_TYPE2<T, A1, A2>::R>::R Eval(const T& t, A1& a1, A2& a2){return t(a1,a2);} template<class T, class A1, class A2> typename DISABLE_IF< IS_LAMBDA2<T,A1,A2>::r, typename RET_TYPE2<T,A1,A2>::R&>::R Eval( T& t, const A1& a1, const A2& a2){return t;} template<class T, class A1, class A2, class A3> typename ENABLE_IF < IS_LAMBDA3<T,A1,A2,A3>::r, typename RET_TYPE3<T,const A1,const A2,const A3>::R>::R Eval(const T& t, const A1& a1, const A2& a2, const A3& a3){return t(a1,a2,a3);} template<class T, class A1, class A2, class A3> typename ENABLE_IF < IS_LAMBDA3<T,A1,A2,A3>::r, typename RET_TYPE3<T,const A1,const A2, A3>::R>::R Eval(const T& t, const A1& a1, const A2& a2, A3& a3){return t(a1,a2,a3);} template<class T, class A1, class A2, class A3> typename ENABLE_IF < IS_LAMBDA3<T,A1,A2,A3>::r, typename RET_TYPE3<T,const A1, A2,const A3>::R>::R Eval(const T& t, const A1& a1, A2& a2, const A3& a3){return t(a1,a2,a3);} template<class T, class A1, class A2, class A3> typename ENABLE_IF < IS_LAMBDA3<T,A1,A2,A3>::r, typename RET_TYPE3<T,const A1, A2, A3>::R>::R Eval(const T& t, const A1& a1, A2& a2, A3& a3){return t(a1,a2,a3);} template<class T, class A1, class A2, class A3> typename ENABLE_IF < IS_LAMBDA3<T,A1,A2,A3>::r, typename RET_TYPE3<T, A1,const A2,const A3>::R>::R Eval(const T& t, A1& a1, const A2& a2, const A3& a3){return t(a1,a2,a3);} template<class T, class A1, class A2, class A3> typename ENABLE_IF < IS_LAMBDA3<T,A1,A2,A3>::r, typename RET_TYPE3<T, A1,const A2, A3>::R>::R Eval(const T& t, A1& a1, const A2& a2, A3& a3){return t(a1,a2,a3);} template<class T, class A1, class A2, class A3> typename ENABLE_IF < IS_LAMBDA3<T,A1,A2,A3>::r, typename RET_TYPE3<T, A1, A2,const A3>::R>::R Eval(const T& t, A1& a1, A2& a2, const A3& a3){return t(a1,a2,a3);} template<class T, class A1, class A2, class A3> typename ENABLE_IF < IS_LAMBDA3<T,A1,A2,A3>::r, typename RET_TYPE3<T, A1, A2, A3>::R>::R Eval(const T& t, A1& a1, A2& a2, A3& a3){return t(a1,a2,a3);} template<class T, class A1, class A2, class A3> typename DISABLE_IF < IS_LAMBDA3<T,A1,A2,A3>::r, typename RET_TYPE3<T,A1,A2,A3>::R&>::R Eval( T& t, const A1& a1, const A2& a2, const A3& a3){return t;} template<class L1, class L2> class LambdaPlus{ public: LambdaPlus(const L1& _l1, const L2& _l2) : l1(_l1), l2(_l2){} template<class A1> typename RET_TYPE1<LambdaPlus,A1>::R operator()(const A1& a1)const{ return Eval(l1,a1) + Eval(l2,a1); } template<class A1, class A2> typename RET_TYPE2<LambdaPlus,A1,A2>::R operator()(const A1& a1, const A2& a2)const{ return Eval(l1,a1,a2) + Eval(l2,a1,a2); } template<class A1, class A2, class A3> typename RET_TYPE3<LambdaPlus,A1,A2,A3>::R operator()(const A1& a1, const A2& a2, const A3& a3)const{ return Eval(l1,a1,a2,a3) + Eval(l2,a1,a2,a3); } private: const L1& l1; const L2& l2; }; template<class L1, class L2> typename ENABLE_IF<HAS_OPERATOR_CALL<L1>::r or HAS_OPERATOR_CALL<L2>::r, LambdaPlus<L1,L2> >::R operator+(const L1& l1, const L2& l2){ return LambdaPlus<L1,L2>(l1,l2); } template<class L1, class L2> class LambdaMinus{ public: LambdaMinus(const L1& _l1, const L2& _l2) : l1(_l1), l2(_l2){} template<class A1> typename RET_TYPE1<LambdaMinus,A1>::R operator()(const A1& a1)const{ return Eval(l1,a1) - Eval(l2,a1); } template<class A1, class A2> typename RET_TYPE2<LambdaMinus,A1,A2>::R operator()(const A1& a1, const A2& a2)const{ return Eval(l1,a1,a2) - Eval(l2,a1,a2); } template<class A1, class A2, class A3> typename RET_TYPE3<LambdaMinus,A1,A2,A3>::R operator()(const A1& a1, const A2& a2, const A3& a3)const{ return Eval(l1,a1,a2,a3) - Eval(l2,a1,a2,a3); } private: const L1& l1; const L2& l2; }; template<class L1, class L2> typename ENABLE_IF<HAS_OPERATOR_CALL<L1>::r or HAS_OPERATOR_CALL<L2>::r, LambdaMinus<L1,L2> >::R operator-(const L1& l1, const L2& l2){ return LambdaMinus<L1,L2>(l1,l2); } template<class L1> class LambdaUnaryMinus{ public: LambdaUnaryMinus(const L1& _l1) : l1(_l1){} template<class A1> typename RET_TYPE1<LambdaUnaryMinus,A1>::R operator()(const A1& a1)const{ return -Eval(l1,a1); } template<class A1, class A2> typename RET_TYPE2<LambdaUnaryMinus,A1,A2>::R operator()(const A1& a1, const A2& a2)const{ return -Eval(l1,a1,a2); } template<class A1, class A2, class A3> typename RET_TYPE3<LambdaUnaryMinus,A1,A2,A3>::R operator()(const A1& a1, const A2& a2, const A3& a3)const{ return -Eval(l1,a1,a2,a3); } private: const L1& l1; }; template<class L1> typename ENABLE_IF<HAS_OPERATOR_CALL<L1>::r, LambdaUnaryMinus<L1> >::R operator-(const L1& l1){ return LambdaUnaryMinus<L1>(l1); } template<class L1, class L2> class LambdaMultiplies{ public: LambdaMultiplies(const L1& _l1, const L2& _l2) : l1(_l1), l2(_l2){} template<class A1> typename RET_TYPE1<LambdaMultiplies,A1>::R operator()(const A1& a1)const{ return Eval(l1,a1) * Eval(l2,a1); } template<class A1, class A2> typename RET_TYPE2<LambdaMultiplies,A1,A2>::R operator()(const A1& a1, const A2& a2)const{ return Eval(l1,a1,a2) * Eval(l2,a1,a2); } template<class A1, class A2, class A3> typename RET_TYPE3<LambdaMultiplies,A1,A2,A3>::R operator()(const A1& a1, const A2& a2, const A3& a3)const{ return Eval(l1,a1,a2,a3) * Eval(l2,a1,a2,a3); } private: const L1& l1; const L2& l2; }; template<class L1, class L2> typename ENABLE_IF<HAS_OPERATOR_CALL<L1>::r or HAS_OPERATOR_CALL<L2>::r, LambdaMultiplies<L1,L2> >::R operator*(const L1& l1, const L2& l2){ return LambdaMultiplies<L1,L2>(l1,l2); } template<class L1, class L2> class LambdaDivides{ public: LambdaDivides(const L1& _l1, const L2& _l2) : l1(_l1), l2(_l2){} template<class A1> typename RET_TYPE1<LambdaDivides,A1>::R operator()(const A1& a1)const{ return Eval(l1,a1) / Eval(l2,a1); } template<class A1, class A2> typename RET_TYPE2<LambdaDivides,A1,A2>::R operator()(const A1& a1, const A2& a2)const{ return Eval(l1,a1,a2) / Eval(l2,a1,a2); } template<class A1, class A2, class A3> typename RET_TYPE3<LambdaDivides,A1,A2,A3>::R operator()(const A1& a1, const A2& a2, const A3& a3)const{ return Eval(l1,a1,a2,a3) / Eval(l2,a1,a2,a3); } private: const L1& l1; const L2& l2; }; template<class L1, class L2> typename ENABLE_IF<HAS_OPERATOR_CALL<L1>::r or HAS_OPERATOR_CALL<L2>::r, LambdaDivides<L1,L2> >::R operator/(const L1& l1, const L2& l2){ return LambdaDivides<L1,L2>(l1,l2); } template<class L1, class L2> class LambdaPlusAssign{ public: LambdaPlusAssign(L1& _l1, const L2& _l2) : l1(_l1), l2(_l2){} template<class A1> typename RET_TYPE1<LambdaPlusAssign,A1>::R operator()(const A1& a1)const{return Eval(l1,a1) += Eval(l2,a1);} template<class A1> typename RET_TYPE1<LambdaPlusAssign,A1>::R operator()( A1& a1)const{return Eval(l1,a1) += Eval(l2,a1);} template<class A1, class A2> typename RET_TYPE2<LambdaPlusAssign,const A1,const A2>::R operator()(const A1& a1, const A2& a2)const{return Eval(l1,a1,a2) += Eval(l2,a1,a2);} template<class A1, class A2> typename RET_TYPE2<LambdaPlusAssign,const A1, A2>::R operator()(const A1& a1, A2& a2)const{return Eval(l1,a1,a2) += Eval(l2,a1,a2);} template<class A1, class A2> typename RET_TYPE2<LambdaPlusAssign, A1,const A2>::R operator()( A1& a1, const A2& a2)const{return Eval(l1,a1,a2) += Eval(l2,a1,a2);} template<class A1, class A2> typename RET_TYPE2<LambdaPlusAssign, A1, A2>::R operator()( A1& a1, A2& a2)const{return Eval(l1,a1,a2) += Eval(l2,a1,a2);} template<class A1, class A2, class A3> typename RET_TYPE3<LambdaPlusAssign,A1,A2,A3>::R operator()(const A1& a1, const A2& a2, const A3& a3)const{return Eval(l1,a1,a2,a3) += Eval(l2,a1,a2,a3);} template<class A1, class A2, class A3> typename RET_TYPE3<LambdaPlusAssign,A1,A2,A3>::R operator()(const A1& a1, const A2& a2, A3& a3)const{return Eval(l1,a1,a2,a3) += Eval(l2,a1,a2,a3);} template<class A1, class A2, class A3> typename RET_TYPE3<LambdaPlusAssign,A1,A2,A3>::R operator()(const A1& a1, A2& a2, const A3& a3)const{return Eval(l1,a1,a2,a3) += Eval(l2,a1,a2,a3);} template<class A1, class A2, class A3> typename RET_TYPE3<LambdaPlusAssign,A1,A2,A3>::R operator()(const A1& a1, A2& a2, A3& a3)const{return Eval(l1,a1,a2,a3) += Eval(l2,a1,a2,a3);} template<class A1, class A2, class A3> typename RET_TYPE3<LambdaPlusAssign,A1,A2,A3>::R operator()( A1& a1, const A2& a2, const A3& a3)const{return Eval(l1,a1,a2,a3) += Eval(l2,a1,a2,a3);} template<class A1, class A2, class A3> typename RET_TYPE3<LambdaPlusAssign,A1,A2,A3>::R operator()( A1& a1, const A2& a2, A3& a3)const{return Eval(l1,a1,a2,a3) += Eval(l2,a1,a2,a3);} template<class A1, class A2, class A3> typename RET_TYPE3<LambdaPlusAssign,A1,A2,A3>::R operator()( A1& a1, A2& a2, const A3& a3)const{return Eval(l1,a1,a2,a3) += Eval(l2,a1,a2,a3);} template<class A1, class A2, class A3> typename RET_TYPE3<LambdaPlusAssign,A1,A2,A3>::R operator()( A1& a1, A2& a2, A3& a3)const{return Eval(l1,a1,a2,a3) += Eval(l2,a1,a2,a3);} private: L1& l1; const L2& l2; }; template<class L1, class L2> typename ENABLE_IF<IS_LAMBDA<L1>::r or IS_LAMBDA<L2>::r, LambdaPlusAssign<L1,L2> >::R operator+=(L1& l1, const L2& l2){ return LambdaPlusAssign<L1,L2>(l1,l2); } // LOGICAL template<class L1, class L2> class LambdaLess{ public: LambdaLess(const L1& _l1, const L2& _l2) : l1(_l1), l2(_l2){} template<class A1> typename RET_TYPE1<LambdaLess,A1>::R operator()(const A1& a1)const{ return Eval(l1,a1) < Eval(l2,a1); } template<class A1, class A2> typename RET_TYPE2<LambdaLess,A1,A2>::R operator()(const A1& a1, const A2& a2)const{ return Eval(l1,a1,a2) < Eval(l2,a1,a2); } template<class A1, class A2, class A3> typename RET_TYPE3<LambdaLess,A1,A2,A3>::R operator()(const A1& a1, const A2& a2, const A3& a3)const{ return Eval(l1,a1,a2,a3) < Eval(l2,a1,a2,a3); } private: const L1& l1; const L2& l2; }; template<class L1, class L2> typename ENABLE_IF<HAS_OPERATOR_CALL<L1>::r or HAS_OPERATOR_CALL<L2>::r, LambdaLess<L1,L2> >::R operator<(const L1& l1, const L2& l2){ return LambdaLess<L1,L2>(l1,l2); } template<class L1, class L2> class LambdaGreater{ public: LambdaGreater(const L1& _l1, const L2& _l2) : l1(_l1), l2(_l2){} template<class A1> typename RET_TYPE1<LambdaGreater,A1>::R operator()(const A1& a1)const{ return Eval(l1,a1) > Eval(l2,a1); } template<class A1, class A2> typename RET_TYPE2<LambdaGreater,A1,A2>::R operator()(const A1& a1, const A2& a2)const{ return Eval(l1,a1,a2) > Eval(l2,a1,a2); } template<class A1, class A2, class A3> typename RET_TYPE3<LambdaGreater,A1,A2,A3>::R operator()(const A1& a1, const A2& a2, const A3& a3)const{ return Eval(l1,a1,a2,a3) > Eval(l2,a1,a2,a3); } private: const L1& l1; const L2& l2; }; template<class L1, class L2> typename ENABLE_IF<HAS_OPERATOR_CALL<L1>::r or HAS_OPERATOR_CALL<L2>::r, LambdaGreater<L1,L2> >::R operator>(const L1& l1, const L2& l2){ return LambdaGreater<L1,L2>(l1,l2); } template<class L1, class L2> class LambdaDotStar{ public: LambdaDotStar(L1& _l1, const L2& _l2) : l1(_l1), l2(_l2) {} template<class A1> typename RET_TYPE1<LambdaDotStar,const A1>::R operator()(const A1& a1)const{return Eval(l1,a1).*Eval(l2,a1);} template<class A1> typename RET_TYPE1<LambdaDotStar, A1>::R operator()( A1& a1)const{return Eval(l1,a1).*Eval(l2,a1);} template<class A1, class A2> typename RET_TYPE2<LambdaDotStar,const A1,const A2>::R operator()(const A1& a1, const A2& a2)const{return Eval(l1,a1,a2).*Eval(l2,a1,a2);} template<class A1, class A2> typename RET_TYPE2<LambdaDotStar,const A1, A2>::R operator()(const A1& a1, A2& a2)const{return Eval(l1,a1,a2).*Eval(l2,a1,a2);} template<class A1, class A2> typename RET_TYPE2<LambdaDotStar, A1,const A2>::R operator()( A1& a1, const A2& a2)const{return Eval(l1,a1,a2).*Eval(l2,a1,a2);} template<class A1, class A2> typename RET_TYPE2<LambdaDotStar, A1, A2>::R operator()( A1& a1, A2& a2)const{return Eval(l1,a1,a2).*Eval(l2,a1,a2);} template<class A1, class A2, class A3> typename RET_TYPE3<LambdaDotStar,const A1,const A2,const A3>::R operator()(const A1& a1, const A2& a2, const A3& a3)const{return Eval(l1,a1,a2,a3).*Eval(l2,a1,a2,a3);} template<class A1, class A2, class A3> typename RET_TYPE3<LambdaDotStar,const A1,const A2, A3>::R operator()(const A1& a1, const A2& a2, A3& a3)const{return Eval(l1,a1,a2,a3).*Eval(l2,a1,a2,a3);} template<class A1, class A2, class A3> typename RET_TYPE3<LambdaDotStar,const A1, A2,const A3>::R operator()(const A1& a1, A2& a2, const A3& a3)const{return Eval(l1,a1,a2,a3).*Eval(l2,a1,a2,a3);} template<class A1, class A2, class A3> typename RET_TYPE3<LambdaDotStar,const A1, A2, A3>::R operator()(const A1& a1, A2& a2, A3& a3)const{return Eval(l1,a1,a2,a3).*Eval(l2,a1,a2,a3);} template<class A1, class A2, class A3> typename RET_TYPE3<LambdaDotStar, A1,const A2,const A3>::R operator()( A1& a1, const A2& a2, const A3& a3)const{return Eval(l1,a1,a2,a3).*Eval(l2,a1,a2,a3);} template<class A1, class A2, class A3> typename RET_TYPE3<LambdaDotStar, A1,const A2, A3>::R operator()( A1& a1, const A2& a2, A3& a3)const{return Eval(l1,a1,a2,a3).*Eval(l2,a1,a2,a3);} template<class A1, class A2, class A3> typename RET_TYPE3<LambdaDotStar, A1, A2,const A3>::R operator()( A1& a1, A2& a2, const A3& a3)const{return Eval(l1,a1,a2,a3).*Eval(l2,a1,a2,a3);} template<class A1, class A2, class A3> typename RET_TYPE3<LambdaDotStar, A1, A2, A3>::R operator()( A1& a1, A2& a2, A3& a3)const{return Eval(l1,a1,a2,a3).*Eval(l2,a1,a2,a3);} private: L1& l1; const L2& l2; }; // bv == bind var template<class PTR, class T> LambdaDotStar<const T,PTR> bv(const PTR& ptr, const T& t){ return LambdaDotStar<const T,PTR>(t,ptr); } template<class PTR, class T> LambdaDotStar<T,PTR> bv(const PTR& ptr, T& t){ return LambdaDotStar<T,PTR>(t,ptr); } // is lambda (before evaluation) template<> struct IS_LAMBDA<Lambda1>{static const bool r = true;}; template<> struct IS_LAMBDA<Lambda2>{static const bool r = true;}; template<> struct IS_LAMBDA<Lambda3>{static const bool r = true;}; template<class A,class B> struct IS_LAMBDA<LambdaPlus<A,B> >{static const bool r = true;}; template<class A,class B> struct IS_LAMBDA<LambdaMinus<A,B> >{static const bool r = true;}; template<class A,class B> struct IS_LAMBDA<LambdaMultiplies<A,B> >{static const bool r = true;}; template<class A,class B> struct IS_LAMBDA<LambdaDivides<A,B> >{static const bool r = true;}; template<class A,class B> struct IS_LAMBDA<LambdaPlusAssign<A,B> >{static const bool r = true;}; // TODO // lambda return types /* template<class A1> struct RET_TYPE1<Lambda1,A1>{typedef A1 R;}; template<class A1, class A2> struct RET_TYPE2<Lambda1,A1,A2>{typedef A1 R;}; template<class A1, class A2> struct RET_TYPE2<Lambda2,A1,A2>{typedef A2 R;}; */ template<class A1, class A2, class A3> struct RET_TYPE3<Lambda1,A1,A2,A3>{typedef A1& R;}; template<class A1, class A2, class A3> struct RET_TYPE3<Lambda2,A1,A2,A3>{typedef A2& R;}; template<class A1, class A2, class A3> struct RET_TYPE3<Lambda3,A1,A2,A3>{typedef A3& R;}; #ifdef _MSC_VER #define __typeof decltype #endif template<class L1, class L2, class A1, class A2, class A3> struct RET_TYPE3<LambdaPlus<L1,L2>,A1,A2,A3>{ typedef __typeof(TYPE(RRC,typename RET_TYPE3<L1,A1,A2,A3>::R)() + TYPE(RRC,typename RET_TYPE3<L2,A1,A2,A3>::R)()) R; }; template<class L1, class L2, class A1, class A2, class A3> struct RET_TYPE3<LambdaMinus<L1,L2>,A1,A2,A3>{ typedef __typeof(TYPE(RRC,typename RET_TYPE3<L1,A1,A2,A3>::R)() - TYPE(RRC,typename RET_TYPE3<L2,A1,A2,A3>::R)()) R; }; template<class L1, class A1, class A2, class A3> struct RET_TYPE3<LambdaUnaryMinus<L1>,A1,A2,A3>{ typedef __typeof(- TYPE(RRC,typename RET_TYPE3<L1,A1,A2,A3>::R)()) R; }; template<class L1, class L2, class A1, class A2, class A3> struct RET_TYPE3<LambdaMultiplies<L1,L2>,A1,A2,A3>{ typedef __typeof(*(typename RRC<typename RET_TYPE3<L1,A1,A2,A3>::R>::R*)nullptr * *(typename RRC<typename RET_TYPE3<L2,A1,A2,A3>::R>::R*)nullptr) R; }; template<class L1, class L2, class A1, class A2, class A3> struct RET_TYPE3<LambdaDivides<L1,L2>,A1,A2,A3>{ typedef __typeof(typename RRC<typename RET_TYPE3<L1,A1,A2,A3>::R>::R() / typename RRC<typename RET_TYPE3<L2,A1,A2,A3>::R>::R()) R; }; template<class L1, class L2, class A1, class A2, class A3> struct RET_TYPE3<LambdaPlusAssign<L1,L2>,A1,A2,A3>{ typedef __typeof(*(new typename RRC<typename RET_TYPE3<L1,A1,A2,A3>::R>::R) += *(new typename RRC<typename RET_TYPE3<L2,A1,A2,A3>::R>::R))& R; }; template<class L1, class L2, class A1, class A2, class A3> struct RET_TYPE3<LambdaLess<L1,L2>,A1,A2,A3>{ typedef __typeof(*(new typename RRC<typename RET_TYPE3<L1,A1,A2,A3>::R>::R) < *(new typename RRC<typename RET_TYPE3<L2,A1,A2,A3>::R>::R)) R; }; template<class L1, class L2, class A1, class A2, class A3> struct RET_TYPE3<LambdaGreater<L1,L2>,A1,A2,A3>{ typedef __typeof(*(new typename RRC<typename RET_TYPE3<L1,A1,A2,A3>::R>::R) > *(new typename RRC<typename RET_TYPE3<L2,A1,A2,A3>::R>::R)) R; }; template<class L1, class L2, class A1, class A2, class A3> struct RET_TYPE3<LambdaDotStar<L1,L2>,A1,A2,A3>{ typedef __typeof(*(new typename RRC<typename RET_TYPE3<L1,A1,A2,A3>::R>::R) .* *(new typename RRC<typename RET_TYPE3<L2,A1,A2,A3>::R>::R)) R; }; template<class F, class L1> class LambdaBinder1{ public: LambdaBinder1(const F& _f, const L1& _l1) : f(_f), l1(_l1) {} template<class A1> int operator()(const A1& a1){ return f(Eval(l1,a1)); } template<class A1, class A2> int operator()(const A1& a1, const A2& a2){ return f(Eval(l1,a1,a2)); } template<class A1, class A2, class A3> int operator()(const A1& a1, const A2& a2, const A3& a3){ return f(Eval(l1,a1,a2,a3)); } private: const F& f; const L1& l1; }; template<typename F, class L1> LambdaBinder1<F,L1> bf(const F& f, const L1& l1){ return LambdaBinder1<F,L1>(f,l1); } template<class F, class L1, class L2> class LambdaBinder2{ public: LambdaBinder2(const F& _f, const L1& _l1, const L2& _l2) : f(_f), l1(_l1), l2(_l2) {} template<class A1> int operator()(const A1& a1){ return f(Eval(l1,a1),Eval(l2,a1)); } template<class A1, class A2> int operator()(const A1& a1, const A2& a2){ return f(Eval(l1,a1,a2),Eval(l2,a1,a2)); } template<class A1, class A2, class A3> int operator()(const A1& a1, const A2& a2, const A3& a3){ return f(Eval(l1,a1,a2,a3),Eval(l2,a1,a2,a3)); } private: const F& f; const L1& l1; const L2& l2; }; template<typename F, class L1, class L2> LambdaBinder2<F,L1,L2> bf(const F& f, const L1& l1, const L2& l2){ return LambdaBinder2<F,L1,L2>(f,l1,l2); } template<class F, class L1, class L2, class L3> class LambdaBinder3{ public: LambdaBinder3(const F& _f, const L1& _l1, const L2& _l2, const L3& _l3) : f(_f), l1(_l1), l2(_l2), l3(_l3) {} template<class A1> int operator()(const A1& a1){ return f(Eval(l1,a1),Eval(l2,a1),Eval(l3,a1)); } template<class A1, class A2> int operator()(const A1& a1, const A2& a2){ return f(Eval(l1,a1,a2),Eval(l2,a1,a2),Eval(l3,a1,a2)); } template<class A1, class A2, class A3> int operator()(const A1& a1, const A2& a2, const A3& a3){ return f(Eval(l1,a1,a2,a3),Eval(l2,a1,a2,a3),Eval(l3,a1,a2,a3)); } private: const F& f; const L1& l1; const L2& l2; const L3& l3; }; template<typename F, class L1, class L2, class L3> LambdaBinder3<F,L1,L2,L3> bf(const F& f, const L1& l1, const L2& l2, const L3& l3){ return LambdaBinder3<F,L1,L2,L3>(f,l1,l2,l3); } } namespace ab { // type-less! #define PI 3.141592653589793238462643383279502884197169399375105820974944 template<typename T, typename IDX=int> struct binomial { binomial() : n(0), k(0), val(1) {} binomial(const IDX& nn, const IDX& kk) : n(nn), val(1){ ASS(0<=kk); if(2*kk<nn){ k = 0; while(k<kk)moveR(); } else{ k=nn; while(k>kk)moveL(); } } IDX n,k; T val; operator T(){ return val; } bool outside(){ bool ret = n<0 || k<0 || k>n; if(ret)val=0; return ret; } void moveL(){ --k; if(outside()) return; if(k==n) val=1; else { val*=(k+1); val/=(n-k); } } void moveR(){ ++k; if(outside()) return; if(k==0) val=1; else { val*=(n-k+1); val/=(k); } } void moveD(){ ++n; if(outside()) return; if(n==k) val=1; else { val*=n; val/=(n-k); } } void moveU(){ --n; if(outside()) return; val*=(n-k+1); val/=(n+1); } }; // GCD #define gcd __gcd /* template<typename T> T gcd(T a, T b) { while(b!=0) { T t = a; a = a%b; b = t; } return a; } */ // a*aa + b*bb == gcd(a,b) template<typename T> T gcdExt(T a, T b, T& aaa, T& bbb) { T a0 = a; T b0 = b; T aa = 0; T bb = 1; aaa = 1; bbb = 0; // niezmiennik: a0*aa + b0*bb == b // niezmiennik: a0*aaa + b0*bbb == a while(b != 0) { T q = a/b; a = a%b; // a -= b*(a/b) aaa -= aa*q; bbb -= bb*q; swap(a,b); swap(aa,aaa); swap(bb,bbb); } ASS(a0*aaa + b0*bbb == a); return a; } template<class T, class E, class OP> inline T fpow(T a, E exp, const OP& op, const T& neutral = 1){ T r = neutral; while(exp){ if(exp&1){ r = op(r,a); --exp; } else{ exp >>= 1; a = op(a,a); } } return r; } template<class T, class E> inline T fpow(T a, E exp){ return fpow(a, exp, _1 * _2); } template<class T> const T& max2(const T& a, const T& b){return a<b?b:a;} template<class T> const T& min2(const T& a, const T& b){return b<a?b:a;} template<class T, class F> inline T lerp(const T& a, const T& b, const F& alpha){ return (1.0f-alpha)*a + alpha*b; } } namespace ab{ template<int mod1 = (1u<<31)-1, int mod2 = (1u<<31)-19> struct H2{ H2() : h1(0), h2(0) {} H2(int x) : h1(x%mod1), h2(x%mod2) {} ui32 h1,h2; H2& operator+=(const H2& o){ h1 = (h1+o.h1)%mod1; h2 = (h2+o.h2)%mod2; return *this; } H2 operator+(const H2& o)const{ return H2(*this)+=o; } H2& operator-=(const H2& o){ h1 = (mod1+h1-o.h1)%mod1; h2 = (mod2+h2-o.h2)%mod2; return *this; } H2 operator-(const H2& o)const{ return H2(*this)-=o; } H2& operator*=(const H2& o){ h1 = (1ULL*h1*o.h1)%mod1; h2 = (1ULL*h2*o.h2)%mod2; return *this; } H2 operator*(const H2& o)const{ return H2(*this)*=o; } bool operator==(const H2& o)const{ return h1==o.h1 and h2==o.h2; } bool operator!=(const H2& o)const{ return !operator==(o); } }; template<int mod1 = (1u<<31)-1> struct H1{ H1() : h1(0) {} H1(int x) : h1(x%mod1) {} ui32 h1; H1& operator+=(const H1& o){ h1 = (h1+o.h1); if(h1>=mod1)h1-=mod1; return *this; } H1 operator+(const H1& o)const{ return H1(*this)+=o; } H1& operator-=(const H1& o){ h1 = (mod1+h1-o.h1); if(h1>=mod1)h1-=mod1; return *this; } H1 operator-(const H1& o)const{ return H1(*this)-=o; } H1& operator*=(const H1& o){ h1 = (1ULL*h1*o.h1)%mod1; return *this; } H1 operator*(const H1& o)const{ return H1(*this)*=o; } H1& operator/=(const H1& o){ //ASS(millerRabin(mod1)); return (*this)*=fpow(o, mod1-2, _1 * _2, H1(1)); } H1 operator/(const H1& o)const{ return H1(*this)/=o; } /* bool operator==(const H1& o)const{ return h1==o.h1; } bool operator!=(const H1& o)const{ return !operator==(o); }*/ operator ui32()const{ return h1; } }; // hash long long, // fpow multiplication template<ull mod = (1ULL<<63)-25> // big prime less than (2^64)/2 struct HLL{ HLL() : h(0) {} HLL(ull x) : h(x%mod) {} ull h; HLL& operator+=(const HLL& o){ h = (h+o.h); if(h>=mod)h-=mod; return *this; } HLL operator+(const HLL& o)const{ return HLL(*this)+=o; } HLL& operator-=(const HLL& o){ h = (mod+h-o.h); if(h>=mod)h-=mod; return *this; } HLL operator-(const HLL& o)const{ return HLL(*this)-=o; } // multiplication by fast exponentation HLL operator*(const HLL& o)const{ return fpow(*this, (ull)o, _1 + _2, HLL(0)); } HLL& operator*=(const HLL& o){ *this = *this * o; return *this; } HLL& operator/=(const HLL& o){ //ASS(millerRabin(mod)); return (*this)*=fpow(o, mod-2, _1 * _2, HLL(1)); } HLL operator/(const HLL& o)const{ return HLL(*this)/=o; } HLL& operator--(){ if(h==0)h=mod-1; else --h; } HLL& operator++(){ if(h==mod-1)h=0; else ++h; } /* bool operator==(const H1& o)const{ return h1==o.h1; } bool operator!=(const H1& o)const{ return !operator==(o); }*/ operator ull()const{ return h; } }; // 2 ints + 1 long long template<int mod1 = (1u<<31)-1, int mod2 = (1u<<31)-19> struct SuperHash2{ SuperHash2() : h1(0), h2(0), h(0) {} SuperHash2(int x) : h1(x%mod1), h2(x%mod2), h(x) {} ui32 h1,h2; ull h; SuperHash2& operator+=(const SuperHash2& o){ h1 += o.h1; h2 += o.h2; if(h1>=mod1) h1-=mod1; if(h2>=mod2) h2-=mod2; h += o.h; return *this; } SuperHash2 operator+(const SuperHash2& o)const{ return SuperHash2(*this)+=o; } SuperHash2& operator-=(const SuperHash2& o){ h1 += mod1-o.h1; h2 += mod2-o.h2; if(h1>=mod1) h1-=mod1; if(h2>=mod2) h2-=mod2; h -= o.h; return *this; } SuperHash2 operator-(const SuperHash2& o)const{ return SuperHash2(*this)-=o; } SuperHash2& operator*=(const SuperHash2& o){ h1 = (1ULL*h1*o.h1)%mod1; h2 = (1ULL*h2*o.h2)%mod2; h *= o.h; return *this; } SuperHash2 operator*(const SuperHash2& o)const{ return SuperHash2(*this)*=o; } bool operator==(const SuperHash2& o)const{ return h1==o.h1 and h2==o.h2 and h==o.h; } bool operator!=(const SuperHash2& o)const{ return !operator==(o); } }; // 3 ints + 1 long long template<int mod1 = (1u<<31)-1, int mod2 = (1u<<31)-19, int mod3 = (1u<<31)-69> struct SuperHash3{ SuperHash3() : h1(0), h2(0), h3(0), h(0) {} SuperHash3(int x) : h1(x%mod1), h2(x%mod2), h3(x%mod3), h(x) {} ui32 h1,h2,h3; ull h; SuperHash3& operator+=(const SuperHash3& o){ h1 += o.h1; h2 += o.h2; h3 += o.h3; if(h1>=mod1) h1-=mod1; if(h2>=mod2) h2-=mod2; if(h3>=mod3) h3-=mod3; h += o.h; return *this; } SuperHash3 operator+(const SuperHash3& o)const{ return SuperHash3(*this)+=o; } SuperHash3& operator-=(const SuperHash3& o){ h1 += mod1-o.h1; h2 += mod2-o.h2; h3 += mod3-o.h3; if(h1>=mod1) h1-=mod1; if(h2>=mod2) h2-=mod2; if(h3>=mod3) h3-=mod3; h -= o.h; return *this; } SuperHash3 operator-(const SuperHash3& o)const{ return SuperHash3(*this)-=o; } SuperHash3& operator*=(const SuperHash3& o){ h1 = (1ULL*h1*o.h1)%mod1; h2 = (1ULL*h2*o.h2)%mod2; h3 = (1ULL*h3*o.h3)%mod3; h *= o.h; return *this; } SuperHash3 operator*(const SuperHash3& o)const{ return SuperHash3(*this)*=o; } bool operator==(const SuperHash3& o)const{ return h1==o.h1 and h2==o.h2 and h3==o.h3 and h==o.h; } bool operator!=(const SuperHash3& o)const{ return !operator==(o); } }; // dynamic hash struct H1X{ H1X(int mod) : mod1(mod), h1(0) {} H1X(int mod, int x) : mod1(mod), h1(x%mod1) {} //H1X(const H1X& o) : mod1(o.mod1), h1(o.h1) {} H1X& operator=(const H1X& o){ ASS(mod1==o.mod1); //mod1 = o.mod1; h1 = o.h1; return *this; } const int mod1; ui32 h1; H1X& operator+=(const H1X& o){ ASS(mod1==o.mod1); h1 = (h1+o.h1)%mod1; return *this; } H1X operator+(const H1X& o)const{ return H1X(*this)+=o; } H1X& operator-=(const H1X& o){ ASS(mod1==o.mod1); h1 = (mod1+h1-o.h1)%mod1; return *this; } H1X operator-(const H1X& o)const{ return H1X(*this)-=o; } H1X& operator*=(const H1X& o){ ASS(mod1==o.mod1); h1 = (1ULL*h1*o.h1)%mod1; return *this; } H1X operator*(const H1X& o)const{ return H1X(*this)*=o; } H1X& operator/=(const H1X& o){ ASS(mod1==o.mod1); //ASS(millerRabin(mod1)); return (*this)*=fpow(o,mod1-2, (_1 * _2), H1X(mod1,1)); } H1X operator/(const H1X& o)const{ return H1X(*this)/=o; } /* bool operator==(const H1X& o)const{ return h1==o.h1; } bool operator!=(const H1X& o)const{ return !operator==(o); }*/ operator ui32()const{ return h1; } template<class T> explicit operator T()const{ return h1; } }; template<class INT> H1X operator/(const H1X& h, const INT& i){ ASS(h.mod1); return h / H1X(h.mod1,i); } template<class INT> H1X operator/(const INT& i, const H1X& h){ ASS(h.mod1); return H1X(h.mod1,i) / h; } template<class INT> H1X operator*(const H1X& h, const INT& i){ ASS(h.mod1); return h * H1X(h.mod1,i); } template<class INT> H1X operator*(const INT& i, const H1X& h){ ASS(h.mod1); return H1X(h.mod1,i) * h; } template<class INT> H1X operator+(const H1X& h, const INT& i){ ASS(h.mod1); return h + H1X(h.mod1,i); } template<class INT> H1X operator+(const INT& i, const H1X& h){ ASS(h.mod1); return H1X(h.mod1,i) + h; } template<class INT> H1X operator-(const H1X& h, const INT& i){ ASS(h.mod1); return h - H1X(h.mod1,i); } template<class INT> H1X operator-(const INT& i, const H1X& h){ ASS(h.mod1); return H1X(h.mod1,i) - h; } } #if __cplusplus >= 201103L or defined _MSC_VER #include <unordered_map> namespace std{ #else #include <ext/hash_map> namespace __gnu_cxx{ #endif template<int h1, int h2> struct hash<ab::H2<h1,h2> >{ size_t operator()(const ab::H2<h1,h2>& h)const{ return h.h1^h.h2; } }; template<int h1> struct hash<ab::H1<h1> >{ size_t operator()(const ab::H1<h1>& h)const{ return h.h1; } }; } //////////////////////////////////////////////////////////////////////////////// // // // ACTUAL SOLUTION IS AT THE END OF THIS FILE!!! // // // //////////////////////////////////////////////////////////////////////////////// ////////////////////////// PROTOTYPING TEMPLATE ///////////////////////////////// // // Implementation: Adam Blaszkiewicz (atablash.pl), 2012-2014 /* This program is licensed under GNU General Public License version 3. See <http://www.gnu.org/licenses/>. */ // // // enable c++ iostream; only if no fastio //#define IOSTREAM //#define NO_UNLOCKED // fread_unlocked, fwrite_unlocked // PRIi64, PRIui64 #define __STDC_FORMAT_MACROS #include<inttypes.h> //#if MIN_GCC(4,1,3) //#include<bits/stdc++.h> //#else #include<vector> #include<map> #include<queue> #include<algorithm> #include<string> #include<stdint.h> #include<cstring> #include<iostream> #include<set> #include<climits> #include<bitset> #include<limits> #include<sstream> #include<cmath> //#endif /* #define fo3(i,a,b) for(int i=(int)(a); i<(int)(b); ++i) #define of3(i,a,b) for(int i=(int)((b)-1); i>=(int)(a); --i) #define fo2(i,n) fo3(i,0,n) #define of2(i,n) of3(i,0,n) #define fo(...) VA_SEL(fo,__VA_ARGS__) #define of(...) VA_SEL(of,__VA_ARGS__) */ #define inl inline __attribute__ ((always_inline)) #define ninl __attribute__ ((noinline)) #ifdef DEBUG_BUILD #undef FASTIO #endif #ifdef _WIN32 // TODO #ifndef NO_UNLOCKED #define NO_UNLOCKED #endif //#define fread_unlocked _fread_nolock //#define fwrite_unlocked _fwrite_nolock #endif using namespace std; using namespace __gnu_cxx; namespace ab{} using namespace ab; template<class T>inline void sortu(T& t){t.resize(std::unique(ALL(t))-t.be);} inline uint _rand(){ if(RAND_MAX < (1<<15)) return (rand()<<15) | rand(); else return rand(); } #define RND _rand() #define ri _rint() #define rui _ruint() #define rll _rll() #define rull _rull() #define rchar _rchar() #define rstr _rstr() #ifndef FASTIO inline void _fin(int){} inline void _fout(int){} #ifdef IOSTREAM inline int _rint(){int r;cin>>r;return r;} inline uint _ruint(){uint r;cin>>r;return r;} inline ull _rull(){ull r;cin>>r;return r;} inline ll _rll(){ll r;cin>>r;return r;} inline char _rchar(){return cin.get();} inline string _rstr(){string s;cin>>s;return s;} template<typename T>inline void W(const T& i){cout<<i; cout.flush();} inline void W(){W('\n');} #else inline int _rint(){int r;scanf("%d",&r);return r;} inline uint _ruint(){uint r;scanf("%u",&r);return r;} inline ull _rull(){ull r;scanf("%" SCNu64,&r);return r;} inline ull _rll(){ll r;scanf("%" SCNd64,&r);return r;} inline char _rchar(){return getchar();} inline string _rstr(){ string s; char c=rchar; while(c!=-1 && !isgraph(c))c=rchar; for(;;){ if(!isgraph(c))break; s+=c; c=rchar; } return s; } inline void W(char a='\n'){printf("%c",a);} inline void W(const string&s){printf("%s",s.c_str());} inline void W(int a){printf("%d",a);} inline void W(uint a){printf("%u",a);} inline void W(const ui64& a){printf("%" PRIu64,a);} inline void W(const i64& a){printf("%" PRIi64,a);} inline void W(const char *str){fputs(str,stdout);} inline void W(const double& d){printf("%.20f",d);} inline void W(const long double& d){printf("%.50Lf",d);} #endif #else #ifndef NO_UNLOCKED #define FREAD fread_unlocked #define FWRITE fwrite_unlocked #else #define FREAD fread #define FWRITE fwrite #endif // todo: test on pointers const int _IB=32768;const int _OB=32768; char _iB[_IB];int _iP = _IB; char _oB[_OB];int _oP = 0; inline void _fin(int mc){ int r=_IB-_iP; if(r>mc)return; FOR(i,r) _iB[i]=_iB[i+_iP]; // TODO: try memcpy int rd=FREAD(_iB+r,1,_iP,stdin); if(rd!=_iP) _iB[r+rd]=0; _iP = 0; } inline void _fout(int mc){ if(_OB-_oP>mc)return; FWRITE(_oB,1,_oP,stdout); _oP = 0; } inline void _W(char c='\n'){_oB[_oP++]=c;} // unchecked! inline char _rc(){return _iB[_iP++];} inline char _rchar(){_fin(1);return _rc();} inline string _rstr(){ string s; for(;;){ char c=rchar; if(!isprint(c)||isspace(c))break; s+=c; } return s; } template<class T>inline T _rur(){ // unchecked! T r=0;char c=_rc(); do c-='0',r=r*10+c,c=_rc();while(c>='0'); return r;} template<class T>inline T _rsi(){ // unchecked! char c=_rc(); while(c<'-')c=_rc(); if(c=='-')return -_rur<T>(); else{--_iP;return _rur<T>();}} template<class T>inline T _ru(){ // unchecked! while(_rc()<'0');--_iP; return _rur<T>(); } inline int _rint(){_fin(15);return _rsi<int>();} inline uint _ruint(){_fin(15);return _ru<uint>();} inline ll _rll(){_fin(25);return _rsi<ll>();} inline ull _rull(){_fin(25);return _ru<ull>();} const int _rs = 20; char _r[_rs]; template<class T>inline void _w(T i){ // unchecked! if(numeric_limits<T>::is_signed and i<0) _W('-'), i=-i; else if(i==0){_W('0'); return;} int pos = _rs; do _r[--pos]='0'+i%10, i/=10; while(i); do _W(_r[pos++]); while(pos<_rs); } inline void W(const char c){_fout(2);_W(c);} inline void W(const int32_t& i){_fout(15);_w(i);} inline void W(const uint32_t& i){_fout(15);_w(i);} inline void W(const int64_t& i){_fout(25);_w(i);} inline void W(const uint64_t& i){_fout(25);_w(i);} inline void W(const char*str, int l){ for(int b=0; b<l; b+=_OB){ int num = min(_OB,l-b); _fout(num); memcpy(&_oB[_oP], str+b, num); _oP+=num; } } inline void W(const char*str){W(str,strlen(str));} inline void W(const string&s){W(s.c_str(),s.siz);} inline void W(double d){_fout(50);_oP+=sprintf(&_oB[_oP],"%.20f",d);} // todo #endif template<class A,class B>inline void W(const A&a,const B&b){W(a);W(b);} template<class A,class B,class C> inline void W(const A&a,const B&b,const C&c){W(a);W(b);W(c);} template<class A,class B,class C,class DD> inline void W(const A&a,const B&b,const C&c,const DD&dd){W(a);W(b);W(c);W(dd);} template<class T>inline void sortall(T&t){sort(t.be,t.en);} template<class T>inline void uniqall(T&t){t.resize(unique(t.be,t.en)-t.be);} #define rgraph _rgraph() inline char _rgraph(){char c;do c=rchar;while(!isgraph(c));return c;} #define subset(a,b) (a&b==a) #define kbegin(k) ((1<<k)-1) #define kend(k,n) ((kbegin(k)<<n-k)+1) inline uint knext(uint a){uint b=(a|(a-1))+1;return b|((a^b)>>(__builtin_ctz(a)+2));} #define fors(x,k,n) for(uint x=kbegin(k);x<kend(k,n);x=knext(x)) struct _O{template<class T>_O&operator<(const T&_t){ oc(1);W(_t);oc(0); return *this; }}O; struct _I{ _I&operator>(int32_t&_t){_t=ri;return*this;} _I&operator>(uint32_t&_t){_t=rui;return*this;} _I&operator>(int64_t&_t){_t=rll;return*this;} _I&operator>(uint64_t&_t){_t=rull;return*this;} _I&operator>(char&_t){_t=rchar;return*this;} _I&operator>(string&_t){_t=rstr;return*this;} }I; inline void tc(); #ifdef GEN int argc; char **argv; int main(int _argc, char *_argv[]){ argc = _argc; argv = _argv; if(argc<2){ O < "Usage: " < argv[0] < " NR_TESTU" < N; return 1; } srand(atoi(argv[1])); #else int main(){ #endif //Log log; //log.autoFlush = true; //Log::global() = &log; #ifdef FASTIO setvbuf(stdin,0,_IONBF,0);setvbuf(stdout,0,_IONBF,0); #elif defined IOSTREAM ios_base::sync_with_stdio(0);cout.precision(10);cout.setf(ios::fixed,ios::floatfield); #if not defined DEBUG_BUILD and not defined COLOR_CONSOLE cin.tie(0); #endif #endif #ifdef DEBUG_BUILD #ifdef _WIN32 ec(0); oc(0); #else // TODO: *nix #endif #endif #ifdef READ_NUM_TEST_CASES int nz=rui;FOR(i,nz) #endif tc(); #ifdef FASTIO FWRITE(_oB,1,_oP,stdout); #endif return 0; } // END OF TEMPLATE //////////////////////////////////////////////////////////////////////////////// // // // ACTUAL SOLUTION BEGINS HERE: // // // //////////////////////////////////////////////////////////////////////////////// #include "poszukiwania.h" #include "message.h" typedef SuperHash3<> H; //typedef SuperHash2<> H; //typedef H2<> H; // meta int numberOfNodes; int myNodeId; // input int textSize; int pattSize; // common const int base = 1000000117; int numPrzylozenia; int przylozeniaPerNode; H patternHash; inline int seqAt(int i){ return SeqAt(i+1)+1; } inline int signalAt(int i){ return SignalAt(i+1)+1; } const int maxMessageSize = 256 * 1024; const int hashSize = 3*4 + 8; // !!! inline void putHash(int target, const H& value){ PutInt(target, value.h1); PutInt(target, value.h2); PutInt(target, value.h3); PutLL(target, value.h); } inline H getHash(int from){ H h; h.h1 = GetInt(from); h.h2 = GetInt(from); h.h3 = GetInt(from); h.h = GetLL(from); return h; } // instance 0 int numTextPrefsumBlocks; int textPrefsumBlockSize; int blocksPerNode; vector<H> textPrefsumSparse; //ull textPrefsumSparse2[1010000000 / textPrefsumBlockSize]; // worker inline pii getPrefsumFragmentBounds(int iBlock){ int fr = iBlock*textPrefsumBlockSize; int to = (iBlock+1)*textPrefsumBlockSize; if(to >= textSize) to = textSize; if(fr>to) fr = to; return mp(fr,to); } void workerComputePrefsumFragment(){ textPrefsumBlockSize = sqrt(textSize); if(textPrefsumBlockSize <= 0) textPrefsumBlockSize = 1; numTextPrefsumBlocks = (textSize + textPrefsumBlockSize - 1) / textPrefsumBlockSize; if(myNodeId==0) E < "prefsum block size: " < textPrefsumBlockSize < NL; blocksPerNode = (numTextPrefsumBlocks + numberOfNodes - 1) / numberOfNodes; int messageSize = 0; FOR(i,blocksPerNode){ int iBlock = blocksPerNode*myNodeId + i; pii bounds = getPrefsumFragmentBounds(iBlock); if(bounds.fi == bounds.se) break; H cHash = 0; H basePow = fpow((H)base, bounds.fi); FO(j, bounds.fi, bounds.se){ cHash += basePow * seqAt(j); basePow *= base; } E < "computed hash " < cHash.h1 < " for block " < iBlock < NL; if(messageSize + hashSize > maxMessageSize){ Send(0); messageSize = 0; } putHash(0,cHash); messageSize += hashSize; } Send(0); } void masterSendPrefsums(){ if(myNodeId != 0) return; textPrefsumSparse.resize(numTextPrefsumBlocks+1); // receive prefsum fragments //FOR(kk,numberOfNodes){ //int fromNode = Receive(-1); FOR(fromNode,numberOfNodes){ Receive(fromNode); int messageSize = 0; FOR(i,blocksPerNode){ int iBlock = blocksPerNode*fromNode + i; pii bounds = getPrefsumFragmentBounds(iBlock); if(bounds.fi == bounds.se) break; if(messageSize + hashSize > maxMessageSize){ Receive(fromNode); messageSize = 0; } H h = getHash(fromNode); messageSize += hashSize; textPrefsumSparse[iBlock+1] = h; } } FOR(i,numTextPrefsumBlocks) textPrefsumSparse[i+1] += textPrefsumSparse[i]; // send required prefsums FOR(iNode,numberOfNodes){ int przylozenie = przylozeniaPerNode*iNode; int fullBlocks = przylozenie / textPrefsumBlockSize; int fullBlocks2 = (przylozenie + pattSize) / textPrefsumBlockSize; putHash(iNode, textPrefsumSparse[fullBlocks]); putHash(iNode, textPrefsumSparse[fullBlocks2]); Send(iNode); } } inline void tc(){ numberOfNodes = NumberOfNodes(); myNodeId = MyNodeId(); textSize = SeqLength(); pattSize = SignalLength(); numPrzylozenia = textSize-pattSize+1; przylozeniaPerNode = (numPrzylozenia+numberOfNodes-1) / numberOfNodes; // compute prefsums workerComputePrefsumFragment(); masterSendPrefsums(); if(myNodeId==0) E < "prefsums computed" < NL; // compute pattern hash patternHash = 0; OF(i,0,pattSize){ patternHash *= base; patternHash += signalAt(i); } if(myNodeId==0) E < "pattern hash computed. " < patternHash.h1 < NL; int fr = myNodeId*przylozeniaPerNode; int to = fr + przylozeniaPerNode; Receive(0); H prefsumLeft = getHash(0); H prefsumRight = getHash(0); //if(myNodeId==0) cerr << "prefsums sent" << endl; int iLeft = fr / textPrefsumBlockSize * textPrefsumBlockSize; H basePowLeft = fpow((H)base,iLeft); FO(i, iLeft, fr){ prefsumLeft += basePowLeft * seqAt(i); basePowLeft *= base; } int iRight = (fr + pattSize) / textPrefsumBlockSize * textPrefsumBlockSize; H basePowRight = fpow((H)base,iRight); FO(i, iRight, fr + pattSize){ prefsumRight += basePowRight * seqAt(i); basePowRight *= base; } patternHash *= fpow((H)base, fr); int result = 0; FO(i, fr, to){ if(i >= numPrzylozenia)break; // check przylozenie 'i' H candHash = prefsumRight - prefsumLeft; E < "position " < i < ": candHash==" < candHash.h1 < " patternHash==" < patternHash.h1 < NL; if(candHash == patternHash){ E < "match at position " < i < NL; ++result; } if(i < to-1){ prefsumLeft += basePowLeft * seqAt(i); prefsumRight += basePowRight * seqAt(i+pattSize); basePowLeft *= base; basePowRight *= base; patternHash *= base; } } // collect result PutInt(0,result); Send(0); if(myNodeId==0){ result = 0; FOR(kk,numberOfNodes){ int fromNode = Receive(-1); result += GetInt(fromNode); } O < result < NL; } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 | //#define READ_NUM_TEST_CASES //#define ASSERTS // force asserts on online judge //#define FASTIO //#define DEBUG_BUILD //#include<atablash/splay/common.hpp> //#include<atablash/splay/node.hpp> //#include<atablash/interval-tree.hpp> #include<stdint.h> #include<utility> #include<vector> #include<memory> // abbrevs typedef unsigned int uint; typedef int8_t i8; typedef uint8_t ui8; typedef int16_t i16; typedef uint16_t ui16; typedef int32_t i32; typedef uint32_t ui32; typedef int64_t i64; typedef uint64_t ui64; // old: typedef i64 ll; typedef ui64 ull; typedef std::pair<i32,i32> pii; typedef std::pair<double,double> pdd; typedef std::vector<i32> vi; #define VE std::vector #define umap std::unordered_map #define umultimap std::unordered_multimap #define uset std::unordered_set #define umultiset std::unordered_multiset typedef unsigned short ushort; typedef unsigned char uchar; template<class T> using sptr = std::shared_ptr<T>; template<class T> using uptr = std::unique_ptr<T>; template<class A, class B> using p = std::pair<A, B>; namespace ab{ struct Void {}; // convert to float with enough precision template<typename I>struct FloatFrom{typedef Void R;}; template<>struct FloatFrom<i32>{typedef double R;}; template<>struct FloatFrom<ui32>{typedef double R;}; template<>struct FloatFrom<i64>{typedef long double R;}; template<>struct FloatFrom<ui64>{typedef long double R;}; template<>struct FloatFrom<i16>{typedef float R;}; template<>struct FloatFrom<ui16>{typedef float R;}; template<>struct FloatFrom<i8>{typedef float R;}; template<>struct FloatFrom<ui8>{typedef float R;}; template<>struct FloatFrom<float>{typedef float R;}; template<>struct FloatFrom<double>{typedef double R;}; template<>struct FloatFrom<long double>{typedef long double R;}; // get type capable of storing multiplication result template<typename I>struct Double{typedef Void R;}; template<>struct Double<i8>{typedef i16 R;}; template<>struct Double<ui8>{typedef ui16 R;}; template<>struct Double<i16>{typedef i32 R;}; template<>struct Double<ui16>{typedef ui32 R;}; template<>struct Double<i32>{typedef i64 R;}; template<>struct Double<ui32>{typedef ui64 R;}; template<>struct Double<float>{typedef float R;}; template<>struct Double<double>{typedef double R;}; template<>struct Double<long double>{typedef long double R;}; #if defined __GNUC__ && ( defined __x86_64__ || defined __ppc64__ || defined __MINGW64__ ) typedef __int128_t i128; typedef __uint128_t ui128; template<>struct Double<i64>{typedef i128 R;}; template<>struct Double<ui64>{typedef ui128 R;}; #endif } // enable debug if any debug macro is defined #if (defined _DEBUG || defined DEBUG) && !defined DEBUG_BUILD #define DEBUG_BUILD #endif // DEBUG_BUILD (set or not set) overrides NDEBUG #ifdef DEBUG_BUILD #undef NDEBUG //#pragma message "Compiling in DEBUG mode." #else #ifndef NDEBUG #define NDEBUG #endif #endif //#include <cstdio> //#include <cfloat> //#include<type_traits> #define _CAT(a,b) a##b #define CAT(a,b) _CAT(a,b) #define _10TH(a,b,c,d,e,f,g,h,i,X,...) X #define VA_CNT(...) _10TH(__VA_ARGS__,9,8,7,6,5,4,3,2,1,0) #define VA_SEL(a,...) CAT(a,VA_CNT(__VA_ARGS__))(__VA_ARGS__) #define _VA_GTONE(...) _10TH(__VA_ARGS__,1,1,1,1,1,1,1,1,0,0) #define VA_GTONE(a,...) CAT(a,_VA_GTONE(__VA_ARGS__))(__VA_ARGS__) #define MIN_GCC(a,b,c) (__GNUC__ > (a) || \ (__GNUC__ == (a) && (__GNUC_MINOR__ > (b) || \ (__GNUC_MINOR__ == (b) && \ __GNUC_PATCHLEVEL__ >= (c))))) // TODO: po co to bylo?? /* #include<functional> #if __cplusplus >= 201103L namespace std { template<class T> struct hash { typedef T argument_type; typedef typename std::underlying_type< argument_type >::type underlying_type; typedef typename std::hash< underlying_type >::result_type result_type; result_type operator()( const argument_type& arg ) const { std::hash< underlying_type > hasher; return hasher( static_cast< underlying_type >( arg ) ); } }; } #endif */ #define bac back() #define fro front() #define emp empty() #define fi first #define se second #define siz size() #define ins insert #define era erase #define mp make_pair #define be begin() #define en end() #define rbe rbegin() #define ren rend() #define pub push_back #define puf push_front #define pob pop_back() #define pof pop_front() #define bs binary_search #define lb lower_bound #define ub upper_bound #define pq priority_queue #define si (int) #undef FO #undef OF #define FOR(i,n) for(int i=0; i<(int)(n); ++i) #define FO(i,a,b) for(int i=(a); i<(int)(b); ++i) #define OF(i,a,b) for(int i=(int)(b)-1;i>=(int)(a);--i) #define FE(it,c) for(__typeof((c).begin()) it=(c).begin(); it!=(c).end(); ++it) #define FER(it,c) for(__typeof((c).begin()) it=(c).begin(); it!=(c).end(); ) #define ALL(c) (c).begin(),(c).end() #define ZERO(c) memset(c,0,sizeof(c)) #define AU(v,val) __typeof(val) v(val) const char NL='\n'; const char SP=' '; // Visual C++ intristics #ifdef _MSC_VER #include <intrin.h> #define __builtin_popcount __popcnt #ifdef _WIN64 #define __builtin_popcountll __popcnt64 #else inline int __builtin_popcountll(__int64 a){ return __builtin_popcount((ui32)a) + __builtin_popcount(a >> 32); } #endif #endif // Visual C++ 'and', 'or', 'not' #ifdef _MSC_VER #define and && #define or || #define not ! #endif namespace ab{ template<class T>inline T cbitl(T x,int i){return (x<<i) ^ (x>>(sizeof(T)*8-i));} // cycle bits left inline int popcount(uint i){return __builtin_popcount(i);} inline int popcount(ull i){return __builtin_popcountll(i);} #ifdef __GNUC__ inline int parity(uint i){return __builtin_parity(i);} inline int parity(ull i){return __builtin_parityll(i);} inline int ilog2(uint i){return 31-__builtin_clz(i);} // int log; floor(log2(i)) inline int ilog2(ull i){return 63-__builtin_clzll(i);} #endif template<class T>inline bool isPowerOf2(const T&x){return !(x&(x-1));} struct Static{ Static() = default; Static(const Static&) = delete; Static(Static&&) = delete; Static(const Static&&) = delete; Static& operator=(const Static&) = delete; Static& operator=(Static&&) = delete; Static& operator=(const Static&&) = delete; }; } #include<vector> #include<unordered_map> // ? #include<iostream> #include<fstream> #include<string> #include<algorithm> #include<mutex> // to_string fix for MinGW #include<map> #include<iostream> #if __cplusplus >= 201103L or defined _MSC_VER #include <unordered_map> #include <unordered_set> namespace std{ #else #include <ext/hash_map> #include <ext/hash_set> namespace __gnu_cxx{ #define unordered_map hash_map #define unordered_multimap hash_multimap #define unordered_set hash_set #define unordered_multiset hash_multiset /* template<class K, class V> class unordered_map : public hash_map<K,V>{ public: typename hash_map<K,V>::iterator erase(const typename hash_map<K,V>::iterator& it){ typename hash_map<K,V>::iterator r = it; ++r; if(r!=hash_map<K,V>::end()){ K key = r->first; hash_map<K,V>::erase(it); return hash_map<K,V>::find(key); } else{ hash_map<K,V>::erase(it); return r; } } typename hash_map<K,V>::size_type erase(const K& key){ return hash_map<K,V>::erase(key); } }; */ #endif // 1,2,last (!!!!!) /*template<>*/ template<class A,class B>struct hash<std::map<A,B> >{ size_t operator()(const std::map<A,B>& m) const{ size_t r = m.size(); if(!r)return 0; __typeof(m.begin()) i=m.be; r^=hash<__typeof(i)>()(i); ++i; r^=cbitl(hash<__typeof(i)>()(*i),sizeof(r)*8/4); i=m.end(); --i; r^=cbitl(hash<__typeof(i)>()(*i),sizeof(r)*8/2); return r; } }; // hash pairs: /*template<>*/ template<class A, class B>struct hash<pair<A,B> >{ size_t operator()(const pair<A,B>& p) const{ return hash<A>()(p.first) ^ ab::cbitl(hash<B>()(p.second),sizeof(size_t)*8/2); } }; // hash enums: // // TODO: fix for msvc #if defined __GNUC__ and MIN_GCC(4,8,4) template<class E> struct hash { using sfinae = typename std::enable_if<std::is_enum<E>::value, E>::type; public: size_t operator()(const E&e) const { return std::hash<typename std::underlying_type<E>::type>()((typename std::underlying_type<E>::type)e); } }; #endif } // namespace std / __gnu_cxx // component-wise pair operators template<class A, class B, class C, class D> inline std::pair<A,B>& operator+=(std::pair<A,B>& p, const std::pair<C,D>& o){ p.first+=o.first; p.second+=o.second; return p; } template<class A, class B, class C, class D> inline std::pair<A,B>& operator-=(std::pair<A,B>& p, const std::pair<C,D>& o){ p.first-=o.first; p.second-=o.second; return p; } template<class A, class B, class C, class D> inline std::pair<A,B>& operator*=(std::pair<A,B>& p, const std::pair<C,D>& o){ p.first*=o.first; p.second*=o.second; return p; } template<class A, class B, class C, class D> inline std::pair<A,B>& operator/=(std::pair<A,B>& p, const std::pair<C,D>& o){ p.first/=o.first; p.second/=o.second; return p; } template<class Afi, class Ase, class Bfi, class Bse> inline std::pair<decltype(Afi() + Bfi()), decltype(Ase() + Bse())>& operator+(const std::pair<Afi,Ase>& a, const std::pair<Bfi,Bse>& b){ return std::make_pair(a.fi+b.fi, a.se+b.se); } template<class Afi, class Ase, class Bfi, class Bse> inline std::pair<decltype(Afi() - Bfi()), decltype(Ase() - Bse())>& operator-(const std::pair<Afi,Ase>& a, const std::pair<Bfi,Bse>& b){ return std::make_pair(a.fi-b.fi, a.se-b.se); } template<class Afi, class Ase, class Bfi, class Bse> inline std::pair<decltype(Afi()*Bfi()), decltype(Ase()*Bse())>& operator*(const std::pair<Afi,Ase>& a, const std::pair<Bfi,Bse>& b){ return std::make_pair(a.fi*b.fi, a.se*b.se); } template<class Afi, class Ase, class Bfi, class Bse> inline std::pair<decltype(Afi() / Bfi()), decltype(Ase() / Bse())>& operator/(const std::pair<Afi,Ase>& a, const std::pair<Bfi,Bse>& b){ return std::make_pair(a.fi/b.fi, a.se/b.se); } // vector operators template<class T, class U> inline std::vector<T>& operator+=(std::vector<T>& v, const std::vector<U>& o){ ASS(v.siz == o.siz); FOR(i,v.siz) v[i] += o[i]; return v; } template<class T> inline std::vector<T>& operator*=(std::vector<T>& v, const T& a){ FOR(i,v.siz) v[i] *= a; return v; } // serialization template<class A,class B>inline std::ostream&operator<<(std::ostream&o,const std::pair<A,B>&p){o<<'<'<<p.first<<", "<<p.second<<'>';return o;} template<class A,class B>inline std::ostream&operator<<(std::ostream&o,const std::map<A,B>&m){o<<'\n';FE(k,m)o<<"map["<<k->first<<"] -> "<<k->se<<'\n';return o;} template<class A,class B>inline std::ostream&operator<<(std::ostream&o,const umap<A,B>&m){o<<'\n';FE(k,m)o<<"umap["<<k->first<<"] -> "<<k->se<<'\n';return o;} template<class A,class B>inline std::ostream&operator<<(std::ostream&o,const uset<A,B>&_m){o<<"uset{\n";FE(k,_m)o<<*k<<'\n';o<<'\n';return o;} template<class A>inline std::ostream&operator<<(std::ostream&o,const std::vector<A>&_v){o<<'\n';FOR(i,_v.siz)o<<"vector["<<i<<"] = "<<_v[i]<<"}\n";return o;} // fix for MinGW: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=52015 #ifdef _GLIBCXX_HAVE_BROKEN_VSWPRINTF namespace std{ template<class T> inline string to_string(const T& t){ stringstream ss; ss << t; return ss.str(); } } #endif //#define LOG0(str) fprintf(stderr,"%s:%d - " str "\n",__FILE__,__LINE__) //#define LOG1(str,...) fprintf(stderr,"%s:%d - " str "\n",__FILE__,__LINE__,__VA_ARGS__) //#define LOG(...) VA_GTONE(LOG,__VA_ARGS__) #define LOGD(str) if(ab::Log::global()) ab::Log::global()->debug(str,__FILE__,__LINE__) #define LOGI(str) if(ab::Log::global()) ab::Log::global()->info(str,__FILE__,__LINE__) #define LOGW(str) if(ab::Log::global()) ab::Log::global()->warning(str,__FILE__,__LINE__) #define LOGE(str) if(ab::Log::global()) ab::Log::global()->error(str,__FILE__,__LINE__) #define LOGF(str) if(ab::Log::global()) ab::Log::global()->fail(str,__FILE__,__LINE__) namespace ab{ // TODO: czas logowania // TODO: interfejs do dodawania nazw message levelom, ustawiania nazwy pliku z logiem, czy printowac na stderr, ... // // level >= DEBUG powoduje ignorowanie logowania w release class Log{ public: enum{ DEBUG = 5000, INFO = 4000, // default? WARNING = 3000, ERROR = 2000, FAIL = 1000}; public: void debug(const std::string& _message, const std::string& file="", int line=0){ message(DEBUG, _message, file, line); } void info(const std::string& _message, const std::string& file = "", int line = 0){ message(INFO, _message, file, line); } void warning(const std::string& _message, const std::string& file = "", int line = 0){ message(WARNING, _message, file, line); } void error(const std::string& _message, const std::string& file = "", int line = 0){ message(ERROR, _message, file, line); } void fail(const std::string& _message, const std::string& file = "", int line = 0){ message(FAIL, _message, file, line); } // slow void flushConsole(){ // avoid slow cout during lock // copy messages to print static const uint max_messages_flush = 128; std::vector<Message> messages_copy; { std::lock_guard<std::mutex> lock(mut); messages_copy.assign(messages.begin() + numPrinted, messages.begin() + numPrinted + std::min(max_messages_flush, (uint)messages.size() - numPrinted)); } for (uint i = 0; i < messages_copy.size(); ++i){ std::cout << messageToString(messages_copy[i]) << '\n'; } std::cout.flush(); numPrinted += messages_copy.size(); } void message(int level, const std::string& messageText, const std::string& file = "", int line = 0){ #if !defined DEBUG_BUILD if (level >= DEBUG) return; #endif std::lock_guard<std::mutex> lock(mut); messages.push_back(Message{ level, messageText, file, line }); //if(autoFlush) flushConsole(); #ifdef DEBUG_BUILD if (level <= ERROR){ #ifdef _MSC_VER __debugbreak(); #endif #ifdef __GNUC__ __builtin_trap(); #endif } #endif } // reference to pointer. client should create the log himself static Log*& global(){ static Log* log = nullptr; return log; } public: Log(){ levelNames[DEBUG] = "DEBUG"; levelNames[INFO] = "INFO"; levelNames[WARNING] = "WARNING"; levelNames[ERROR] = "ERROR"; levelNames[FAIL] = "FAIL"; } ~Log(){ if (filename != ""){ std::ofstream os(filename); for (auto&m : messages){ os << messageToString(m) << '\n'; } } } //bool autoFlush = false; private: struct Message{ int level; std::string text; std::string file; int line; }; static std::string shortenPath(const std::string& s){ std::string result = s; std::string ss[] = {"include/", "include\\", "src/", "src\\"}; for(uint i=0; i<sizeof(ss)/sizeof(std::string); ++i){ auto pos = result.rfind(ss[i]); if(pos == std::string::npos) continue; result = result.substr(pos + ss[i].size(), std::string::npos); } return result; } std::string messageToString(const Message& m) const { std::string levelName; auto r = levelNames.find(m.level); if (r == levelNames.end()) levelName = std::to_string(m.level); else levelName = r->second; std::string result = levelName + ": " + m.text; if (m.file != "") result += " at " + shortenPath(m.file) + ":" + std::to_string(m.line); return result; } uint numPrinted = 0; std::vector<Message> messages; std::mutex mut; // TODO: spinlock std::unordered_map<int, std::string> levelNames; std::string filename = "atablash.log"; private: Log(const Log&) = delete; Log& operator=(const Log&) = delete; }; } inline void breakpoint(){} #ifdef NDEBUG #define DE if(0) #else // DEBUG #define DE #endif #if defined NDEBUG && not defined ASSERTS #define ASS(x) #define FLOAT_OK(x) #define ASSERT_FLOAT(x) #else #include<cassert> #define ASS(x) assert(x) #define FLOAT_OK(x) ((x) <= LDBL_MAX && (x) >= -LDBL_MAX) #define ASSERT_FLOAT(f) ASS(FLOAT_OK(f)) #endif #define DV(x) (E<#x<": "<(x)<N) #define XX E<"XX at line "<__LINE__<NL; #if defined DEBUG_BUILD or defined COLOR_CONSOLE #ifdef _WIN32 #ifndef NOMINMAX #define NOMINMAX #endif #ifndef WIN32_LEAN_AND_MEAN #define WIN32_LEAN_AND_MEAN #endif #include<windows.h> inline void ec(int a){static HANDLE h=GetStdHandle(-12);SetConsoleTextAttribute(h,a?12:0xf);} // color 12 inline void oc(int a){static HANDLE h=GetStdHandle(-11);SetConsoleTextAttribute(h,a?10:0xf);} // color 10 #else // *nix includes for debug #endif #else #define ec(x) #define oc(x) #endif #include <iostream> struct _E{template<class T>_E&operator<(const T&_t){DE{ec(1);std::cerr<<_t;ec(0);}return*this;}}; #ifdef __GNUC__ #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wunused-variable" // not working #endif static _E E; #ifdef __GNUC__ #pragma GCC diagnostic pop #endif // E unused #ifdef __GNUC__ template<class T>void ___fhsdhsdhrtsh(){(void)E;} #endif #include<cstdlib> namespace ab { /* template<typename Ret, typename T> class Binder { public: Binder(Ret T::*_var) : var(_var) {} Ret& operator()(T& obj) const { return obj.*var; } const Ret& operator()(const T& obj) const { return obj.*var; } private: Ret T::*var; }; template<typename Ret, typename T> Binder<Ret,T> bind(Ret T::*var) { return Binder<Ret,T>(var); }*/ /* template<class A, class B> __typeof(A()+B()) plus(const A& a, const B& b){return a+b;} template<class A, class B> __typeof(A()-B()) minus(const A& a, const B& b){return a-b;} template<class A, class B> __typeof(A()*B()) multiplies(const A& a, const B& b){return a*b;} template<class A, class B> __typeof(A()/B()) divides(const A& a, const B& b){return a/b;} template<class A, class B> A& plus_assign(A& a,const B& b){return a+=b;} template<class A, class B> A& minus_assign(A& a,const B& b){return a-=b;} template<class A, class B> A& multiplies_assign(A& a,const B& b){return a*=b;} template<class A, class B> A& divides_assign(A& a,const B& b){return a/=b;} */ } //#include<type_traits> namespace ab { template<bool cond, typename T, typename F> struct IF {typedef T R;}; template<typename T, typename F> struct IF<false,T,F> {typedef F R;}; template <bool B, class T = void> struct ENABLE_IF {typedef T R;}; template <class T> struct ENABLE_IF<false, T> {}; template <bool B, class T = void> struct DISABLE_IF {typedef T R;}; template <class T> struct DISABLE_IF<true, T> {}; template<class A, class B> struct SAME{ static const bool r = false;}; template<typename T> struct SAME<T,T>{ static const bool r = true;}; template<typename T> struct IS_CLASS{ template<typename C> static char (&f(int C::*))[1]; template<typename C> static char (&f(...))[2]; static bool const r = sizeof(f<T>(0))==sizeof(char); }; // remove reference template<class T> struct RR{typedef T R;}; template<class T> struct RR<T&>{typedef T R;}; //remove const template<class T> struct RC{typedef T R;}; template<class T> struct RC<const T>{typedef T R;}; // remove const and reference template<class T> struct RRC{typedef typename RC<typename RR<T>::R>::R R;}; #define TYPE(T,...) typename T<__VA_ARGS__>::R struct TRU{static const bool r = true; typedef TRU R;}; struct FAL{static const bool r = false; typedef FAL R;}; // SFINAE test #define HAS_MEMBER(member_alias, member) \ template<typename __T> struct HAS_CLASS_##member_alias { \ struct Fallback { void member(); }; \ struct Derived : __T, Fallback { }; \ \ template<typename C, C> struct ChT; \ \ template<typename C> static char (&f(ChT<void (Fallback::*)(), &C::member>*))[1]; \ template<typename C> static char (&f(...))[2]; \ \ static const bool r = sizeof(f<Derived>(0)) == 2; \ }; \ template<typename __T> \ struct HAS_##member_alias \ { \ typedef typename IF<IS_CLASS<__T>::r, HAS_CLASS_##member_alias<__T>, FAL>::R R; \ static const bool r = R::r; \ }; // Dzia│a dok│adnie, uwzglŕdnia typ (const, referencje) wyniku i argumentˇw, uwzglŕdnia const membera // Przyk│ad dla non-static klasy A: // typedef int (A::*sign1)(unsigned short&); // Przyk│ad dla static dowolnej klasy: // typedef int (*sign1)(unsigned short&); #define HAS_MEMBER_SIG(member_name, member_signature) \ template<typename CLS> \ struct HAS_SIG_##member_name \ { \ template<typename C, C> struct ChT; \ template<typename C> static char (&f(ChT<member_signature, &C::member_name>*))[1]; \ template<typename C> static char (&f(...))[2]; \ static const bool r = sizeof(f<CLS>(0)) == sizeof(char); \ }; HAS_MEMBER(OPERATOR_CALL, operator()) /* // get &T::x or &T::first template<class T,class=void>struct GET_X{}; template<class T>struct GET_X<T,typename std::enable_if<std::is_object<decltype(T::x)>::value>::type>{typedef decltype(&T::x) R;}; template<class T>struct GET_X<T,typename std::enable_if<std::is_object<decltype(T::first)>::value>::type>{typedef decltype(&T::first) R;}; // get &T::y or &T::second template<class T,class=void>struct GET_Y{}; template<class T>struct GET_Y<T,typename std::enable_if<std::is_object<decltype(T::y)>::value>::type>{typedef decltype(&T::y) R;}; template<class T>struct GET_Y<T,typename std::enable_if<std::is_object<decltype(T::second)>::value>::type>{typedef decltype(&T::second) R;}; */ } #include<utility> namespace ab{ struct Identity{ template<class TT> TT operator()(TT&& x)const { return std::forward<TT>(x); } template<class TT> TT operator[](TT&& x) const { return std::forward<TT>(x); } }; } namespace ab{ struct Lambda1{ template<class A1> const A1& operator()(const A1& a1)const{return a1;} template<class A1> A1& operator()( A1& a1)const{return a1;} template<class A1,class A2> const A1& operator()(const A1& a1, const A2&)const{return a1;} template<class A1,class A2> A1& operator()( A1& a1, const A2&)const{return a1;} template<class A1,class A2,class A3> const A1& operator()(const A1& a1, const A2&, const A3&)const{return a1;} template<class A1,class A2,class A3> A1& operator()( A1& a1, const A2&, const A3&)const{return a1;} }; struct Lambda2{ template<class A1,class A2> const A2& operator()(const A1& a1, const A2& a2)const{return a2;} template<class A1,class A2> A2& operator()(const A1& a1, A2& a2)const{return a2;} template<class A1,class A2,class A3> const A2& operator()(const A1& a1, const A2& a2, const A3& a3)const{return a2;} template<class A1,class A2,class A3> A2& operator()(const A1& a1, A2& a2, const A3& a3)const{return a2;} }; struct Lambda3{ template<class A1,class A2,class A3> const A3& operator()(const A1& a1, const A2& a2, const A3& a3)const{return a3;} template<class A1,class A2,class A3> A3& operator()(const A1& a1, const A2& a2, A3& a3)const{return a3;} }; #ifdef __GNUC__ #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wunused-variable" // not working #endif static Lambda1 _1; static Lambda2 _2; static Lambda3 _3; //#define _1 Lambda1() //#define _2 Lambda2() //#define _3 Lambda3() #ifdef __GNUC__ #pragma GCC diagnostic pop #endif #ifdef __GNUC__ // _1 _2 _3 unused // pragma diagnostic ignored not working? template<class T>void ___fhsdhsdhrtshx(){(void)_1;(void)_2;(void)_3;} #endif template<class L, class A1, class A2, class A3> struct RET_TYPE3{typedef L& R;}; template<class L, class A1> struct RET_TYPE1{typedef typename RET_TYPE3<L,A1,A1,A1>::R R;}; template<class L, class A1, class A2> struct RET_TYPE2{typedef typename RET_TYPE3<L,A1,A2,A2>::R R;}; // is lambda before evaluation template<class L> struct IS_LAMBDA{static const bool r = false;}; // is lambda after evaluation template<class L, class A1, class A2, class A3> struct IS_LAMBDA3{ static const bool r = !SAME<L&, typename RET_TYPE3<L,A1,A2,A3>::R&>::r;}; template<class L, class A1, class A2> struct IS_LAMBDA2{ static const bool r = !SAME<typename RR<L>::R, typename RR<typename RET_TYPE2<L,A1,A2>::R>::R>::r;}; template<class L, class A1> struct IS_LAMBDA1{ static const bool r = !SAME<typename RR<L>::R, typename RR<typename RET_TYPE1<L,A1>::R>::R>::r;}; template<class T, class A1> typename ENABLE_IF < IS_LAMBDA1<T,A1>::r, typename RET_TYPE1<T,const A1>::R>::R Eval(const T& t, const A1& a1){return t(a1);} template<class T, class A1> typename ENABLE_IF < IS_LAMBDA1<T,A1>::r, typename RET_TYPE1<T, A1>::R>::R Eval(const T& t, A1& a1){return t(a1);} template<class T, class A1> typename DISABLE_IF < IS_LAMBDA1<T,A1>::r, typename RET_TYPE1<T,A1>::R&>::R Eval( T& t, const A1& a1){return t;} template<class T, class A1, class A2> typename ENABLE_IF < IS_LAMBDA2<T,A1,A2>::r, typename RET_TYPE2<T,const A1,const A2>::R>::R Eval(const T& t, const A1& a1, const A2& a2){return t(a1,a2);} template<class T, class A1, class A2> typename ENABLE_IF < IS_LAMBDA2<T,A1,A2>::r, typename RET_TYPE2<T,const A1, A2>::R>::R Eval(const T& t, const A1& a1, A2& a2){return t(a1,a2);} template<class T, class A1, class A2> typename ENABLE_IF < IS_LAMBDA2<T,A1,A2>::r, typename RET_TYPE2<T, A1,const A2>::R>::R Eval(const T& t, A1& a1, const A2& a2){return t(a1,a2);} template<class T, class A1, class A2> typename ENABLE_IF < IS_LAMBDA2<T,A1,A2>::r, typename RET_TYPE2<T, A1, A2>::R>::R Eval(const T& t, A1& a1, A2& a2){return t(a1,a2);} template<class T, class A1, class A2> typename DISABLE_IF< IS_LAMBDA2<T,A1,A2>::r, typename RET_TYPE2<T,A1,A2>::R&>::R Eval( T& t, const A1& a1, const A2& a2){return t;} template<class T, class A1, class A2, class A3> typename ENABLE_IF < IS_LAMBDA3<T,A1,A2,A3>::r, typename RET_TYPE3<T,const A1,const A2,const A3>::R>::R Eval(const T& t, const A1& a1, const A2& a2, const A3& a3){return t(a1,a2,a3);} template<class T, class A1, class A2, class A3> typename ENABLE_IF < IS_LAMBDA3<T,A1,A2,A3>::r, typename RET_TYPE3<T,const A1,const A2, A3>::R>::R Eval(const T& t, const A1& a1, const A2& a2, A3& a3){return t(a1,a2,a3);} template<class T, class A1, class A2, class A3> typename ENABLE_IF < IS_LAMBDA3<T,A1,A2,A3>::r, typename RET_TYPE3<T,const A1, A2,const A3>::R>::R Eval(const T& t, const A1& a1, A2& a2, const A3& a3){return t(a1,a2,a3);} template<class T, class A1, class A2, class A3> typename ENABLE_IF < IS_LAMBDA3<T,A1,A2,A3>::r, typename RET_TYPE3<T,const A1, A2, A3>::R>::R Eval(const T& t, const A1& a1, A2& a2, A3& a3){return t(a1,a2,a3);} template<class T, class A1, class A2, class A3> typename ENABLE_IF < IS_LAMBDA3<T,A1,A2,A3>::r, typename RET_TYPE3<T, A1,const A2,const A3>::R>::R Eval(const T& t, A1& a1, const A2& a2, const A3& a3){return t(a1,a2,a3);} template<class T, class A1, class A2, class A3> typename ENABLE_IF < IS_LAMBDA3<T,A1,A2,A3>::r, typename RET_TYPE3<T, A1,const A2, A3>::R>::R Eval(const T& t, A1& a1, const A2& a2, A3& a3){return t(a1,a2,a3);} template<class T, class A1, class A2, class A3> typename ENABLE_IF < IS_LAMBDA3<T,A1,A2,A3>::r, typename RET_TYPE3<T, A1, A2,const A3>::R>::R Eval(const T& t, A1& a1, A2& a2, const A3& a3){return t(a1,a2,a3);} template<class T, class A1, class A2, class A3> typename ENABLE_IF < IS_LAMBDA3<T,A1,A2,A3>::r, typename RET_TYPE3<T, A1, A2, A3>::R>::R Eval(const T& t, A1& a1, A2& a2, A3& a3){return t(a1,a2,a3);} template<class T, class A1, class A2, class A3> typename DISABLE_IF < IS_LAMBDA3<T,A1,A2,A3>::r, typename RET_TYPE3<T,A1,A2,A3>::R&>::R Eval( T& t, const A1& a1, const A2& a2, const A3& a3){return t;} template<class L1, class L2> class LambdaPlus{ public: LambdaPlus(const L1& _l1, const L2& _l2) : l1(_l1), l2(_l2){} template<class A1> typename RET_TYPE1<LambdaPlus,A1>::R operator()(const A1& a1)const{ return Eval(l1,a1) + Eval(l2,a1); } template<class A1, class A2> typename RET_TYPE2<LambdaPlus,A1,A2>::R operator()(const A1& a1, const A2& a2)const{ return Eval(l1,a1,a2) + Eval(l2,a1,a2); } template<class A1, class A2, class A3> typename RET_TYPE3<LambdaPlus,A1,A2,A3>::R operator()(const A1& a1, const A2& a2, const A3& a3)const{ return Eval(l1,a1,a2,a3) + Eval(l2,a1,a2,a3); } private: const L1& l1; const L2& l2; }; template<class L1, class L2> typename ENABLE_IF<HAS_OPERATOR_CALL<L1>::r or HAS_OPERATOR_CALL<L2>::r, LambdaPlus<L1,L2> >::R operator+(const L1& l1, const L2& l2){ return LambdaPlus<L1,L2>(l1,l2); } template<class L1, class L2> class LambdaMinus{ public: LambdaMinus(const L1& _l1, const L2& _l2) : l1(_l1), l2(_l2){} template<class A1> typename RET_TYPE1<LambdaMinus,A1>::R operator()(const A1& a1)const{ return Eval(l1,a1) - Eval(l2,a1); } template<class A1, class A2> typename RET_TYPE2<LambdaMinus,A1,A2>::R operator()(const A1& a1, const A2& a2)const{ return Eval(l1,a1,a2) - Eval(l2,a1,a2); } template<class A1, class A2, class A3> typename RET_TYPE3<LambdaMinus,A1,A2,A3>::R operator()(const A1& a1, const A2& a2, const A3& a3)const{ return Eval(l1,a1,a2,a3) - Eval(l2,a1,a2,a3); } private: const L1& l1; const L2& l2; }; template<class L1, class L2> typename ENABLE_IF<HAS_OPERATOR_CALL<L1>::r or HAS_OPERATOR_CALL<L2>::r, LambdaMinus<L1,L2> >::R operator-(const L1& l1, const L2& l2){ return LambdaMinus<L1,L2>(l1,l2); } template<class L1> class LambdaUnaryMinus{ public: LambdaUnaryMinus(const L1& _l1) : l1(_l1){} template<class A1> typename RET_TYPE1<LambdaUnaryMinus,A1>::R operator()(const A1& a1)const{ return -Eval(l1,a1); } template<class A1, class A2> typename RET_TYPE2<LambdaUnaryMinus,A1,A2>::R operator()(const A1& a1, const A2& a2)const{ return -Eval(l1,a1,a2); } template<class A1, class A2, class A3> typename RET_TYPE3<LambdaUnaryMinus,A1,A2,A3>::R operator()(const A1& a1, const A2& a2, const A3& a3)const{ return -Eval(l1,a1,a2,a3); } private: const L1& l1; }; template<class L1> typename ENABLE_IF<HAS_OPERATOR_CALL<L1>::r, LambdaUnaryMinus<L1> >::R operator-(const L1& l1){ return LambdaUnaryMinus<L1>(l1); } template<class L1, class L2> class LambdaMultiplies{ public: LambdaMultiplies(const L1& _l1, const L2& _l2) : l1(_l1), l2(_l2){} template<class A1> typename RET_TYPE1<LambdaMultiplies,A1>::R operator()(const A1& a1)const{ return Eval(l1,a1) * Eval(l2,a1); } template<class A1, class A2> typename RET_TYPE2<LambdaMultiplies,A1,A2>::R operator()(const A1& a1, const A2& a2)const{ return Eval(l1,a1,a2) * Eval(l2,a1,a2); } template<class A1, class A2, class A3> typename RET_TYPE3<LambdaMultiplies,A1,A2,A3>::R operator()(const A1& a1, const A2& a2, const A3& a3)const{ return Eval(l1,a1,a2,a3) * Eval(l2,a1,a2,a3); } private: const L1& l1; const L2& l2; }; template<class L1, class L2> typename ENABLE_IF<HAS_OPERATOR_CALL<L1>::r or HAS_OPERATOR_CALL<L2>::r, LambdaMultiplies<L1,L2> >::R operator*(const L1& l1, const L2& l2){ return LambdaMultiplies<L1,L2>(l1,l2); } template<class L1, class L2> class LambdaDivides{ public: LambdaDivides(const L1& _l1, const L2& _l2) : l1(_l1), l2(_l2){} template<class A1> typename RET_TYPE1<LambdaDivides,A1>::R operator()(const A1& a1)const{ return Eval(l1,a1) / Eval(l2,a1); } template<class A1, class A2> typename RET_TYPE2<LambdaDivides,A1,A2>::R operator()(const A1& a1, const A2& a2)const{ return Eval(l1,a1,a2) / Eval(l2,a1,a2); } template<class A1, class A2, class A3> typename RET_TYPE3<LambdaDivides,A1,A2,A3>::R operator()(const A1& a1, const A2& a2, const A3& a3)const{ return Eval(l1,a1,a2,a3) / Eval(l2,a1,a2,a3); } private: const L1& l1; const L2& l2; }; template<class L1, class L2> typename ENABLE_IF<HAS_OPERATOR_CALL<L1>::r or HAS_OPERATOR_CALL<L2>::r, LambdaDivides<L1,L2> >::R operator/(const L1& l1, const L2& l2){ return LambdaDivides<L1,L2>(l1,l2); } template<class L1, class L2> class LambdaPlusAssign{ public: LambdaPlusAssign(L1& _l1, const L2& _l2) : l1(_l1), l2(_l2){} template<class A1> typename RET_TYPE1<LambdaPlusAssign,A1>::R operator()(const A1& a1)const{return Eval(l1,a1) += Eval(l2,a1);} template<class A1> typename RET_TYPE1<LambdaPlusAssign,A1>::R operator()( A1& a1)const{return Eval(l1,a1) += Eval(l2,a1);} template<class A1, class A2> typename RET_TYPE2<LambdaPlusAssign,const A1,const A2>::R operator()(const A1& a1, const A2& a2)const{return Eval(l1,a1,a2) += Eval(l2,a1,a2);} template<class A1, class A2> typename RET_TYPE2<LambdaPlusAssign,const A1, A2>::R operator()(const A1& a1, A2& a2)const{return Eval(l1,a1,a2) += Eval(l2,a1,a2);} template<class A1, class A2> typename RET_TYPE2<LambdaPlusAssign, A1,const A2>::R operator()( A1& a1, const A2& a2)const{return Eval(l1,a1,a2) += Eval(l2,a1,a2);} template<class A1, class A2> typename RET_TYPE2<LambdaPlusAssign, A1, A2>::R operator()( A1& a1, A2& a2)const{return Eval(l1,a1,a2) += Eval(l2,a1,a2);} template<class A1, class A2, class A3> typename RET_TYPE3<LambdaPlusAssign,A1,A2,A3>::R operator()(const A1& a1, const A2& a2, const A3& a3)const{return Eval(l1,a1,a2,a3) += Eval(l2,a1,a2,a3);} template<class A1, class A2, class A3> typename RET_TYPE3<LambdaPlusAssign,A1,A2,A3>::R operator()(const A1& a1, const A2& a2, A3& a3)const{return Eval(l1,a1,a2,a3) += Eval(l2,a1,a2,a3);} template<class A1, class A2, class A3> typename RET_TYPE3<LambdaPlusAssign,A1,A2,A3>::R operator()(const A1& a1, A2& a2, const A3& a3)const{return Eval(l1,a1,a2,a3) += Eval(l2,a1,a2,a3);} template<class A1, class A2, class A3> typename RET_TYPE3<LambdaPlusAssign,A1,A2,A3>::R operator()(const A1& a1, A2& a2, A3& a3)const{return Eval(l1,a1,a2,a3) += Eval(l2,a1,a2,a3);} template<class A1, class A2, class A3> typename RET_TYPE3<LambdaPlusAssign,A1,A2,A3>::R operator()( A1& a1, const A2& a2, const A3& a3)const{return Eval(l1,a1,a2,a3) += Eval(l2,a1,a2,a3);} template<class A1, class A2, class A3> typename RET_TYPE3<LambdaPlusAssign,A1,A2,A3>::R operator()( A1& a1, const A2& a2, A3& a3)const{return Eval(l1,a1,a2,a3) += Eval(l2,a1,a2,a3);} template<class A1, class A2, class A3> typename RET_TYPE3<LambdaPlusAssign,A1,A2,A3>::R operator()( A1& a1, A2& a2, const A3& a3)const{return Eval(l1,a1,a2,a3) += Eval(l2,a1,a2,a3);} template<class A1, class A2, class A3> typename RET_TYPE3<LambdaPlusAssign,A1,A2,A3>::R operator()( A1& a1, A2& a2, A3& a3)const{return Eval(l1,a1,a2,a3) += Eval(l2,a1,a2,a3);} private: L1& l1; const L2& l2; }; template<class L1, class L2> typename ENABLE_IF<IS_LAMBDA<L1>::r or IS_LAMBDA<L2>::r, LambdaPlusAssign<L1,L2> >::R operator+=(L1& l1, const L2& l2){ return LambdaPlusAssign<L1,L2>(l1,l2); } // LOGICAL template<class L1, class L2> class LambdaLess{ public: LambdaLess(const L1& _l1, const L2& _l2) : l1(_l1), l2(_l2){} template<class A1> typename RET_TYPE1<LambdaLess,A1>::R operator()(const A1& a1)const{ return Eval(l1,a1) < Eval(l2,a1); } template<class A1, class A2> typename RET_TYPE2<LambdaLess,A1,A2>::R operator()(const A1& a1, const A2& a2)const{ return Eval(l1,a1,a2) < Eval(l2,a1,a2); } template<class A1, class A2, class A3> typename RET_TYPE3<LambdaLess,A1,A2,A3>::R operator()(const A1& a1, const A2& a2, const A3& a3)const{ return Eval(l1,a1,a2,a3) < Eval(l2,a1,a2,a3); } private: const L1& l1; const L2& l2; }; template<class L1, class L2> typename ENABLE_IF<HAS_OPERATOR_CALL<L1>::r or HAS_OPERATOR_CALL<L2>::r, LambdaLess<L1,L2> >::R operator<(const L1& l1, const L2& l2){ return LambdaLess<L1,L2>(l1,l2); } template<class L1, class L2> class LambdaGreater{ public: LambdaGreater(const L1& _l1, const L2& _l2) : l1(_l1), l2(_l2){} template<class A1> typename RET_TYPE1<LambdaGreater,A1>::R operator()(const A1& a1)const{ return Eval(l1,a1) > Eval(l2,a1); } template<class A1, class A2> typename RET_TYPE2<LambdaGreater,A1,A2>::R operator()(const A1& a1, const A2& a2)const{ return Eval(l1,a1,a2) > Eval(l2,a1,a2); } template<class A1, class A2, class A3> typename RET_TYPE3<LambdaGreater,A1,A2,A3>::R operator()(const A1& a1, const A2& a2, const A3& a3)const{ return Eval(l1,a1,a2,a3) > Eval(l2,a1,a2,a3); } private: const L1& l1; const L2& l2; }; template<class L1, class L2> typename ENABLE_IF<HAS_OPERATOR_CALL<L1>::r or HAS_OPERATOR_CALL<L2>::r, LambdaGreater<L1,L2> >::R operator>(const L1& l1, const L2& l2){ return LambdaGreater<L1,L2>(l1,l2); } template<class L1, class L2> class LambdaDotStar{ public: LambdaDotStar(L1& _l1, const L2& _l2) : l1(_l1), l2(_l2) {} template<class A1> typename RET_TYPE1<LambdaDotStar,const A1>::R operator()(const A1& a1)const{return Eval(l1,a1).*Eval(l2,a1);} template<class A1> typename RET_TYPE1<LambdaDotStar, A1>::R operator()( A1& a1)const{return Eval(l1,a1).*Eval(l2,a1);} template<class A1, class A2> typename RET_TYPE2<LambdaDotStar,const A1,const A2>::R operator()(const A1& a1, const A2& a2)const{return Eval(l1,a1,a2).*Eval(l2,a1,a2);} template<class A1, class A2> typename RET_TYPE2<LambdaDotStar,const A1, A2>::R operator()(const A1& a1, A2& a2)const{return Eval(l1,a1,a2).*Eval(l2,a1,a2);} template<class A1, class A2> typename RET_TYPE2<LambdaDotStar, A1,const A2>::R operator()( A1& a1, const A2& a2)const{return Eval(l1,a1,a2).*Eval(l2,a1,a2);} template<class A1, class A2> typename RET_TYPE2<LambdaDotStar, A1, A2>::R operator()( A1& a1, A2& a2)const{return Eval(l1,a1,a2).*Eval(l2,a1,a2);} template<class A1, class A2, class A3> typename RET_TYPE3<LambdaDotStar,const A1,const A2,const A3>::R operator()(const A1& a1, const A2& a2, const A3& a3)const{return Eval(l1,a1,a2,a3).*Eval(l2,a1,a2,a3);} template<class A1, class A2, class A3> typename RET_TYPE3<LambdaDotStar,const A1,const A2, A3>::R operator()(const A1& a1, const A2& a2, A3& a3)const{return Eval(l1,a1,a2,a3).*Eval(l2,a1,a2,a3);} template<class A1, class A2, class A3> typename RET_TYPE3<LambdaDotStar,const A1, A2,const A3>::R operator()(const A1& a1, A2& a2, const A3& a3)const{return Eval(l1,a1,a2,a3).*Eval(l2,a1,a2,a3);} template<class A1, class A2, class A3> typename RET_TYPE3<LambdaDotStar,const A1, A2, A3>::R operator()(const A1& a1, A2& a2, A3& a3)const{return Eval(l1,a1,a2,a3).*Eval(l2,a1,a2,a3);} template<class A1, class A2, class A3> typename RET_TYPE3<LambdaDotStar, A1,const A2,const A3>::R operator()( A1& a1, const A2& a2, const A3& a3)const{return Eval(l1,a1,a2,a3).*Eval(l2,a1,a2,a3);} template<class A1, class A2, class A3> typename RET_TYPE3<LambdaDotStar, A1,const A2, A3>::R operator()( A1& a1, const A2& a2, A3& a3)const{return Eval(l1,a1,a2,a3).*Eval(l2,a1,a2,a3);} template<class A1, class A2, class A3> typename RET_TYPE3<LambdaDotStar, A1, A2,const A3>::R operator()( A1& a1, A2& a2, const A3& a3)const{return Eval(l1,a1,a2,a3).*Eval(l2,a1,a2,a3);} template<class A1, class A2, class A3> typename RET_TYPE3<LambdaDotStar, A1, A2, A3>::R operator()( A1& a1, A2& a2, A3& a3)const{return Eval(l1,a1,a2,a3).*Eval(l2,a1,a2,a3);} private: L1& l1; const L2& l2; }; // bv == bind var template<class PTR, class T> LambdaDotStar<const T,PTR> bv(const PTR& ptr, const T& t){ return LambdaDotStar<const T,PTR>(t,ptr); } template<class PTR, class T> LambdaDotStar<T,PTR> bv(const PTR& ptr, T& t){ return LambdaDotStar<T,PTR>(t,ptr); } // is lambda (before evaluation) template<> struct IS_LAMBDA<Lambda1>{static const bool r = true;}; template<> struct IS_LAMBDA<Lambda2>{static const bool r = true;}; template<> struct IS_LAMBDA<Lambda3>{static const bool r = true;}; template<class A,class B> struct IS_LAMBDA<LambdaPlus<A,B> >{static const bool r = true;}; template<class A,class B> struct IS_LAMBDA<LambdaMinus<A,B> >{static const bool r = true;}; template<class A,class B> struct IS_LAMBDA<LambdaMultiplies<A,B> >{static const bool r = true;}; template<class A,class B> struct IS_LAMBDA<LambdaDivides<A,B> >{static const bool r = true;}; template<class A,class B> struct IS_LAMBDA<LambdaPlusAssign<A,B> >{static const bool r = true;}; // TODO // lambda return types /* template<class A1> struct RET_TYPE1<Lambda1,A1>{typedef A1 R;}; template<class A1, class A2> struct RET_TYPE2<Lambda1,A1,A2>{typedef A1 R;}; template<class A1, class A2> struct RET_TYPE2<Lambda2,A1,A2>{typedef A2 R;}; */ template<class A1, class A2, class A3> struct RET_TYPE3<Lambda1,A1,A2,A3>{typedef A1& R;}; template<class A1, class A2, class A3> struct RET_TYPE3<Lambda2,A1,A2,A3>{typedef A2& R;}; template<class A1, class A2, class A3> struct RET_TYPE3<Lambda3,A1,A2,A3>{typedef A3& R;}; #ifdef _MSC_VER #define __typeof decltype #endif template<class L1, class L2, class A1, class A2, class A3> struct RET_TYPE3<LambdaPlus<L1,L2>,A1,A2,A3>{ typedef __typeof(TYPE(RRC,typename RET_TYPE3<L1,A1,A2,A3>::R)() + TYPE(RRC,typename RET_TYPE3<L2,A1,A2,A3>::R)()) R; }; template<class L1, class L2, class A1, class A2, class A3> struct RET_TYPE3<LambdaMinus<L1,L2>,A1,A2,A3>{ typedef __typeof(TYPE(RRC,typename RET_TYPE3<L1,A1,A2,A3>::R)() - TYPE(RRC,typename RET_TYPE3<L2,A1,A2,A3>::R)()) R; }; template<class L1, class A1, class A2, class A3> struct RET_TYPE3<LambdaUnaryMinus<L1>,A1,A2,A3>{ typedef __typeof(- TYPE(RRC,typename RET_TYPE3<L1,A1,A2,A3>::R)()) R; }; template<class L1, class L2, class A1, class A2, class A3> struct RET_TYPE3<LambdaMultiplies<L1,L2>,A1,A2,A3>{ typedef __typeof(*(typename RRC<typename RET_TYPE3<L1,A1,A2,A3>::R>::R*)nullptr * *(typename RRC<typename RET_TYPE3<L2,A1,A2,A3>::R>::R*)nullptr) R; }; template<class L1, class L2, class A1, class A2, class A3> struct RET_TYPE3<LambdaDivides<L1,L2>,A1,A2,A3>{ typedef __typeof(typename RRC<typename RET_TYPE3<L1,A1,A2,A3>::R>::R() / typename RRC<typename RET_TYPE3<L2,A1,A2,A3>::R>::R()) R; }; template<class L1, class L2, class A1, class A2, class A3> struct RET_TYPE3<LambdaPlusAssign<L1,L2>,A1,A2,A3>{ typedef __typeof(*(new typename RRC<typename RET_TYPE3<L1,A1,A2,A3>::R>::R) += *(new typename RRC<typename RET_TYPE3<L2,A1,A2,A3>::R>::R))& R; }; template<class L1, class L2, class A1, class A2, class A3> struct RET_TYPE3<LambdaLess<L1,L2>,A1,A2,A3>{ typedef __typeof(*(new typename RRC<typename RET_TYPE3<L1,A1,A2,A3>::R>::R) < *(new typename RRC<typename RET_TYPE3<L2,A1,A2,A3>::R>::R)) R; }; template<class L1, class L2, class A1, class A2, class A3> struct RET_TYPE3<LambdaGreater<L1,L2>,A1,A2,A3>{ typedef __typeof(*(new typename RRC<typename RET_TYPE3<L1,A1,A2,A3>::R>::R) > *(new typename RRC<typename RET_TYPE3<L2,A1,A2,A3>::R>::R)) R; }; template<class L1, class L2, class A1, class A2, class A3> struct RET_TYPE3<LambdaDotStar<L1,L2>,A1,A2,A3>{ typedef __typeof(*(new typename RRC<typename RET_TYPE3<L1,A1,A2,A3>::R>::R) .* *(new typename RRC<typename RET_TYPE3<L2,A1,A2,A3>::R>::R)) R; }; template<class F, class L1> class LambdaBinder1{ public: LambdaBinder1(const F& _f, const L1& _l1) : f(_f), l1(_l1) {} template<class A1> int operator()(const A1& a1){ return f(Eval(l1,a1)); } template<class A1, class A2> int operator()(const A1& a1, const A2& a2){ return f(Eval(l1,a1,a2)); } template<class A1, class A2, class A3> int operator()(const A1& a1, const A2& a2, const A3& a3){ return f(Eval(l1,a1,a2,a3)); } private: const F& f; const L1& l1; }; template<typename F, class L1> LambdaBinder1<F,L1> bf(const F& f, const L1& l1){ return LambdaBinder1<F,L1>(f,l1); } template<class F, class L1, class L2> class LambdaBinder2{ public: LambdaBinder2(const F& _f, const L1& _l1, const L2& _l2) : f(_f), l1(_l1), l2(_l2) {} template<class A1> int operator()(const A1& a1){ return f(Eval(l1,a1),Eval(l2,a1)); } template<class A1, class A2> int operator()(const A1& a1, const A2& a2){ return f(Eval(l1,a1,a2),Eval(l2,a1,a2)); } template<class A1, class A2, class A3> int operator()(const A1& a1, const A2& a2, const A3& a3){ return f(Eval(l1,a1,a2,a3),Eval(l2,a1,a2,a3)); } private: const F& f; const L1& l1; const L2& l2; }; template<typename F, class L1, class L2> LambdaBinder2<F,L1,L2> bf(const F& f, const L1& l1, const L2& l2){ return LambdaBinder2<F,L1,L2>(f,l1,l2); } template<class F, class L1, class L2, class L3> class LambdaBinder3{ public: LambdaBinder3(const F& _f, const L1& _l1, const L2& _l2, const L3& _l3) : f(_f), l1(_l1), l2(_l2), l3(_l3) {} template<class A1> int operator()(const A1& a1){ return f(Eval(l1,a1),Eval(l2,a1),Eval(l3,a1)); } template<class A1, class A2> int operator()(const A1& a1, const A2& a2){ return f(Eval(l1,a1,a2),Eval(l2,a1,a2),Eval(l3,a1,a2)); } template<class A1, class A2, class A3> int operator()(const A1& a1, const A2& a2, const A3& a3){ return f(Eval(l1,a1,a2,a3),Eval(l2,a1,a2,a3),Eval(l3,a1,a2,a3)); } private: const F& f; const L1& l1; const L2& l2; const L3& l3; }; template<typename F, class L1, class L2, class L3> LambdaBinder3<F,L1,L2,L3> bf(const F& f, const L1& l1, const L2& l2, const L3& l3){ return LambdaBinder3<F,L1,L2,L3>(f,l1,l2,l3); } } namespace ab { // type-less! #define PI 3.141592653589793238462643383279502884197169399375105820974944 template<typename T, typename IDX=int> struct binomial { binomial() : n(0), k(0), val(1) {} binomial(const IDX& nn, const IDX& kk) : n(nn), val(1){ ASS(0<=kk); if(2*kk<nn){ k = 0; while(k<kk)moveR(); } else{ k=nn; while(k>kk)moveL(); } } IDX n,k; T val; operator T(){ return val; } bool outside(){ bool ret = n<0 || k<0 || k>n; if(ret)val=0; return ret; } void moveL(){ --k; if(outside()) return; if(k==n) val=1; else { val*=(k+1); val/=(n-k); } } void moveR(){ ++k; if(outside()) return; if(k==0) val=1; else { val*=(n-k+1); val/=(k); } } void moveD(){ ++n; if(outside()) return; if(n==k) val=1; else { val*=n; val/=(n-k); } } void moveU(){ --n; if(outside()) return; val*=(n-k+1); val/=(n+1); } }; // GCD #define gcd __gcd /* template<typename T> T gcd(T a, T b) { while(b!=0) { T t = a; a = a%b; b = t; } return a; } */ // a*aa + b*bb == gcd(a,b) template<typename T> T gcdExt(T a, T b, T& aaa, T& bbb) { T a0 = a; T b0 = b; T aa = 0; T bb = 1; aaa = 1; bbb = 0; // niezmiennik: a0*aa + b0*bb == b // niezmiennik: a0*aaa + b0*bbb == a while(b != 0) { T q = a/b; a = a%b; // a -= b*(a/b) aaa -= aa*q; bbb -= bb*q; swap(a,b); swap(aa,aaa); swap(bb,bbb); } ASS(a0*aaa + b0*bbb == a); return a; } template<class T, class E, class OP> inline T fpow(T a, E exp, const OP& op, const T& neutral = 1){ T r = neutral; while(exp){ if(exp&1){ r = op(r,a); --exp; } else{ exp >>= 1; a = op(a,a); } } return r; } template<class T, class E> inline T fpow(T a, E exp){ return fpow(a, exp, _1 * _2); } template<class T> const T& max2(const T& a, const T& b){return a<b?b:a;} template<class T> const T& min2(const T& a, const T& b){return b<a?b:a;} template<class T, class F> inline T lerp(const T& a, const T& b, const F& alpha){ return (1.0f-alpha)*a + alpha*b; } } namespace ab{ template<int mod1 = (1u<<31)-1, int mod2 = (1u<<31)-19> struct H2{ H2() : h1(0), h2(0) {} H2(int x) : h1(x%mod1), h2(x%mod2) {} ui32 h1,h2; H2& operator+=(const H2& o){ h1 = (h1+o.h1)%mod1; h2 = (h2+o.h2)%mod2; return *this; } H2 operator+(const H2& o)const{ return H2(*this)+=o; } H2& operator-=(const H2& o){ h1 = (mod1+h1-o.h1)%mod1; h2 = (mod2+h2-o.h2)%mod2; return *this; } H2 operator-(const H2& o)const{ return H2(*this)-=o; } H2& operator*=(const H2& o){ h1 = (1ULL*h1*o.h1)%mod1; h2 = (1ULL*h2*o.h2)%mod2; return *this; } H2 operator*(const H2& o)const{ return H2(*this)*=o; } bool operator==(const H2& o)const{ return h1==o.h1 and h2==o.h2; } bool operator!=(const H2& o)const{ return !operator==(o); } }; template<int mod1 = (1u<<31)-1> struct H1{ H1() : h1(0) {} H1(int x) : h1(x%mod1) {} ui32 h1; H1& operator+=(const H1& o){ h1 = (h1+o.h1); if(h1>=mod1)h1-=mod1; return *this; } H1 operator+(const H1& o)const{ return H1(*this)+=o; } H1& operator-=(const H1& o){ h1 = (mod1+h1-o.h1); if(h1>=mod1)h1-=mod1; return *this; } H1 operator-(const H1& o)const{ return H1(*this)-=o; } H1& operator*=(const H1& o){ h1 = (1ULL*h1*o.h1)%mod1; return *this; } H1 operator*(const H1& o)const{ return H1(*this)*=o; } H1& operator/=(const H1& o){ //ASS(millerRabin(mod1)); return (*this)*=fpow(o, mod1-2, _1 * _2, H1(1)); } H1 operator/(const H1& o)const{ return H1(*this)/=o; } /* bool operator==(const H1& o)const{ return h1==o.h1; } bool operator!=(const H1& o)const{ return !operator==(o); }*/ operator ui32()const{ return h1; } }; // hash long long, // fpow multiplication template<ull mod = (1ULL<<63)-25> // big prime less than (2^64)/2 struct HLL{ HLL() : h(0) {} HLL(ull x) : h(x%mod) {} ull h; HLL& operator+=(const HLL& o){ h = (h+o.h); if(h>=mod)h-=mod; return *this; } HLL operator+(const HLL& o)const{ return HLL(*this)+=o; } HLL& operator-=(const HLL& o){ h = (mod+h-o.h); if(h>=mod)h-=mod; return *this; } HLL operator-(const HLL& o)const{ return HLL(*this)-=o; } // multiplication by fast exponentation HLL operator*(const HLL& o)const{ return fpow(*this, (ull)o, _1 + _2, HLL(0)); } HLL& operator*=(const HLL& o){ *this = *this * o; return *this; } HLL& operator/=(const HLL& o){ //ASS(millerRabin(mod)); return (*this)*=fpow(o, mod-2, _1 * _2, HLL(1)); } HLL operator/(const HLL& o)const{ return HLL(*this)/=o; } HLL& operator--(){ if(h==0)h=mod-1; else --h; } HLL& operator++(){ if(h==mod-1)h=0; else ++h; } /* bool operator==(const H1& o)const{ return h1==o.h1; } bool operator!=(const H1& o)const{ return !operator==(o); }*/ operator ull()const{ return h; } }; // 2 ints + 1 long long template<int mod1 = (1u<<31)-1, int mod2 = (1u<<31)-19> struct SuperHash2{ SuperHash2() : h1(0), h2(0), h(0) {} SuperHash2(int x) : h1(x%mod1), h2(x%mod2), h(x) {} ui32 h1,h2; ull h; SuperHash2& operator+=(const SuperHash2& o){ h1 += o.h1; h2 += o.h2; if(h1>=mod1) h1-=mod1; if(h2>=mod2) h2-=mod2; h += o.h; return *this; } SuperHash2 operator+(const SuperHash2& o)const{ return SuperHash2(*this)+=o; } SuperHash2& operator-=(const SuperHash2& o){ h1 += mod1-o.h1; h2 += mod2-o.h2; if(h1>=mod1) h1-=mod1; if(h2>=mod2) h2-=mod2; h -= o.h; return *this; } SuperHash2 operator-(const SuperHash2& o)const{ return SuperHash2(*this)-=o; } SuperHash2& operator*=(const SuperHash2& o){ h1 = (1ULL*h1*o.h1)%mod1; h2 = (1ULL*h2*o.h2)%mod2; h *= o.h; return *this; } SuperHash2 operator*(const SuperHash2& o)const{ return SuperHash2(*this)*=o; } bool operator==(const SuperHash2& o)const{ return h1==o.h1 and h2==o.h2 and h==o.h; } bool operator!=(const SuperHash2& o)const{ return !operator==(o); } }; // 3 ints + 1 long long template<int mod1 = (1u<<31)-1, int mod2 = (1u<<31)-19, int mod3 = (1u<<31)-69> struct SuperHash3{ SuperHash3() : h1(0), h2(0), h3(0), h(0) {} SuperHash3(int x) : h1(x%mod1), h2(x%mod2), h3(x%mod3), h(x) {} ui32 h1,h2,h3; ull h; SuperHash3& operator+=(const SuperHash3& o){ h1 += o.h1; h2 += o.h2; h3 += o.h3; if(h1>=mod1) h1-=mod1; if(h2>=mod2) h2-=mod2; if(h3>=mod3) h3-=mod3; h += o.h; return *this; } SuperHash3 operator+(const SuperHash3& o)const{ return SuperHash3(*this)+=o; } SuperHash3& operator-=(const SuperHash3& o){ h1 += mod1-o.h1; h2 += mod2-o.h2; h3 += mod3-o.h3; if(h1>=mod1) h1-=mod1; if(h2>=mod2) h2-=mod2; if(h3>=mod3) h3-=mod3; h -= o.h; return *this; } SuperHash3 operator-(const SuperHash3& o)const{ return SuperHash3(*this)-=o; } SuperHash3& operator*=(const SuperHash3& o){ h1 = (1ULL*h1*o.h1)%mod1; h2 = (1ULL*h2*o.h2)%mod2; h3 = (1ULL*h3*o.h3)%mod3; h *= o.h; return *this; } SuperHash3 operator*(const SuperHash3& o)const{ return SuperHash3(*this)*=o; } bool operator==(const SuperHash3& o)const{ return h1==o.h1 and h2==o.h2 and h3==o.h3 and h==o.h; } bool operator!=(const SuperHash3& o)const{ return !operator==(o); } }; // dynamic hash struct H1X{ H1X(int mod) : mod1(mod), h1(0) {} H1X(int mod, int x) : mod1(mod), h1(x%mod1) {} //H1X(const H1X& o) : mod1(o.mod1), h1(o.h1) {} H1X& operator=(const H1X& o){ ASS(mod1==o.mod1); //mod1 = o.mod1; h1 = o.h1; return *this; } const int mod1; ui32 h1; H1X& operator+=(const H1X& o){ ASS(mod1==o.mod1); h1 = (h1+o.h1)%mod1; return *this; } H1X operator+(const H1X& o)const{ return H1X(*this)+=o; } H1X& operator-=(const H1X& o){ ASS(mod1==o.mod1); h1 = (mod1+h1-o.h1)%mod1; return *this; } H1X operator-(const H1X& o)const{ return H1X(*this)-=o; } H1X& operator*=(const H1X& o){ ASS(mod1==o.mod1); h1 = (1ULL*h1*o.h1)%mod1; return *this; } H1X operator*(const H1X& o)const{ return H1X(*this)*=o; } H1X& operator/=(const H1X& o){ ASS(mod1==o.mod1); //ASS(millerRabin(mod1)); return (*this)*=fpow(o,mod1-2, (_1 * _2), H1X(mod1,1)); } H1X operator/(const H1X& o)const{ return H1X(*this)/=o; } /* bool operator==(const H1X& o)const{ return h1==o.h1; } bool operator!=(const H1X& o)const{ return !operator==(o); }*/ operator ui32()const{ return h1; } template<class T> explicit operator T()const{ return h1; } }; template<class INT> H1X operator/(const H1X& h, const INT& i){ ASS(h.mod1); return h / H1X(h.mod1,i); } template<class INT> H1X operator/(const INT& i, const H1X& h){ ASS(h.mod1); return H1X(h.mod1,i) / h; } template<class INT> H1X operator*(const H1X& h, const INT& i){ ASS(h.mod1); return h * H1X(h.mod1,i); } template<class INT> H1X operator*(const INT& i, const H1X& h){ ASS(h.mod1); return H1X(h.mod1,i) * h; } template<class INT> H1X operator+(const H1X& h, const INT& i){ ASS(h.mod1); return h + H1X(h.mod1,i); } template<class INT> H1X operator+(const INT& i, const H1X& h){ ASS(h.mod1); return H1X(h.mod1,i) + h; } template<class INT> H1X operator-(const H1X& h, const INT& i){ ASS(h.mod1); return h - H1X(h.mod1,i); } template<class INT> H1X operator-(const INT& i, const H1X& h){ ASS(h.mod1); return H1X(h.mod1,i) - h; } } #if __cplusplus >= 201103L or defined _MSC_VER #include <unordered_map> namespace std{ #else #include <ext/hash_map> namespace __gnu_cxx{ #endif template<int h1, int h2> struct hash<ab::H2<h1,h2> >{ size_t operator()(const ab::H2<h1,h2>& h)const{ return h.h1^h.h2; } }; template<int h1> struct hash<ab::H1<h1> >{ size_t operator()(const ab::H1<h1>& h)const{ return h.h1; } }; } //////////////////////////////////////////////////////////////////////////////// // // // ACTUAL SOLUTION IS AT THE END OF THIS FILE!!! // // // //////////////////////////////////////////////////////////////////////////////// ////////////////////////// PROTOTYPING TEMPLATE ///////////////////////////////// // // Implementation: Adam Blaszkiewicz (atablash.pl), 2012-2014 /* This program is licensed under GNU General Public License version 3. See <http://www.gnu.org/licenses/>. */ // // // enable c++ iostream; only if no fastio //#define IOSTREAM //#define NO_UNLOCKED // fread_unlocked, fwrite_unlocked // PRIi64, PRIui64 #define __STDC_FORMAT_MACROS #include<inttypes.h> //#if MIN_GCC(4,1,3) //#include<bits/stdc++.h> //#else #include<vector> #include<map> #include<queue> #include<algorithm> #include<string> #include<stdint.h> #include<cstring> #include<iostream> #include<set> #include<climits> #include<bitset> #include<limits> #include<sstream> #include<cmath> //#endif /* #define fo3(i,a,b) for(int i=(int)(a); i<(int)(b); ++i) #define of3(i,a,b) for(int i=(int)((b)-1); i>=(int)(a); --i) #define fo2(i,n) fo3(i,0,n) #define of2(i,n) of3(i,0,n) #define fo(...) VA_SEL(fo,__VA_ARGS__) #define of(...) VA_SEL(of,__VA_ARGS__) */ #define inl inline __attribute__ ((always_inline)) #define ninl __attribute__ ((noinline)) #ifdef DEBUG_BUILD #undef FASTIO #endif #ifdef _WIN32 // TODO #ifndef NO_UNLOCKED #define NO_UNLOCKED #endif //#define fread_unlocked _fread_nolock //#define fwrite_unlocked _fwrite_nolock #endif using namespace std; using namespace __gnu_cxx; namespace ab{} using namespace ab; template<class T>inline void sortu(T& t){t.resize(std::unique(ALL(t))-t.be);} inline uint _rand(){ if(RAND_MAX < (1<<15)) return (rand()<<15) | rand(); else return rand(); } #define RND _rand() #define ri _rint() #define rui _ruint() #define rll _rll() #define rull _rull() #define rchar _rchar() #define rstr _rstr() #ifndef FASTIO inline void _fin(int){} inline void _fout(int){} #ifdef IOSTREAM inline int _rint(){int r;cin>>r;return r;} inline uint _ruint(){uint r;cin>>r;return r;} inline ull _rull(){ull r;cin>>r;return r;} inline ll _rll(){ll r;cin>>r;return r;} inline char _rchar(){return cin.get();} inline string _rstr(){string s;cin>>s;return s;} template<typename T>inline void W(const T& i){cout<<i; cout.flush();} inline void W(){W('\n');} #else inline int _rint(){int r;scanf("%d",&r);return r;} inline uint _ruint(){uint r;scanf("%u",&r);return r;} inline ull _rull(){ull r;scanf("%" SCNu64,&r);return r;} inline ull _rll(){ll r;scanf("%" SCNd64,&r);return r;} inline char _rchar(){return getchar();} inline string _rstr(){ string s; char c=rchar; while(c!=-1 && !isgraph(c))c=rchar; for(;;){ if(!isgraph(c))break; s+=c; c=rchar; } return s; } inline void W(char a='\n'){printf("%c",a);} inline void W(const string&s){printf("%s",s.c_str());} inline void W(int a){printf("%d",a);} inline void W(uint a){printf("%u",a);} inline void W(const ui64& a){printf("%" PRIu64,a);} inline void W(const i64& a){printf("%" PRIi64,a);} inline void W(const char *str){fputs(str,stdout);} inline void W(const double& d){printf("%.20f",d);} inline void W(const long double& d){printf("%.50Lf",d);} #endif #else #ifndef NO_UNLOCKED #define FREAD fread_unlocked #define FWRITE fwrite_unlocked #else #define FREAD fread #define FWRITE fwrite #endif // todo: test on pointers const int _IB=32768;const int _OB=32768; char _iB[_IB];int _iP = _IB; char _oB[_OB];int _oP = 0; inline void _fin(int mc){ int r=_IB-_iP; if(r>mc)return; FOR(i,r) _iB[i]=_iB[i+_iP]; // TODO: try memcpy int rd=FREAD(_iB+r,1,_iP,stdin); if(rd!=_iP) _iB[r+rd]=0; _iP = 0; } inline void _fout(int mc){ if(_OB-_oP>mc)return; FWRITE(_oB,1,_oP,stdout); _oP = 0; } inline void _W(char c='\n'){_oB[_oP++]=c;} // unchecked! inline char _rc(){return _iB[_iP++];} inline char _rchar(){_fin(1);return _rc();} inline string _rstr(){ string s; for(;;){ char c=rchar; if(!isprint(c)||isspace(c))break; s+=c; } return s; } template<class T>inline T _rur(){ // unchecked! T r=0;char c=_rc(); do c-='0',r=r*10+c,c=_rc();while(c>='0'); return r;} template<class T>inline T _rsi(){ // unchecked! char c=_rc(); while(c<'-')c=_rc(); if(c=='-')return -_rur<T>(); else{--_iP;return _rur<T>();}} template<class T>inline T _ru(){ // unchecked! while(_rc()<'0');--_iP; return _rur<T>(); } inline int _rint(){_fin(15);return _rsi<int>();} inline uint _ruint(){_fin(15);return _ru<uint>();} inline ll _rll(){_fin(25);return _rsi<ll>();} inline ull _rull(){_fin(25);return _ru<ull>();} const int _rs = 20; char _r[_rs]; template<class T>inline void _w(T i){ // unchecked! if(numeric_limits<T>::is_signed and i<0) _W('-'), i=-i; else if(i==0){_W('0'); return;} int pos = _rs; do _r[--pos]='0'+i%10, i/=10; while(i); do _W(_r[pos++]); while(pos<_rs); } inline void W(const char c){_fout(2);_W(c);} inline void W(const int32_t& i){_fout(15);_w(i);} inline void W(const uint32_t& i){_fout(15);_w(i);} inline void W(const int64_t& i){_fout(25);_w(i);} inline void W(const uint64_t& i){_fout(25);_w(i);} inline void W(const char*str, int l){ for(int b=0; b<l; b+=_OB){ int num = min(_OB,l-b); _fout(num); memcpy(&_oB[_oP], str+b, num); _oP+=num; } } inline void W(const char*str){W(str,strlen(str));} inline void W(const string&s){W(s.c_str(),s.siz);} inline void W(double d){_fout(50);_oP+=sprintf(&_oB[_oP],"%.20f",d);} // todo #endif template<class A,class B>inline void W(const A&a,const B&b){W(a);W(b);} template<class A,class B,class C> inline void W(const A&a,const B&b,const C&c){W(a);W(b);W(c);} template<class A,class B,class C,class DD> inline void W(const A&a,const B&b,const C&c,const DD&dd){W(a);W(b);W(c);W(dd);} template<class T>inline void sortall(T&t){sort(t.be,t.en);} template<class T>inline void uniqall(T&t){t.resize(unique(t.be,t.en)-t.be);} #define rgraph _rgraph() inline char _rgraph(){char c;do c=rchar;while(!isgraph(c));return c;} #define subset(a,b) (a&b==a) #define kbegin(k) ((1<<k)-1) #define kend(k,n) ((kbegin(k)<<n-k)+1) inline uint knext(uint a){uint b=(a|(a-1))+1;return b|((a^b)>>(__builtin_ctz(a)+2));} #define fors(x,k,n) for(uint x=kbegin(k);x<kend(k,n);x=knext(x)) struct _O{template<class T>_O&operator<(const T&_t){ oc(1);W(_t);oc(0); return *this; }}O; struct _I{ _I&operator>(int32_t&_t){_t=ri;return*this;} _I&operator>(uint32_t&_t){_t=rui;return*this;} _I&operator>(int64_t&_t){_t=rll;return*this;} _I&operator>(uint64_t&_t){_t=rull;return*this;} _I&operator>(char&_t){_t=rchar;return*this;} _I&operator>(string&_t){_t=rstr;return*this;} }I; inline void tc(); #ifdef GEN int argc; char **argv; int main(int _argc, char *_argv[]){ argc = _argc; argv = _argv; if(argc<2){ O < "Usage: " < argv[0] < " NR_TESTU" < N; return 1; } srand(atoi(argv[1])); #else int main(){ #endif //Log log; //log.autoFlush = true; //Log::global() = &log; #ifdef FASTIO setvbuf(stdin,0,_IONBF,0);setvbuf(stdout,0,_IONBF,0); #elif defined IOSTREAM ios_base::sync_with_stdio(0);cout.precision(10);cout.setf(ios::fixed,ios::floatfield); #if not defined DEBUG_BUILD and not defined COLOR_CONSOLE cin.tie(0); #endif #endif #ifdef DEBUG_BUILD #ifdef _WIN32 ec(0); oc(0); #else // TODO: *nix #endif #endif #ifdef READ_NUM_TEST_CASES int nz=rui;FOR(i,nz) #endif tc(); #ifdef FASTIO FWRITE(_oB,1,_oP,stdout); #endif return 0; } // END OF TEMPLATE //////////////////////////////////////////////////////////////////////////////// // // // ACTUAL SOLUTION BEGINS HERE: // // // //////////////////////////////////////////////////////////////////////////////// #include "poszukiwania.h" #include "message.h" typedef SuperHash3<> H; //typedef SuperHash2<> H; //typedef H2<> H; // meta int numberOfNodes; int myNodeId; // input int textSize; int pattSize; // common const int base = 1000000117; int numPrzylozenia; int przylozeniaPerNode; H patternHash; inline int seqAt(int i){ return SeqAt(i+1)+1; } inline int signalAt(int i){ return SignalAt(i+1)+1; } const int maxMessageSize = 256 * 1024; const int hashSize = 3*4 + 8; // !!! inline void putHash(int target, const H& value){ PutInt(target, value.h1); PutInt(target, value.h2); PutInt(target, value.h3); PutLL(target, value.h); } inline H getHash(int from){ H h; h.h1 = GetInt(from); h.h2 = GetInt(from); h.h3 = GetInt(from); h.h = GetLL(from); return h; } // instance 0 int numTextPrefsumBlocks; int textPrefsumBlockSize; int blocksPerNode; vector<H> textPrefsumSparse; //ull textPrefsumSparse2[1010000000 / textPrefsumBlockSize]; // worker inline pii getPrefsumFragmentBounds(int iBlock){ int fr = iBlock*textPrefsumBlockSize; int to = (iBlock+1)*textPrefsumBlockSize; if(to >= textSize) to = textSize; if(fr>to) fr = to; return mp(fr,to); } void workerComputePrefsumFragment(){ textPrefsumBlockSize = sqrt(textSize); if(textPrefsumBlockSize <= 0) textPrefsumBlockSize = 1; numTextPrefsumBlocks = (textSize + textPrefsumBlockSize - 1) / textPrefsumBlockSize; if(myNodeId==0) E < "prefsum block size: " < textPrefsumBlockSize < NL; blocksPerNode = (numTextPrefsumBlocks + numberOfNodes - 1) / numberOfNodes; int messageSize = 0; FOR(i,blocksPerNode){ int iBlock = blocksPerNode*myNodeId + i; pii bounds = getPrefsumFragmentBounds(iBlock); if(bounds.fi == bounds.se) break; H cHash = 0; H basePow = fpow((H)base, bounds.fi); FO(j, bounds.fi, bounds.se){ cHash += basePow * seqAt(j); basePow *= base; } E < "computed hash " < cHash.h1 < " for block " < iBlock < NL; if(messageSize + hashSize > maxMessageSize){ Send(0); messageSize = 0; } putHash(0,cHash); messageSize += hashSize; } Send(0); } void masterSendPrefsums(){ if(myNodeId != 0) return; textPrefsumSparse.resize(numTextPrefsumBlocks+1); // receive prefsum fragments //FOR(kk,numberOfNodes){ //int fromNode = Receive(-1); FOR(fromNode,numberOfNodes){ Receive(fromNode); int messageSize = 0; FOR(i,blocksPerNode){ int iBlock = blocksPerNode*fromNode + i; pii bounds = getPrefsumFragmentBounds(iBlock); if(bounds.fi == bounds.se) break; if(messageSize + hashSize > maxMessageSize){ Receive(fromNode); messageSize = 0; } H h = getHash(fromNode); messageSize += hashSize; textPrefsumSparse[iBlock+1] = h; } } FOR(i,numTextPrefsumBlocks) textPrefsumSparse[i+1] += textPrefsumSparse[i]; // send required prefsums FOR(iNode,numberOfNodes){ int przylozenie = przylozeniaPerNode*iNode; int fullBlocks = przylozenie / textPrefsumBlockSize; int fullBlocks2 = (przylozenie + pattSize) / textPrefsumBlockSize; putHash(iNode, textPrefsumSparse[fullBlocks]); putHash(iNode, textPrefsumSparse[fullBlocks2]); Send(iNode); } } inline void tc(){ numberOfNodes = NumberOfNodes(); myNodeId = MyNodeId(); textSize = SeqLength(); pattSize = SignalLength(); numPrzylozenia = textSize-pattSize+1; przylozeniaPerNode = (numPrzylozenia+numberOfNodes-1) / numberOfNodes; // compute prefsums workerComputePrefsumFragment(); masterSendPrefsums(); if(myNodeId==0) E < "prefsums computed" < NL; // compute pattern hash patternHash = 0; OF(i,0,pattSize){ patternHash *= base; patternHash += signalAt(i); } if(myNodeId==0) E < "pattern hash computed. " < patternHash.h1 < NL; int fr = myNodeId*przylozeniaPerNode; int to = fr + przylozeniaPerNode; Receive(0); H prefsumLeft = getHash(0); H prefsumRight = getHash(0); //if(myNodeId==0) cerr << "prefsums sent" << endl; int iLeft = fr / textPrefsumBlockSize * textPrefsumBlockSize; H basePowLeft = fpow((H)base,iLeft); FO(i, iLeft, fr){ prefsumLeft += basePowLeft * seqAt(i); basePowLeft *= base; } int iRight = (fr + pattSize) / textPrefsumBlockSize * textPrefsumBlockSize; H basePowRight = fpow((H)base,iRight); FO(i, iRight, fr + pattSize){ prefsumRight += basePowRight * seqAt(i); basePowRight *= base; } patternHash *= fpow((H)base, fr); int result = 0; FO(i, fr, to){ if(i >= numPrzylozenia)break; // check przylozenie 'i' H candHash = prefsumRight - prefsumLeft; E < "position " < i < ": candHash==" < candHash.h1 < " patternHash==" < patternHash.h1 < NL; if(candHash == patternHash){ E < "match at position " < i < NL; ++result; } if(i < to-1){ prefsumLeft += basePowLeft * seqAt(i); prefsumRight += basePowRight * seqAt(i+pattSize); basePowLeft *= base; basePowRight *= base; patternHash *= base; } } // collect result PutInt(0,result); Send(0); if(myNodeId==0){ result = 0; FOR(kk,numberOfNodes){ int fromNode = Receive(-1); result += GetInt(fromNode); } O < result < NL; } } |