結果

問題 No.457 (^^*)
ユーザー cielciel
提出日時 2016-12-08 21:47:30
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 575 ms / 2,000 ms
コード長 3,312 bytes
コンパイル時間 1,015 ms
コンパイル使用メモリ 78,912 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-19 02:35:23
合計ジャッジ時間 2,919 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,384 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,384 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 3 ms
4,380 KB
testcase_11 AC 7 ms
4,376 KB
testcase_12 AC 575 ms
4,380 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 567 ms
4,380 KB
testcase_16 AC 3 ms
4,380 KB
testcase_17 AC 3 ms
4,376 KB
testcase_18 AC 1 ms
4,376 KB
testcase_19 AC 2 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma GCC optimize("O3")

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
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 <boost/range.hpp>
#define make_range boost::irange
#else
#include <iterator>
template<typename T>
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<other.p*d;}
		bool operator>(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<typename T>
range<T> make_range(T a,T b,long long d=1){return range<T>(a,b,d);}
#endif


int solve(const string &s){
	int len=s.size();
	vector<int>al;
	vector<int>ar;
	vector<int>ah(len);
	vector<int>ast;
	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<r){
				ret++;
				f=true;
			}
		}
	}
	return ret;
}
int main(){
	string s;
	getline(cin,s);
	cout<<solve(s)<<' ';
	reverse(s.begin(),s.end());
	for(auto &e:s){
		if(e=='(')e=')';
		else if(e==')')e='(';
	}
	cout<<solve(s)<<endl;
}
0