#pragma GCC optimize("O3") #include #include #include #include using namespace std; // cpp_range (generic range class, very small equivalent of boost::irange) // (C) @cielavenir under Boost Software License. // note: end position is exclusive. #ifdef BOOST #include #define make_range boost::irange #else #include template class range{ public: struct iterator{ const T a,b; T p; const long long d; //because T might be unsigned. iterator(T _a,T _b,T _p,long long _d=1):a(_a),b(_b),p(_p),d(_d){} public: typedef T value_type; typedef T& reference; typedef T* pointer; typedef std::ptrdiff_t difference_type; typedef std::random_access_iterator_tag iterator_category; //copy iterator(const iterator &other):a(other.a),b(other.b),p(other.p),d(other.d){} iterator operator=(const iterator &other){return iterator(other.a,other.b,other.p,other.d);} //advance iterator& operator+=(T n){p+=n*d;return *this;} iterator& operator-=(T n){return *this+=(-n);} iterator& operator++(){return *this+=1;} iterator& operator--(){return *this-=1;} iterator operator+(T n) const{return iterator(a,b,p+n*d);} iterator operator-(T n) const{return *this+(-n);} //difference T operator-(const iterator& other) const{return p-other.p;} //equality bool operator==(const iterator& other) const{return a==other.a && b==other.b && d==other.d && p==other.p;} bool operator!=(const iterator& other) const{return !(*this==other);} //compare bool operator<(const iterator& other) const{return a==other.a && b==other.b && d==other.d && p*d(const iterator& other) const{return a==other.a && b==other.b && d==other.d && p*d>other.p*d;} //reference const T& operator*() const{return p;} }; private: const T a,b; T siz; long long d; public: range(T _a,T _b,long long _d=1):a(_a),b(_b){ d=_d; if(d==0)d=1; // siz=( (d>0?(b-a):(a-b)) - 1) / (d>0?d:-d); } T operator[](T n){return a+n*d;} iterator begin(){return iterator(a,a+d*siz,a,d);} iterator end(){return iterator(a,a+d*siz,a+d*(siz+1),d);} iterator rbegin(){return iterator(b-d*siz,b,b,-1*d);} iterator rend(){return iterator(b-d*siz,b,b-d*(siz+1),-1*d);} T size(){return siz+1;} }; template range make_range(T a,T b,long long d=1){return range(a,b,d);} #endif int solve(const string &s){ int len=s.size(); vectoral; vectorar; vectorah(len); vectorast; for(auto &i: make_range(0,len)){ if(s[i]=='('){ al.push_back(i); }else if(s[i]==')'){ ar.push_back(i); }else if(s[i]=='^'){ ah[i]++; }else if(s[i]=='*'){ ast.push_back(i); } } for(auto &i: make_range(1,len))ah[i]+=ah[i-1]; int ret=0; for(auto &l:al){ bool f=false; for(auto &r:ar){ if(l>=r)continue; if(f){ret++;continue;} auto lr=make_range(l,r); int hidx=distance(ah.begin(),lower_bound(ah.begin(),ah.end(),ah[l]+2)); if(hidx>=r)continue; auto st=lower_bound(ast.begin(),ast.end(),hidx); if(st!=ast.end()&&*st