제출 #1326061

#제출 시각아이디문제언어결과실행 시간메모리
1326061sporknivesHomework (CEOI22_homework)C++20
23 / 100
257 ms151312 KiB
#include <bits/stdc++.h>
#define int long long
using namespace std;
typedef pair<int,int> pii;
// 1 - max, 2 - min
int op[2000004];
void process(string s, int n) {
	//cout<<"processing string: "<<s<<" "<<n<<'\n';
	int cnt=0;
	if(s=="?") {
		op[n]=-1; return;
	}
	else if(s[1]=='i')op[n]=0;
	else op[n]=1;
	
	if(s[4]=='?') {
		process("?",n*2+1);
		process(s.substr(6,s.length()-7), n*2+2);
		return;
	}
	
	int idx=-1;
	for(int i=4;i<s.length();i++) {
		if(s[i]=='(') cnt++;
		else if(s[i]==')') {
			cnt--;
			if(cnt==0) {
				process(s.substr(4,i-3),n*2+1);
				idx=i;
				break;
			}
		}
	}
	
	process(s.substr(idx+2,s.length()-idx-3),n*2+2);
}

pii range[2000004];
int leaves[2000004];
void dfs(int x) {
	if(op[x]==-1) {
		range[x]={1,1};
		leaves[x]=1;
		return;
	}
	dfs(x*2+1);
	dfs(x*2+2);
	
	leaves[x] = leaves[x*2+1] + leaves[x*2+2];
	if(op[x]==1) {
		range[x].first = range[x*2+1].first + range[x*2+2].first;
		range[x].second = leaves[x] - min(leaves[x*2+1]-range[x*2+1].second, 
					leaves[x*2+2]-range[x*2+2].second);
	}
	else {
		range[x].first = min(range[x*2+1].first, range[x*2+2].first);
		range[x].second = leaves[x] - (leaves[x*2+1]-range[x*2+1].second) -
						(leaves[x*2+2]-range[x*2+2].second) - 1;
	}
}

signed main() {
	string s; cin>>s;
	process(s,0);
	
	dfs(0);
	cout<<range[0].second-range[0].first+1;
	return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...